Skip to main content

GridBuf

Struct GridBuf 

Source
pub struct GridBuf<T, B, L> { /* private fields */ }
Available on crate feature buffer only.
Expand description

A 2-dimensional grid implemented by a linear data buffer.

§Layout

The grid is stored in a linear buffer, with elements accessed in an order defined by Layout.

Implementations§

Source§

impl<T, B, L> GridBuf<T, B, L>
where B: AsRef<[T]>, L: LinearLayout,

Source

pub fn from_buffer(buffer: B, width: usize) -> Self

Returns a grid from an existing buffer with a given width in columns.

The height is inferred from the buffer length and width.

Any data type that can be represented as a slice can be used as the buffer type, including arrays, slices, and vectors.

§Panics

This panics if the buffer length is not a multiple of the width.

§Example
use grixy::prelude::*;

let buffer = vec![1, 2, 3, 4, 5, 6];
let grid = GridBuf::<_, _, RowMajor>::from_buffer(buffer, 3);

assert_eq!(grid.get(Pos::new(0, 0)), Some(&1));
assert_eq!(grid.get(Pos::new(1, 0)), Some(&2));
assert_eq!(grid.get(Pos::new(2, 1)), Some(&6));
assert_eq!(grid.get(Pos::new(3, 1)), None); // Out of bounds
Source§

impl<T> GridBuf<T, Vec<T>, RowMajor>

Source

pub fn new(width: usize, height: usize) -> Self
where T: Copy + Default,

Available on crate feature alloc only.

Creates a new grid with the specified width and height, filled with a default value.

This creates a grid with a row-major layout; see new_filled_with_layout to customize.

§Example
use grixy::prelude::*;

let grid = GridBuf::<u8, _, _>::new(3, 3);
assert_eq!(grid.get(Pos::new(0, 0)), Some(&0));
assert_eq!(grid.get(Pos::new(2, 2)), Some(&0));
assert_eq!(grid.get(Pos::new(3, 3)), None); // Out of bounds
Source

pub fn new_filled(width: usize, height: usize, value: T) -> Self
where T: Copy,

Available on crate feature alloc only.

Creates a new grid with the specified width and height, filled with a specified value.

This creates a grid with a row-major layout; see new_filled_with_layout to customize.

§Example
use grixy::prelude::*;

let grid = GridBuf::new_filled(5, 4, 42);

assert_eq!(grid.get(Pos::new(0, 0)), Some(&42));
assert_eq!(grid.get(Pos::new(4, 3)), Some(&42));
assert_eq!(grid.get(Pos::new(5, 3)), None); // Out of bounds
Source§

impl<T, L> GridBuf<T, Vec<T>, L>
where L: LinearLayout,

Source

pub fn new_filled_with_layout(width: usize, height: usize, value: T) -> Self
where T: Copy, L: LinearLayout,

Available on crate feature alloc only.

Creates a new grid with the specified width and height, filled with a default value.

The layout is specified by the type parameter L, allowing for different memory layouts.

§Example
Source§

impl<T, L> GridBuf<T, Vec<T>, L>
where T: Clone + Default, L: LinearLayout,

Source

pub fn resize(&mut self, new_width: usize, new_height: usize)

Available on crate feature alloc only.

Resizes the grid to the new dimensions.

Content in the overlap between the old and new dimensions is preserved. New cells are initialized to T::default().

If both dimensions shrink, excess cells are dropped.

§Examples
use grixy::prelude::*;

let mut grid = GridBuf::new_filled(4, 4, 1u8);
grid[Pos::new(0, 0)] = 99;
grid.resize(6, 6);

assert_eq!(grid.get(Pos::new(0, 0)), Some(&99)); // preserved
assert_eq!(grid.get(Pos::new(5, 5)), Some(&0));  // new, default
Source

pub fn resize_filled(&mut self, new_width: usize, new_height: usize, value: T)

Available on crate feature alloc only.

Resizes the grid, filling new cells with value instead of Default.

§Examples
use grixy::prelude::*;

let mut grid = GridBuf::new_filled(2, 2, 1u8);
grid.resize_filled(4, 4, 42);

assert_eq!(grid.get(Pos::new(0, 0)), Some(&1));  // preserved
assert_eq!(grid.get(Pos::new(3, 3)), Some(&42)); // new, filled with 42
Source§

impl<T, B, L> GridBuf<T, B, L>
where L: LinearLayout,

Source

pub fn into_inner(self) -> (B, usize, usize)

Consumes the GridBuf, returning the underlying buffer, width, and height.

Source

pub fn get_mut(&mut self, pos: Pos) -> Option<&mut T>
where B: AsMut<[T]>, L: LinearLayout,

Returns a mutable reference to the element at pos, or None if out of bounds.

Trait Implementations§

Source§

impl<T, B, L> AsMut<[T]> for GridBuf<T, B, L>
where B: AsMut<[T]>, L: LinearLayout,

Source§

fn as_mut(&mut self) -> &mut [T]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T, B, L> AsRef<[T]> for GridBuf<T, B, L>
where B: AsRef<[T]>, L: LinearLayout,

Source§

fn as_ref(&self) -> &[T]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Clone, B: Clone, L: Clone> Clone for GridBuf<T, B, L>

Source§

fn clone(&self) -> GridBuf<T, B, L>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, B: Debug, L: Debug> Debug for GridBuf<T, B, L>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T, L> Deserialize<'de> for GridBuf<T, Vec<T>, L>
where T: Deserialize<'de> + Default + Copy, L: LinearLayout,

Available on crate feature serde only.
Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T, B, L> Display for GridBuf<T, B, L>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Eq, B: Eq, L: Eq> Eq for GridBuf<T, B, L>

Source§

impl<T, B, L> ExactSizeGrid for GridBuf<T, B, L>
where L: LinearLayout,

Source§

fn width(&self) -> usize

Returns the width of the grid, in columns. Read more
Source§

fn height(&self) -> usize

Returns the height of the grid, in rows. Read more
Source§

fn size(&self) -> Size

Returns the exact size of the grid. Read more
Source§

fn contains(&self, pos: Pos) -> bool

Returns whether the given position is valid for this grid.
Source§

impl<T, B, L> GridBase for GridBuf<T, B, L>
where L: LinearLayout,

Source§

fn size_hint(&self) -> (Size, Option<Size>)

Returns the size of the grid, if known. Read more
Source§

fn trim_rect(&self, rect: Rect) -> Rect

Returns the given rectangle, trimmed to fit within the grid’s size. Read more
Source§

impl<T, B, L> GridReadUnchecked for GridBuf<T, B, L>
where B: AsRef<[T]>, L: LinearLayout,

Source§

type Element<'a> = &'a T where Self: 'a

The type of elements in the grid.
Source§

type Layout = L

The layout of the grid, which determines how elements are stored and accessed.
Source§

unsafe fn get_unchecked(&self, pos: Pos) -> Self::Element<'_>

Returns an element, without doing bounds checking. Read more
Source§

unsafe fn iter_rect_unchecked( &self, bounds: Rect, ) -> impl Iterator<Item = Self::Element<'_>>

Returns an iterator over elements in a rectangular region of the grid. Read more
Source§

impl<T, B, L> GridWriteUnchecked for GridBuf<T, B, L>
where B: AsMut<[T]>, L: LinearLayout,

Source§

type Element = T

The type of elements in the grid.
Source§

type Layout = L

The type of layout used for the grid.
Source§

unsafe fn set_unchecked(&mut self, pos: Pos, value: Self::Element)

Sets the element at a specified position without bounds checking. Read more
Source§

unsafe fn fill_rect_iter_unchecked( &mut self, bounds: Rect, iter: impl IntoIterator<Item = Self::Element>, )

Sets elements within a rectangular region of the grid without bounds checking. Read more
Source§

unsafe fn fill_rect_solid_unchecked( &mut self, bounds: Rect, value: Self::Element, )
where Self::Element: Copy,

Fills a rectangular region of the grid with a single value without bounds checking. Read more
Source§

unsafe fn fill_rect_unchecked( &mut self, dst: Rect, f: impl FnMut(Pos) -> Self::Element, )

Sets elements within a rectangular region of the grid without bounds checking. Read more
Source§

impl<T, B, L> Index<Pos<usize>> for GridBuf<T, B, L>
where L: LinearLayout, B: AsRef<[T]>,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: Pos) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, B, L> IndexMut<Pos<usize>> for GridBuf<T, B, L>
where L: LinearLayout, B: AsRef<[T]> + AsMut<[T]>,

Source§

fn index_mut(&mut self, index: Pos) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T: PartialEq, B: PartialEq, L: PartialEq> PartialEq for GridBuf<T, B, L>

Source§

fn eq(&self, other: &GridBuf<T, B, L>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T, B, L> Serialize for GridBuf<T, B, L>
where T: Serialize, B: AsRef<[T]>, L: LinearLayout,

Available on crate feature serde only.
Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: PartialEq, B: PartialEq, L: PartialEq> StructuralPartialEq for GridBuf<T, B, L>

Source§

impl<T, B, L> TrustedSizeGrid for GridBuf<T, B, L>
where L: LinearLayout,

Auto Trait Implementations§

§

impl<T, B, L> Freeze for GridBuf<T, B, L>
where B: Freeze,

§

impl<T, B, L> RefUnwindSafe for GridBuf<T, B, L>

§

impl<T, B, L> Send for GridBuf<T, B, L>
where B: Send, L: Send, T: Send,

§

impl<T, B, L> Sync for GridBuf<T, B, L>
where B: Sync, L: Sync, T: Sync,

§

impl<T, B, L> Unpin for GridBuf<T, B, L>
where B: Unpin, L: Unpin, T: Unpin,

§

impl<T, B, L> UnsafeUnpin for GridBuf<T, B, L>
where B: UnsafeUnpin,

§

impl<T, B, L> UnwindSafe for GridBuf<T, B, L>
where B: UnwindSafe, L: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> GridConvertExt for T
where T: GridRead,

Source§

fn copied<'a, T>(self) -> Copied<T, Self>
where Self: Sized + GridRead<Element<'a> = &'a T> + 'a, T: Copy + 'a,

Creates a grid that copies all of its elements. Read more
Source§

fn map<F, T>(self, map_fn: F) -> Mapped<F, Self, T>
where Self: Sized, F: Fn(Self::Element<'_>) -> T,

Creates a grid that applies a mapping function to its elements. Read more
Source§

fn view(self, bounds: Rect) -> Viewed<Self>
where Self: Sized,

Creates a view of the grid over a specified rectangular region. Read more
Source§

fn scale(self, factor: usize) -> Scaled<Self>
where Self: Sized,

Creates a scaled version of the grid. Read more
Source§

fn flatten<'a, B, L>(&'a self) -> GridBuf<Self::Element<'a>, B, L>
where B: FromIterator<Self::Element<'a>> + AsRef<[Self::Element<'a>]>, L: LinearLayout, Self: Sized + ExactSizeGrid, Self::Element<'a>: Copy,

Available on crate feature buffer only.
Collects the elements of the grid into a new buffer. Read more
Source§

fn blend<F>(&mut self, blend_fn: F) -> Blended<'_, Self, F>
where Self: Sized + GridWrite, F: Fn(<Self as GridRead>::Element<'_>, <Self as GridWrite>::Element) -> <Self as GridWrite>::Element,

Creates a blended version of this grid, applying a blend function when setting elements. Read more
Source§

impl<G> GridDiff for G

Source§

fn diff<'a>( &'a self, other: &'a G, ) -> impl Iterator<Item = (Pos<usize>, <G as GridRead>::Element<'a>)> + 'a
where <G as GridRead>::Element<'a>: PartialEq,

Returns an iterator over positions where self differs from other. Read more
Source§

impl<T> GridIter for T

Source§

fn iter(&self) -> impl Iterator<Item = <T as GridRead>::Element<'_>>

Returns an iterator over the elements of the grid.
Source§

fn iter_with_pos( &self, ) -> impl Iterator<Item = (Pos<usize>, <T as GridRead>::Element<'_>)>

Returns an iterator over (position, element) pairs for the entire grid. Read more
Source§

fn cells(&self) -> impl Iterator<Item = (Pos, Self::Element<'_>)>

Source§

impl<T> GridRead for T

Source§

type Element<'a> = <T as GridReadUnchecked>::Element<'a> where T: 'a

The type of elements in the grid.
Source§

type Layout = <T as GridReadUnchecked>::Layout

The type of layout used for the grid. Read more
Source§

fn get(&self, pos: Pos<usize>) -> Option<<T as GridRead>::Element<'_>>

Returns a reference to an element at a specified position. Read more
Source§

fn iter_rect( &self, bounds: Rect<usize>, ) -> impl Iterator<Item = <T as GridRead>::Element<'_>>

Returns an iterator over elements in a rectangular region of the grid. Read more
Source§

fn iter_rect_with_pos( &self, bounds: Rect, ) -> impl Iterator<Item = (Pos, Self::Element<'_>)>

Returns an iterator over (position, element) pairs in a rectangular region. Read more
Source§

impl<T> GridWrite for T

Source§

type Element = <T as GridWriteUnchecked>::Element

The type of elements in the grid.
Source§

type Layout = <T as GridWriteUnchecked>::Layout

The type of layout used for the grid. Read more
Source§

fn set( &mut self, pos: Pos<usize>, value: <T as GridWrite>::Element, ) -> Result<(), GridError>

Sets the element at a specified position. Read more
Source§

fn fill_rect( &mut self, bounds: Rect<usize>, f: impl FnMut(Pos<usize>) -> <T as GridWrite>::Element, )

Sets elements within a rectangular region of the grid. Read more
Source§

fn fill_rect_iter( &mut self, dst: Rect<usize>, iter: impl IntoIterator<Item = <T as GridWrite>::Element>, )

Sets elements within a rectangular region of the grid. Read more
Source§

fn fill_rect_solid( &mut self, dst: Rect<usize>, value: <T as GridWrite>::Element, )
where <T as GridWrite>::Element: Copy,

Sets elements within a rectangular region of the grid. Read more
Source§

fn clear(&mut self)
where Self::Element: Default, Self: ExactSizeGrid,

Clears the grid, setting all elements to their default value. Read more
Source§

fn fill(&mut self, f: impl FnMut(Pos) -> Self::Element)
where Self: ExactSizeGrid,

Sets elements within the grid. Read more
Source§

fn fill_iter(&mut self, iter: impl Iterator<Item = Self::Element>)
where Self: ExactSizeGrid,

Sets elements within the grid from an iterator. Read more
Source§

fn fill_solid(&mut self, value: Self::Element)
where Self::Element: Copy, Self: ExactSizeGrid,

Sets elements within the grid to a single value. Read more
Source§

fn clear_rect(&mut self, bounds: Rect)
where Self::Element: Default,

Clears a rectangular region of the grid, setting all elements to their default value. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.