pub struct Buf2<T>(/* private fields */);Expand description
A rectangular 2D buffer that owns its elements, backed by a Vec.
Unlike Vec, however, Buf2 cannot be resized after construction
without explicitly copying the contents to a new, larger buffer.
Buf2 stores its elements contiguously, in standard row-major order,
such that the coordinate pair (x, y) maps to the index
buf.width() * y + xin the backing vector.
§Examples
// Elements initialized with `Default::default()`
let mut buf = Buf2::new((4, 4));
// Indexing with a 2D vector (x, y) yields element at row y, column x:
buf[vec2(2, 1)] = 123;
// Indexing with an usize i yields row with index i as a slice:
assert_eq!(buf[1usize], [0, 0, 123, 0]);
// Thus you can also do this, row first, column second:
assert_eq!(buf[1usize][2], 123)Implementations§
Source§impl<T> Buf2<T>
impl<T> Buf2<T>
Sourcepub fn new_from<I>(dims: Dims, init: I) -> Selfwhere
I: IntoIterator<Item = T>,
pub fn new_from<I>(dims: Dims, init: I) -> Selfwhere
I: IntoIterator<Item = T>,
Returns a buffer with the given dimensions, with elements initialized
in row-major order with values yielded by init.
§Panics
If there are fewer than w * h elements in init.
Sourcepub fn new(dims: Dims) -> Self
pub fn new(dims: Dims) -> Self
Returns a buffer with size w × h, with every element
initialized by calling T::default().
Methods from Deref<Target = Inner<T, Vec<T>>>§
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Returns whether the rows of self are stored as one contiguous
slice, without gaps between rows.
Buf2 instances are always contiguous. A Slice2 or MutSlice2
instance is contiguous if its width equals its stride, if its
height is 1, or if it is empty.
Sourcepub fn slice(&self, rect: impl Into<Rect>) -> Slice2<'_, T>
pub fn slice(&self, rect: impl Into<Rect>) -> Slice2<'_, T>
Returns a borrowed rectangular slice of self.
§Panics
If any part of rect is outside the bounds of self.
Sourcepub fn get(&self, pos: impl Into<Vec2u>) -> Option<&T>
pub fn get(&self, pos: impl Into<Vec2u>) -> Option<&T>
Returns a reference to the element at pos,
or None if pos is out of bounds.
Sourcepub fn rows(&self) -> impl Iterator<Item = &[T]>
pub fn rows(&self) -> impl Iterator<Item = &[T]>
Returns an iterator over the rows of self as &[T] slices.
The length of each slice equals self.width().
Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
Returns an iterator over all the elements of self in row-major order.
First returns the elements on row 0 from left to right, followed by the elements on row 1, and so on.
Sourcepub fn as_mut_slice2(&mut self) -> MutSlice2<'_, T>
pub fn as_mut_slice2(&mut self) -> MutSlice2<'_, T>
Returns a mutably borrowed rectangular slice of self.
Sourcepub fn rows_mut(&mut self) -> impl Iterator<Item = &mut [T]>
pub fn rows_mut(&mut self) -> impl Iterator<Item = &mut [T]>
Returns an iterator over the rows of this buffer as &mut [T].
The length of each slice equals self.width().
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
Returns an iterator over all the elements of self in row-major
order: first the elements on row 0 from left to right, followed
by the elements on row 1, and so on.
Sourcepub fn fill_with<F>(&mut self, fill_fn: F)
pub fn fill_with<F>(&mut self, fill_fn: F)
Fills self by invoking f(x, y) for every element, where
x and y are the column and row of the element, respectively.
Sourcepub fn copy_from(&mut self, src: impl AsSlice2<T>)where
T: Copy,
pub fn copy_from(&mut self, src: impl AsSlice2<T>)where
T: Copy,
Copies each element in src to the same position in self.
This operation is often called “blitting”.
§Panics
if the dimensions of self and src don’t match.
Trait Implementations§
Source§impl<T> AsMutSlice2<T> for &mut Buf2<T>
impl<T> AsMutSlice2<T> for &mut Buf2<T>
Source§fn as_mut_slice2(&mut self) -> MutSlice2<'_, T>
fn as_mut_slice2(&mut self) -> MutSlice2<'_, T>
MutSlice2 view of Self.Source§impl<T> AsMutSlice2<T> for Buf2<T>
impl<T> AsMutSlice2<T> for Buf2<T>
Source§fn as_mut_slice2(&mut self) -> MutSlice2<'_, T>
fn as_mut_slice2(&mut self) -> MutSlice2<'_, T>
MutSlice2 view of Self.Auto Trait Implementations§
impl<T> Freeze for Buf2<T>
impl<T> RefUnwindSafe for Buf2<T>where
T: RefUnwindSafe,
impl<T> Send for Buf2<T>where
T: Send,
impl<T> Sync for Buf2<T>where
T: Sync,
impl<T> Unpin for Buf2<T>where
T: Unpin,
impl<T> UnwindSafe for Buf2<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Buf> Target for Bufwhere
Buf: AsMutSlice2<u32>,
impl<Buf> Target for Bufwhere
Buf: AsMutSlice2<u32>,
Source§fn rasterize<V, Fs>(
&mut self,
sl: Scanline<V>,
fs: &Fs,
ctx: &Context,
) -> Throughputwhere
V: Vary,
Fs: FragmentShader<V>,
fn rasterize<V, Fs>(
&mut self,
sl: Scanline<V>,
fs: &Fs,
ctx: &Context,
) -> Throughputwhere
V: Vary,
Fs: FragmentShader<V>,
Rasterizes scanline into this u32 color buffer.
Does no z-buffering.