### Buffer Length Example Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Demonstrates how to get the number of elements (pixels) in a buffer slice. This is equivalent to the total number of pixels in the buffer. ```rust let a = [1, 2, 3]; assert_eq!(a.len(), 3); ``` -------------------------------- ### Surface::window Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Get a reference to the underlying window handle. ```APIDOC ## Surface::window ### Description Get a reference to the underlying window handle. ### Signature ```rust pub fn window(&self) -> &W ``` ### Returns A reference to the underlying window handle. ``` -------------------------------- ### Check if Buffer Starts With Prefix Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns true if the buffer starts with the specified slice (needle) or is equal to it. ```rust let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(v.starts_with(&v)); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50])); ``` ```rust let v = &[10, 40, 30]; assert!(v.starts_with(&[])); let v: &[u8] = &[]; assert!(v.starts_with(&[])); ``` -------------------------------- ### Buffer Split First Example Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Splits the buffer into its first element and the rest of the slice. This is useful for processing the first pixel separately. ```rust let x = &[0, 1, 2]; if let Some((first, elements)) = x.split_first() { assert_eq!(first, &0); assert_eq!(elements, &[1, 2]); } ``` -------------------------------- ### Mutable Windows with Cell Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Demonstrates how to achieve mutable window-like operations using `Cell` when direct mutable windows are not possible due to lifetime constraints. This example swaps elements across windows. ```rust use std::cell::Cell; let mut array = ['R', 'u', 's', 't', ' ', '2', '0', '1', '5']; let slice = &mut array[..]; let slice_of_cells: &[Cell] = Cell::from_mut(slice).as_slice_of_cells(); for w in slice_of_cells.windows(3) { Cell::swap(&w[0], &w[2]); } assert_eq!(array, ['s', 't', ' ', '2', '0', '1', '5', 'u', 'R']); ``` -------------------------------- ### Implement HasWindowHandle for Surface Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Provides a method to get a handle to the window associated with the surface. ```rust fn window_handle(&self) -> Result, HandleError> ``` -------------------------------- ### Basic Window and Buffer Drawing with Softbuffer Source: https://docs.rs/softbuffer/latest/softbuffer/index.html This example sets up a winit window and uses softbuffer to create a drawing surface. It handles window events, resizes the surface, and fills the buffer with a color pattern before presenting it. Requires the 'winit' and 'softbuffer' crates. ```rust use std::num::NonZeroU32; use std::rc::Rc; use winit::event::{Event, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::window::Window; #[path = "../examples/utils/winit_app.rs"] mod winit_app; fn main() { let event_loop = EventLoop::new().unwrap(); let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap(); let mut app = winit_app::WinitAppBuilder::with_init( |elwt| { let window = elwt.create_window(Window::default_attributes()); Rc::new(window.unwrap()) }, |_elwt, window| softbuffer::Surface::new(&context, window.clone()).unwrap(), ) .with_event_handler(|window, surface, window_id, event, elwt| { elwt.set_control_flow(ControlFlow::Wait); if window_id != window.id() { return; } match event { WindowEvent::RedrawRequested => { let Some(surface) = surface else { eprintln!("RedrawRequested fired before Resumed or after Suspended"); return; }; let size = window.inner_size(); surface .resize( NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap(), ) .unwrap(); let mut buffer = surface.buffer_mut().unwrap(); for index in 0..(buffer.width().get() * buffer.height().get()) { let y = index / buffer.width().get(); let x = index % buffer.width().get(); let red = x % 255; let green = y % 255; let blue = (x * y) % 255; buffer[index as usize] = blue | (green << 8) | (red << 16); } buffer.present().unwrap(); } WindowEvent::CloseRequested => { elwt.exit(); } _ => {} } }); event_loop.run_app(&mut app).unwrap(); } ``` -------------------------------- ### Buffer Is Empty Example Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Shows how to check if a buffer slice is empty, meaning it contains no pixels. An empty buffer has a length of 0. ```rust let a = [1, 2, 3]; assert!(!a.is_empty()); let b: &[i32] = &[]; assert!(b.is_empty()); ``` -------------------------------- ### Pixel Format Example Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Illustrates how to construct the u32 pixel format using bitwise operations. The highest 8 bits are unused, followed by 8 bits each for red, green, and blue channels. ```rust 00000000RRRRRRRRGGGGGGGGBBBBBBBB ``` -------------------------------- ### Get Element or Subslice Safely Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `get` to safely retrieve a reference to an element by its index or a subslice by a range. Returns `None` if the index is out of bounds. ```rust let v = [10, 40, 30]; assert_eq!(Some(&40), v.get(1)); assert_eq!(Some(&[10, 40][..]), v.get(0..2)); assert_eq!(None, v.get(3)); assert_eq!(None, v.get(0..4)); ``` -------------------------------- ### Buffer First Element Example Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Retrieves a reference to the first pixel in the buffer, or `None` if the buffer is empty. The first pixel corresponds to the upper-leftmost pixel. ```rust let v = [10, 40, 30]; assert_eq!(Some(&10), v.first()); let w: &[i32] = &[]; assert_eq!(None, w.first()); ``` -------------------------------- ### Buffer Split Last Example Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Splits the buffer into its last element and the preceding slice. This is useful for operations that need to access the end of the buffer. ```rust let x = &[0, 1, 2]; if let Some((last, elements)) = x.split_last() { assert_eq!(last, &2); assert_eq!(elements, &[0, 1]); } ``` -------------------------------- ### get Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns a reference to an element or subslice. Returns `None` if the index is out of bounds. ```APIDOC ## pub fn get(&self, index: I) -> Option<&>::Output> ### Description Returns a reference to an element or subslice depending on the type of index. * If given a position, returns a reference to the element at that position or `None` if out of bounds. * If given a range, returns the subslice corresponding to that range, or `None` if out of bounds. ### Parameters #### Path Parameters - **index** (I): The index or range to access. ### Response #### Success Response (Option<&>::Output>) - Returns `Some` with a reference to the element or subslice if the index is valid. - Returns `None` if the index is out of bounds. ### Examples ```rust let v = [10, 40, 30]; assert_eq!(Some(&40), v.get(1)); assert_eq!(Some(&[10, 40][..]), v.get(0..2)); assert_eq!(None, v.get(3)); assert_eq!(None, v.get(0..4)); ``` ``` -------------------------------- ### Get underlying window handle Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Retrieves a reference to the window handle associated with the surface. ```rust pub fn window(&self) -> &W ``` -------------------------------- ### array_windows Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator over overlapping windows of `N` elements of a slice, starting at the beginning of the slice. This is the const generic equivalent of `windows`. ```APIDOC ## pub fn array_windows(&self) -> ArrayWindows<'_, T, N> ### Description Returns an iterator over overlapping windows of `N` elements of a slice, starting at the beginning of the slice. ### Panics Panics if `N` is zero. ### Examples ``` let slice = [0, 1, 2, 3]; let mut iter = slice.array_windows(); assert_eq!(iter.next().unwrap(), &[0, 1]); assert_eq!(iter.next().unwrap(), &[1, 2]); assert_eq!(iter.next().unwrap(), &[2, 3]); assert!(iter.next().is_none()); ``` ``` -------------------------------- ### Buffer Split First Mutable Example Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Provides mutable access to the first element and the rest of the buffer slice. Allows modifying the first pixel and subsequent pixels. ```rust let x = &mut [0, 1, 2]; if let Some((first, elements)) = x.split_first_mut() { *first = 3; elements[0] = 4; elements[1] = 5; } assert_eq!(x, &[3, 4, 5]); ``` -------------------------------- ### split_at Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Divides a slice into two at a given index. The first part contains elements from the start up to (but not including) the index, and the second part contains elements from the index to the end. ```APIDOC ## split_at ### Description Divides one slice into two at an index. The first will contain all indices from `[0, mid)` (excluding the index `mid` itself) and the second will contain all indices from `[mid, len)` (excluding the index `len` itself). ### Parameters #### Path Parameters - **mid** (usize) - Description: The index at which to split the slice. ### Examples ``` let v = ['a', 'b', 'c']; { let (left, right) = v.split_at(0); assert_eq!(left, []); assert_eq!(right, ['a', 'b', 'c']); } { let (left, right) = v.split_at(2); assert_eq!(left, ['a', 'b']); assert_eq!(right, ['c']); } { let (left, right) = v.split_at(3); assert_eq!(left, ['a', 'b', 'c']); assert_eq!(right, []); } ``` ``` -------------------------------- ### Split Buffer with Predicate from End Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Splits a buffer into subslices based on a predicate, starting from the end. The matched element is excluded. Limited to n items. ```rust let v = [10, 40, 30, 20, 60, 50]; for group in v.rsplitn(2, |num| *num % 3 == 0) { println!("{group:?}"); } ``` -------------------------------- ### Get Raw Pointer to Buffer Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Obtain a raw pointer to the beginning of the slice's buffer using `as_ptr`. Ensure the slice outlives the pointer and that the memory is not mutated through this pointer. ```rust let x = &[1, 2, 4]; let x_ptr = x.as_ptr(); unsafe { for i in 0..x.len() { assert_eq!(x.get_unchecked(i), &*x_ptr.add(i)); } } ``` -------------------------------- ### Iterate Over Slice in Reverse Chunks Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `rchunks` to get an iterator over immutable slices of a specified `chunk_size`, starting from the end of the slice. The last chunk may be smaller if the slice length is not divisible by `chunk_size`. Panics if `chunk_size` is zero. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.rchunks(2); assert_eq!(iter.next().unwrap(), &['e', 'm']); assert_eq!(iter.next().unwrap(), &['o', 'r']); assert_eq!(iter.next().unwrap(), &['l']); assert!(iter.next().is_none()); ``` -------------------------------- ### Iterate Over Slice in Reverse Mutable Chunks Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `rchunks_mut` to get an iterator over mutable slices of a specified `chunk_size`, starting from the end of the slice. The last chunk may be smaller if the slice length is not divisible by `chunk_size`. Panics if `chunk_size` is zero. ```rust let v = &mut [0, 0, 0, 0, 0]; let mut count = 1; for chunk in v.rchunks_mut(2) { for elem in chunk.iter_mut() { *elem += count; } count += 1; } assert_eq!(v, &[3, 2, 2, 1, 1]); ``` -------------------------------- ### Surface::new Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Creates a new surface for the context for the provided window. ```APIDOC ## Surface::new ### Description Creates a new surface for the context for the provided window. ### Signature ```rust pub fn new(context: &Context, window: W) -> Result ``` ### Parameters * `context`: A reference to the `Context` for the display. * `window`: The window handle for which to create the surface. ``` -------------------------------- ### Get Last Element of a Slice Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `last` to get an immutable reference to the last element. Returns `None` if the slice is empty. ```rust let v = [10, 40, 30]; assert_eq!(Some(&30), v.last()); let w: &[i32] = &[]; assert_eq!(None, w.last()); ``` -------------------------------- ### Context::new Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Context.html Creates a new instance of the Context struct, initializing it with the provided display handle. This is the primary way to obtain a Context object. ```APIDOC ## pub fn new(display: D) -> Result ### Description Creates a new instance of this struct, using the provided display. ### Method `new` ### Parameters - **display** (D) - The display handle specific to the platform. ### Returns A `Result` containing either a new `Context` instance on success or a `SoftBufferError` on failure. ``` -------------------------------- ### Surface::fetch Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Copies the window contents into a buffer. ```APIDOC ## Surface::fetch ### Description Copies the window contents into a buffer. ### Platform Dependent Behavior * On X11, the window must be visible. * On AppKit, UIKit, Redox and Wayland, this function is unimplemented. * On Web, this will fail if the content was supplied by a different origin depending on the sites CORS rules. ### Signature ```rust pub fn fetch(&mut self) -> Result, SoftBufferError> ``` ### Returns A `Result` containing a `Vec` representing the window contents, or an error. ``` -------------------------------- ### Get Mutable Reference to Last Element Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `last_mut` to get a mutable reference to the last element. Returns `None` if the slice is empty. ```rust let x = &mut [0, 1, 2]; if let Some(last) = x.last_mut() { *last = 10; } assert_eq!(x, &[0, 1, 10]); let y: &mut [i32] = &mut []; assert_eq!(None, y.last_mut()); ``` -------------------------------- ### Get mutable buffer for rendering Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Returns a mutable buffer for rendering the next frame. The buffer size must be set using `Surface::resize` beforehand. Initial contents may be zeroed or contain a previous frame; check with `Buffer::age`. On DRM/KMS, users must manually wait for page flips before sending new frames. ```rust pub fn buffer_mut(&mut self) -> Result, SoftBufferError> ``` -------------------------------- ### Create a new Surface Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Creates a new surface for a given context and window. Ensure the context and window handles are valid. ```rust pub fn new(context: &Context, window: W) -> Result ``` -------------------------------- ### Get Last Chunk of a Slice Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `last_chunk::` to get an array reference to the last `N` elements. Returns `None` if the slice is shorter than `N`. ```rust let u = [10, 40, 30]; assert_eq!(Some(&[40, 30]), u.last_chunk::<2>()); let v: &[i32] = &[10]; assert_eq!(None, v.last_chunk::<2>()); let w: &[i32] = &[]; assert_eq!(Some(&[]), w.last_chunk::<0>()); ``` -------------------------------- ### Get First Chunk of a Slice Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `first_chunk::` to get an array reference to the first `N` elements. Returns `None` if the slice is shorter than `N`. ```rust let u = [10, 40, 30]; assert_eq!(Some(&[10, 40]), u.first_chunk::<2>()); let v: &[i32] = &[10]; assert_eq!(None, v.first_chunk::<2>()); let w: &[i32] = &[]; assert_eq!(Some(&[]), w.first_chunk::<0>()); ``` -------------------------------- ### CloneToUninit Method (Nightly) Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Rect.html Performs copy-assignment from self to a raw pointer destination. This is an experimental, nightly-only API. ```rust unsafe fn clone_to_uninit(&self, dest: *mut u8) ``` -------------------------------- ### Get Mutable Last Chunk of a Slice Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `last_chunk_mut::` to get a mutable array reference to the last `N` elements. Returns `None` if the slice is shorter than `N`. ```rust let x = &mut [0, 1, 2]; if let Some(last) = x.last_chunk_mut::<2>() { last[0] = 10; last[1] = 20; } assert_eq!(x, &[0, 10, 20]); assert_eq!(None, x.last_chunk_mut::<4>()); ``` -------------------------------- ### Get Mutable First Chunk of a Slice Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `first_chunk_mut::` to get a mutable array reference to the first `N` elements. Returns `None` if the slice is shorter than `N`. ```rust let x = &mut [0, 1, 2]; if let Some(first) = x.first_chunk_mut::<2>() { first[0] = 5; first[1] = 4; } assert_eq!(x, &[5, 4, 2]); assert_eq!(None, x.first_chunk_mut::<4>()); ``` -------------------------------- ### Get Raw Pointer Range Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Get a half-open range of raw pointers spanning the slice using `as_ptr_range`. The end pointer points one past the last element. Useful for C interop or checking pointer inclusion. ```rust let a = [1, 2, 3]; let x = &a[1] as *const _; let y = &5 as *const _; assert!(a.as_ptr_range().contains(&x)); assert!(!a.as_ptr_range().contains(&y)); ``` -------------------------------- ### starts_with Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns `true` if `needle` is a prefix of the slice or equal to the slice. ```APIDOC ## pub fn starts_with(&self, needle: &[T]) -> bool ### Description Returns `true` if `needle` is a prefix of the slice or equal to the slice. ### Parameters - `needle`: `&[T]` - The slice to check as a prefix. ### Returns - `bool` - `true` if `needle` is a prefix of the slice, `false` otherwise. ``` -------------------------------- ### into Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Calls `U::from(self)`. This conversion is whatever the implementation of `From for U` chooses to do. ```APIDOC ## fn into(self) -> U Calls `U::from(self)`. That is, this conversion is whatever the implementation of `From for U` chooses to do. ``` -------------------------------- ### Buffer::present Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Presents the buffer to the window. This operation may block on certain platforms. ```APIDOC ## Buffer::present ### Description Presents buffer to the window. ### Method `present(self) -> Result<(), SoftBufferError>` ### Platform dependent behavior On Wayland, calling this function may send requests to the underlying `wl_surface`. The graphics context may issue `wl_surface.attach`, `wl_surface.damage`, `wl_surface.damage_buffer` and `wl_surface.commit` requests when presenting the buffer. If the caller wishes to synchronize other surface/window changes, such requests must be sent to the Wayland compositor before calling this function. ``` -------------------------------- ### iter Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator over the slice, yielding items from start to end. ```APIDOC ## iter ### Description Returns an iterator over the slice. The iterator yields all items from start to end. ### Method `iter(&self) -> Iter<'_, T>` ### Examples ``` let x = &[1, 2, 4]; let mut iterator = x.iter(); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&2)); assert_eq!(iterator.next(), Some(&4)); assert_eq!(iterator.next(), None); ``` ``` -------------------------------- ### Fetch window contents into a buffer Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Copies the current window contents into a buffer. Note platform-dependent behavior: on X11, the window must be visible; on AppKit, UIKit, Redox, and Wayland, this is unimplemented; on Web, CORS rules may apply. ```rust pub fn fetch(&mut self) -> Result, SoftBufferError> ``` -------------------------------- ### as_rchunks Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Splits the slice into a slice of N-element arrays starting from the end, and a remainder slice. ```APIDOC ## pub fn as_rchunks(&self) -> (&[T], &[[T; N]]) ### Description Splits the slice into a slice of `N`-element arrays, starting at the end of the slice, and a remainder slice with length strictly less than `N`. ### Parameters * `N`: const generic parameter for the size of each chunk. ### Returns A tuple containing a remainder slice and a slice of `N`-element arrays. ### Panics Panics if `N` is zero. ### Examples ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let (remainder, chunks) = slice.as_rchunks(); assert_eq!(remainder, &['l']); assert_eq!(chunks, &[['o', 'r'], ['e', 'm']]); ``` ``` -------------------------------- ### Implement HasRawWindowHandle (Deprecated) Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Provides a deprecated method for accessing the raw window handle. Use `HasWindowHandle` instead. ```rust fn raw_window_handle(&self) -> Result ``` -------------------------------- ### Implement AsRef for Surface Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Provides a way to convert a `Surface` into a reference of its underlying window handle type. ```rust fn as_ref(&self) -> &W ``` -------------------------------- ### Buffer::present_with_damage Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Presents the buffer to the window, specifying damage regions. This is equivalent to `present` on platforms where damage regions are not supported. ```APIDOC ## Buffer::present_with_damage ### Description Presents buffer to the window, with damage regions. ### Method `present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError>` ### Platform dependent behavior Supported on: * Wayland * X, when XShm is available * Win32 * Web Otherwise this is equivalent to `Self::present`. ``` -------------------------------- ### iter_mut Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator that allows modifying each value in the slice, yielding items from start to end. ```APIDOC ## iter_mut ### Description Returns an iterator that allows modifying each value. The iterator yields all items from start to end. ### Method `iter_mut(&mut self) -> IterMut<'_, T>` ### Examples ``` let x = &mut [1, 2, 4]; for elem in x.iter_mut() { *elem += 2; } assert_eq!(x, &[3, 4, 6]); ``` ``` -------------------------------- ### strip_prefix Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns a subslice with the prefix removed if the slice starts with the given prefix. Returns None otherwise. ```APIDOC ## strip_prefix

(&self, prefix: &P) -> Option<&[T]> ### Description Returns a subslice with the prefix removed. If the slice starts with `prefix`, returns the subslice after the prefix, wrapped in `Some`. If `prefix` is empty, simply returns the original slice. If `prefix` is equal to the original slice, returns an empty slice. If the slice does not start with `prefix`, returns `None`. ### Parameters #### Path Parameters - **prefix** (P): The pattern to check for at the beginning of the slice. ### Response #### Success Response (Some) - **subslice** (&[T]): The subslice after the prefix. #### Failure Response (None) - Indicates the slice does not start with the specified prefix. ### Request Example ```rust let v = &[10, 40, 30]; assert_eq!(v.strip_prefix(&[10]), Some(&[40, 30][..])); let prefix : &str = "he"; assert_eq!(b"hello".strip_prefix(prefix.as_bytes()), Some(b"llo".as_ref())); ``` ``` -------------------------------- ### From Implementation Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Rect.html Allows creating a value of a type from itself. This is a simple identity conversion. ```rust fn from(t: T) -> T ``` -------------------------------- ### as_array Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Gets a reference to the underlying array if the specified length `N` matches the buffer's length. ```APIDOC ## as_array ### Description Gets a reference to the underlying array. If `N` is not exactly equal to the length of `self`, then this method returns `None`. ### Method `as_array(&self) -> Option<&[T; N]>` ``` -------------------------------- ### connect Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Deprecated since 1.3.0 and renamed to `join`. Flattens a slice of `T` into a single value `Self::Output`, inserting a specified separator between each element. ```APIDOC ## pub fn connect( &self, sep: Separator, ) -> <[T] as Join>::Output where [T]: Join, 👎Deprecated since 1.3.0: renamed to join Flattens a slice of `T` into a single value `Self::Output`, placing a given separator between each. ##### §Examples ``` assert_eq!(["hello", "world"].connect(" "), "hello world"); assert_eq!([[1, 2], [3, 4]].connect(&0), [1, 2, 0, 3, 4]); ``` ``` -------------------------------- ### TryFrom Implementation Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Rect.html Attempts to convert a value from one type to another, returning a Result. Use this for fallible conversions. ```rust type Error = Infallible ``` ```rust fn try_from(value: U) -> Result>::Error> ``` -------------------------------- ### rchunks_exact Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. The chunks are slices and do not overlap. ```APIDOC ## pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> ### Description Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. The chunks are slices and do not overlap. If `chunk_size` does not divide the length of the slice, then the last up to `chunk_size-1` elements will be omitted and can be retrieved from the `remainder` function of the iterator. ### Parameters #### Path Parameters - **chunk_size** (usize) - Required - The size of each chunk. ### Panics Panics if `chunk_size` is zero. ### Examples ``` let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.rchunks_exact(2); assert_eq!(iter.next().unwrap(), &['e', 'm']); assert_eq!(iter.next().unwrap(), &['o', 'r']); assert!(iter.next().is_none()); assert_eq!(iter.remainder(), &['l']); ``` ``` -------------------------------- ### rchunks Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. The chunks are slices and do not overlap. ```APIDOC ## pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> ### Description Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. ### Panics Panics if `chunk_size` is zero. ### Examples ``` let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.rchunks(2); assert_eq!(iter.next().unwrap(), &['e', 'm']); assert_eq!(iter.next().unwrap(), &['o', 'r']); assert_eq!(iter.next().unwrap(), &['l']); assert!(iter.next().is_none()); ``` ``` -------------------------------- ### rchunks_exact_mut Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. The chunks are mutable slices and do not overlap. ```APIDOC ## pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> ### Description Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. The chunks are mutable slices, and do not overlap. If `chunk_size` does not divide the length of the slice, then the last up to `chunk_size-1` elements will be omitted and can be retrieved from the `into_remainder` function of the iterator. ### Parameters #### Path Parameters - **chunk_size** (usize) - Required - The size of each chunk. ### Panics Panics if `chunk_size` is zero. ### Examples ``` let v = &mut [0, 0, 0, 0, 0]; let mut count = 1; for chunk in v.rchunks_exact_mut(2) { for elem in chunk.iter_mut() { *elem += count; } count += 1; } assert_eq!(v, &[0, 2, 2, 1, 1]); ``` ``` -------------------------------- ### Inserting into a Sorted Vector with partition_point Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `partition_point` to efficiently find the correct insertion index in a sorted vector to maintain sort order. This can be more efficient than `binary_search` followed by `insert` as it may shift fewer elements. ```rust let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let num = 42; let idx = s.partition_point(|&x| x <= num); // If `num` is unique, `s.partition_point(|&x| x < num)` (with `<`) is equivalent to // `s.binary_search(&num).unwrap_or_else(|x| x)`, but using `<=` will allow `insert` // to shift less elements. s.insert(idx, num); assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]); ``` -------------------------------- ### as_simd Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Splits a slice into a prefix, a middle of aligned SIMD types, and a suffix. This is a safe wrapper around `slice::align_to`, inheriting its guarantees. Note: This is a nightly-only experimental API. ```APIDOC ## pub fn as_simd(&self) -> (&[T], &[Simd], &[T]) ### Description Splits a slice into a prefix, a middle of aligned SIMD types, and a suffix. ### Type Parameters * `LANES`: The number of elements in each SIMD lane. ### Returns A tuple containing three slices: the prefix, the middle SIMD-aligned part, and the suffix. ### Panics This will panic if the size of the SIMD type is different from `LANES` times that of the scalar. This is unlikely with current trait restrictions. ### Examples ```rust #![feature(portable_simd)] use core::simd::prelude::*; let short = &[1, 2, 3]; let (prefix, middle, suffix) = short.as_simd::<4>(); assert_eq!(middle, []); let it = prefix.iter().chain(suffix).copied(); assert_eq!(it.collect::>(), vec![1, 2, 3]); fn basic_simd_sum(x: &[f32]) -> f32 { use std::ops::Add; let (prefix, middle, suffix) = x.as_simd(); let sums = f32x4::from_array([ prefix.iter().copied().sum(), 0.0, 0.0, suffix.iter().copied().sum(), ]); let sums = middle.iter().copied().fold(sums, f32x4::add); sums.reduce_sum() } let numbers: Vec = (1..101).map(|x| x as _).collect(); assert_eq!(basic_simd_sum(&numbers[1..99]), 4949.0); ``` ``` -------------------------------- ### rchunks_mut Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. The chunks are mutable slices, and do not overlap. ```APIDOC ## pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> ### Description Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end of the slice. ### Panics Panics if `chunk_size` is zero. ### Examples ``` let v = &mut [0, 0, 0, 0, 0]; let mut count = 1; for chunk in v.rchunks_mut(2) { for elem in chunk.iter_mut() { *elem += count; } count += 1; } assert_eq!(v, &[3, 2, 2, 1, 1]); ``` ``` -------------------------------- ### Resize Surface buffer Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Surface.html Sets the size of the buffer for the next frame. It's recommended to match the window size to avoid drawing issues. Use your windowing library to determine the window's dimensions. ```rust pub fn resize( &mut self, width: NonZeroU32, height: NonZeroU32, ) -> Result<(), SoftBufferError> ``` -------------------------------- ### Iterate Over Slice Elements Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator that yields immutable references to each element in the slice from start to end. Useful for reading elements. ```rust let x = &[1, 2, 4]; let mut iterator = x.iter(); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&2)); assert_eq!(iterator.next(), Some(&4)); assert_eq!(iterator.next(), None); ``` -------------------------------- ### as_mut_array Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Gets a mutable reference to the slice’s underlying array if the specified length `N` matches the buffer's length. ```APIDOC ## as_mut_array ### Description Gets a mutable reference to the slice’s underlying array. If `N` is not exactly equal to the length of `self`, then this method returns `None`. ### Method `as_mut_array(&mut self) -> Option<&mut [T; N]>` ``` -------------------------------- ### Into Implementation Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Rect.html Converts a value into another type that implements `From`. This is the inverse of `From`. ```rust fn into(self) -> U ``` -------------------------------- ### from Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns the argument unchanged. This is part of the `From` trait implementation. ```APIDOC ## fn from(t: T) -> T ### Description Returns the argument unchanged. ### Method `from` ``` -------------------------------- ### to_vec_in Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Copies the buffer into a new Vec with a specified allocator. This is a nightly-only experimental API and requires the `allocator_api` feature. ```APIDOC ## pub fn to_vec_in(&self, alloc: A) -> Vec where A: Allocator, T: Clone, 🔬This is a nightly-only experimental API. (`allocator_api`) Available on **non-`no_global_oom_handling`** only. Copies `self` into a new `Vec` with an allocator. ##### §Examples ``` #![feature(allocator_api)] use std::alloc.System; let s = [10, 40, 30]; let x = s.to_vec_in(System); // Here, `s` and `x` can be modified independently. ``` ``` -------------------------------- ### Instrument Methods Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Rect.html Enables instrumenting code with spans for tracing. Use these to integrate with tracing frameworks. ```rust fn instrument(self, span: Span) -> Instrumented ``` ```rust fn in_current_span(self) -> Instrumented ``` -------------------------------- ### Iterate and Modify Slice Elements Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator that yields mutable references to each element in the slice, allowing modification. Elements are yielded from start to end. ```rust let x = &mut [1, 2, 4]; for elem in x.iter_mut() { *elem += 2; } assert_eq!(x, &[3, 4, 6]); ``` -------------------------------- ### windows Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator over all contiguous windows of a specified size. Windows overlap. Returns no values if the slice is shorter than the window size. Panics if size is zero. ```APIDOC ## windows ### Description Returns an iterator over all contiguous windows of length `size`. The windows overlap. If the slice is shorter than `size`, the iterator returns no values. ### Method `windows(&self, size: usize) -> Windows<'_, T>` ### Panics Panics if `size` is zero. ### Examples ``` let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.windows(3); assert_eq!(iter.next().unwrap(), &['l', 'o', 'r']); assert_eq!(iter.next().unwrap(), &['o', 'r', 'e']); assert_eq!(iter.next().unwrap(), &['r', 'e', 'm']); assert!(iter.next().is_none()); ``` If the slice is shorter than `size`: ``` let slice = ['f', 'o', 'o']; let mut iter = slice.windows(4); assert!(iter.next().is_none()); ``` Because the Iterator trait cannot represent the required lifetimes, there is no `windows_mut` analog to `windows`; `[0,1,2].windows_mut(2).collect()` would violate the rules of references (though a LendingIterator analog is possible). You can sometimes use `Cell::as_slice_of_cells` in conjunction with `windows` instead: ``` use std::cell::Cell; let mut array = ['R', 'u', 's', 't', ' ', '2', '0', '1', '5']; let slice = &mut array[..]; let slice_of_cells: &[Cell] = Cell::from_mut(slice).as_slice_of_cells(); for w in slice_of_cells.windows(3) { Cell::swap(&w[0], &w[2]); } assert_eq!(array, ['s', 't', ' ', '2', '0', '1', '5', 'u', 'R']); ``` ``` -------------------------------- ### fill Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Fills the buffer with a cloned value. ```APIDOC ## fill <value: T> ### Description Fills `self` with elements by cloning `value`. ### Examples ``` let mut buf = vec![0; 10]; buf.fill(1); assert_eq!(buf, vec![1; 10]); ``` ``` -------------------------------- ### Buffer First Mutable Element Example Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Obtains a mutable reference to the first pixel in the buffer, allowing modification. Returns `None` if the buffer is empty. ```rust let x = &mut [0, 1, 2]; if let Some(first) = x.first_mut() { *first = 5; } assert_eq!(x, &[5, 1, 2]); let y: &mut [i32] = &mut []; assert_eq!(None, y.first_mut()); ``` -------------------------------- ### Split Slice at Index Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Demonstrates splitting a slice into two at different indices, including edge cases. ```rust let v = ['a', 'b', 'c']; { let (left, right) = v.split_at(0); assert_eq!(left, []); assert_eq!(right, ['a', 'b', 'c']); } { let (left, right) = v.split_at(2); assert_eq!(left, ['a', 'b']); assert_eq!(right, ['c']); } { let (left, right) = v.split_at(3); assert_eq!(left, ['a', 'b', 'c']); assert_eq!(right, []); } ``` -------------------------------- ### rsplit Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns an iterator over subslices separated by elements that match `pred`, starting at the end of the slice and working backwards. The matched element is not contained in the subslices. ```APIDOC ## rsplit ### Description Returns an iterator over subslices separated by elements that match `pred`, starting at the end of the slice and working backwards. The matched element is not contained in the subslices. ### Method `rsplit(&self, pred: F) -> RSplit<'_, T, F>` ### Parameters - `pred` (FnMut(&T) -> bool) - A closure that returns true if an element should be used as a separator. ### Examples ``` let slice = [11, 22, 33, 0, 44, 55]; let mut iter = slice.rsplit(|num| *num == 0); assert_eq!(iter.next().unwrap(), &[44, 55]); assert_eq!(iter.next().unwrap(), &[11, 22, 33]); assert_eq!(iter.next(), None); ``` As with `split()`, if the first or last element is matched, an empty slice will be the first (or last) item returned by the iterator. ``` let v = &[0, 1, 1, 2, 3, 5, 8]; let mut it = v.rsplit(|n| *n % 2 == 0); assert_eq!(it.next().unwrap(), &[]); assert_eq!(it.next().unwrap(), &[3, 5]); assert_eq!(it.next().unwrap(), &[1, 1]); assert_eq!(it.next().unwrap(), &[]); assert_eq!(it.next(), None); ``` ``` -------------------------------- ### Get Element or Subslice Unsafely Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Use `get_unchecked` for a performance gain when you are certain the index is within bounds. Calling with an out-of-bounds index results in undefined behavior. ```rust let x = &[1, 2, 4]; unsafe { assert_eq!(x.get_unchecked(1), &2); } ``` -------------------------------- ### Clone Implementation for Rect Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Rect.html Provides methods for cloning Rect instances. ```APIDOC ### impl Clone for Rect #### fn clone(&self) -> Rect Returns a duplicate of the value. ``` ```APIDOC #### fn clone_from(&mut self, source: &Self) Performs copy-assignment from `source`. ``` -------------------------------- ### Split Buffer Mutably with Predicate from End Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Splits a mutable buffer into subslices based on a predicate, starting from the end. The matched element is excluded. Limited to n items. ```rust let mut s = [10, 40, 30, 20, 60, 50]; for group in s.rsplitn_mut(2, |num| *num % 3 == 0) { group[0] = 1; } assert_eq!(s, [1, 40, 30, 20, 60, 1]); ``` -------------------------------- ### as_ptr Source: https://docs.rs/softbuffer/latest/softbuffer/struct.Buffer.html Returns a raw pointer to the slice's buffer. ```APIDOC ## pub fn as_ptr(&self) -> *const T ### Description Returns a raw pointer to the slice's buffer. ### Safety The caller must ensure that the slice outlives the pointer this function returns, or else it will end up dangling. The caller must also ensure that the memory the pointer (non-transitively) points to is never written to (except inside an `UnsafeCell`) using this pointer or any pointer derived from it. Modifying the container referenced by this slice may cause its buffer to be reallocated, which would also make any pointers to it invalid. ### Response #### Success Response (*const T) - A raw pointer to the start of the slice's buffer. ### Examples ```rust let x = &[1, 2, 4]; let x_ptr = x.as_ptr(); unsafe { for i in 0..x.len() { assert_eq!(x.get_unchecked(i), &*x_ptr.add(i)); } } ``` ```