container macro code dedup
This commit is contained in:
		
							parent
							
								
									5ecb84ed16
								
							
						
					
					
						commit
						b4730ffdf3
					
				
					 11 changed files with 148 additions and 222 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
use crate::{
 | 
			
		||||
    containers::ByteSlice,
 | 
			
		||||
    macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods},
 | 
			
		||||
    containers::{wrap_grid, ByteSlice},
 | 
			
		||||
    macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
 | 
			
		||||
    mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
| 
						 | 
				
			
			@ -9,8 +9,7 @@ use servicepoint::{
 | 
			
		|||
};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
wrap_clone!(Bitmap);
 | 
			
		||||
wrap_free!(Bitmap);
 | 
			
		||||
wrap_grid!(Bitmap, bool);
 | 
			
		||||
 | 
			
		||||
wrap_functions!(bitmap;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -107,42 +106,6 @@ wrap_functions!(bitmap;
 | 
			
		|||
 | 
			
		||||
wrap_methods!(Bitmap;
 | 
			
		||||
 | 
			
		||||
    /// Gets the current value at the specified position.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
    ///
 | 
			
		||||
    /// - `x` and `y`: position of the cell to read
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Panics
 | 
			
		||||
    ///
 | 
			
		||||
    /// - when accessing `x` or `y` out of bounds
 | 
			
		||||
    ref fn get(x: usize, y: usize) -> bool;
 | 
			
		||||
 | 
			
		||||
    /// Sets the value of the specified position.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
    ///
 | 
			
		||||
    /// - `x` and `y`: position of the cell
 | 
			
		||||
    /// - `value`: the value to write to the cell
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Panics
 | 
			
		||||
    ///
 | 
			
		||||
    /// - when accessing `x` or `y` out of bounds
 | 
			
		||||
    mut fn set(x: usize, y: usize, value: bool);
 | 
			
		||||
 | 
			
		||||
    /// Sets the state of all pixels in the [Bitmap].
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
    ///
 | 
			
		||||
    /// - `value`: the value to set all pixels to
 | 
			
		||||
    mut fn fill(value: bool);
 | 
			
		||||
 | 
			
		||||
    /// Gets the width in pixels.
 | 
			
		||||
    ref fn width() -> usize;
 | 
			
		||||
 | 
			
		||||
    /// Gets the height in pixels.
 | 
			
		||||
    ref fn height() -> usize;
 | 
			
		||||
 | 
			
		||||
    /// Gets an unsafe reference to the data of the [Bitmap] instance.
 | 
			
		||||
    ///
 | 
			
		||||
    /// The returned memory is valid for the lifetime of the bitmap.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
use crate::{
 | 
			
		||||
    containers::ByteSlice,
 | 
			
		||||
    macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods},
 | 
			
		||||
    containers::{wrap_container, ByteSlice},
 | 
			
		||||
    macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
 | 
			
		||||
    mem::{heap_move_nonnull, heap_move_ok, heap_remove},
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
| 
						 | 
				
			
			@ -8,7 +8,9 @@ use servicepoint::{
 | 
			
		|||
};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
wrap_functions!(bitvec;
 | 
			
		||||
wrap_container!(DisplayBitVec);
 | 
			
		||||
 | 
			
		||||
wrap_functions!(associate DisplayBitVec;
 | 
			
		||||
 | 
			
		||||
    /// Creates a new [DisplayBitVec] instance.
 | 
			
		||||
    ///
 | 
			
		||||
| 
						 | 
				
			
			@ -55,9 +57,6 @@ wrap_functions!(bitvec;
 | 
			
		|||
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
wrap_clone!(DisplayBitVec);
 | 
			
		||||
wrap_free!(DisplayBitVec);
 | 
			
		||||
 | 
			
		||||
wrap_methods!(DisplayBitVec;
 | 
			
		||||
 | 
			
		||||
    /// Gets the value of a bit.
 | 
			
		||||
| 
						 | 
				
			
			@ -76,7 +75,6 @@ wrap_methods!(DisplayBitVec;
 | 
			
		|||
        return(result) { result.map(|x| *x).unwrap_or(false) };
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /// Sets the value of a bit.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
use crate::{
 | 
			
		||||
    containers::ByteSlice,
 | 
			
		||||
    macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods},
 | 
			
		||||
    containers::{wrap_grid, ByteSlice},
 | 
			
		||||
    macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
 | 
			
		||||
    mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
| 
						 | 
				
			
			@ -9,7 +9,9 @@ use servicepoint::{
 | 
			
		|||
};
 | 
			
		||||
use std::{mem::transmute, ptr::NonNull};
 | 
			
		||||
 | 
			
		||||
wrap_functions!(brightnessgrid;
 | 
			
		||||
wrap_grid!(BrightnessGrid, Brightness);
 | 
			
		||||
 | 
			
		||||
wrap_functions!(associate BrightnessGrid;
 | 
			
		||||
 | 
			
		||||
    /// Creates a new [BrightnessGrid] with the specified dimensions.
 | 
			
		||||
    ///
 | 
			
		||||
| 
						 | 
				
			
			@ -71,50 +73,7 @@ wrap_functions!(brightnessgrid;
 | 
			
		|||
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
wrap_clone!(BrightnessGrid);
 | 
			
		||||
wrap_free!(BrightnessGrid);
 | 
			
		||||
 | 
			
		||||
wrap_methods!(BrightnessGrid;
 | 
			
		||||
 | 
			
		||||
    /// Gets the current value at the specified position.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
    ///
 | 
			
		||||
    /// - `x` and `y`: position of the cell to read
 | 
			
		||||
    ///
 | 
			
		||||
    /// returns: value at position
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Panics
 | 
			
		||||
    /// - When accessing `x` or `y` out of bounds.
 | 
			
		||||
    ref fn get(x: usize, y: usize) -> Brightness;
 | 
			
		||||
 | 
			
		||||
    /// Sets the value of the specified position.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
    ///
 | 
			
		||||
    /// - `x` and `y`: position of the cell
 | 
			
		||||
    /// - `value`: the value to write to the cell
 | 
			
		||||
    ///
 | 
			
		||||
    /// returns: old value of the cell
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Panics
 | 
			
		||||
    ///
 | 
			
		||||
    /// - When accessing `x` or `y` out of bounds.
 | 
			
		||||
    mut fn set(x: usize, y: usize, value: Brightness);
 | 
			
		||||
 | 
			
		||||
    /// Sets the value of all cells.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
    ///
 | 
			
		||||
    /// - `value`: the value to set all cells to
 | 
			
		||||
    mut fn fill(value: Brightness);
 | 
			
		||||
 | 
			
		||||
    /// Gets the width of the grid.
 | 
			
		||||
    ref fn width() -> usize;
 | 
			
		||||
 | 
			
		||||
    /// Gets the height of the grid.
 | 
			
		||||
    ref fn height() -> usize;
 | 
			
		||||
 | 
			
		||||
    /// Gets an unsafe reference to the data of the instance.
 | 
			
		||||
    ///
 | 
			
		||||
    /// The returned memory is valid for the lifetime of the grid.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,12 +1,15 @@
 | 
			
		|||
use crate::{
 | 
			
		||||
    containers::ByteSlice,
 | 
			
		||||
    macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods},
 | 
			
		||||
    containers::{derive_get_width_height, wrap_container, ByteSlice},
 | 
			
		||||
    macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
 | 
			
		||||
    mem::{heap_move_nonnull, heap_move_ok, heap_remove},
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
wrap_functions!(chargrid;
 | 
			
		||||
wrap_container!(CharGrid);
 | 
			
		||||
derive_get_width_height!(CharGrid);
 | 
			
		||||
 | 
			
		||||
wrap_functions!(associate CharGrid;
 | 
			
		||||
 | 
			
		||||
    /// Creates a new [CharGrid] with the specified dimensions.
 | 
			
		||||
    ///
 | 
			
		||||
| 
						 | 
				
			
			@ -58,9 +61,6 @@ wrap_functions!(chargrid;
 | 
			
		|||
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
wrap_clone!(CharGrid);
 | 
			
		||||
wrap_free!(CharGrid);
 | 
			
		||||
 | 
			
		||||
wrap_methods!(CharGrid;
 | 
			
		||||
 | 
			
		||||
    /// Returns the current value at the specified position.
 | 
			
		||||
| 
						 | 
				
			
			@ -102,10 +102,4 @@ wrap_methods!(CharGrid;
 | 
			
		|||
    mut fn fill(value: u32) {
 | 
			
		||||
        prepare(value) { char::from_u32(value).unwrap() };
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    /// Gets the width of the grid.
 | 
			
		||||
    ref fn width() -> usize;
 | 
			
		||||
 | 
			
		||||
    /// Gets the height of the grid.
 | 
			
		||||
    ref fn height() -> usize;
 | 
			
		||||
);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,6 @@
 | 
			
		|||
use crate::macros::wrap_functions;
 | 
			
		||||
use crate::{
 | 
			
		||||
    containers::ByteSlice,
 | 
			
		||||
    macros::{wrap_clone, wrap_free, wrap_methods},
 | 
			
		||||
    containers::{wrap_grid, ByteSlice},
 | 
			
		||||
    macros::{derive_clone, derive_free, wrap_functions, wrap_methods},
 | 
			
		||||
    mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
 | 
			
		||||
};
 | 
			
		||||
use servicepoint::{
 | 
			
		||||
| 
						 | 
				
			
			@ -9,6 +8,8 @@ use servicepoint::{
 | 
			
		|||
};
 | 
			
		||||
use std::ptr::NonNull;
 | 
			
		||||
 | 
			
		||||
wrap_grid!(Cp437Grid, u8);
 | 
			
		||||
 | 
			
		||||
wrap_functions!(cp437grid;
 | 
			
		||||
 | 
			
		||||
    /// Creates a new [Cp437Grid] with the specified dimensions.
 | 
			
		||||
| 
						 | 
				
			
			@ -50,49 +51,7 @@ wrap_functions!(cp437grid;
 | 
			
		|||
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
wrap_clone!(Cp437Grid);
 | 
			
		||||
wrap_free!(Cp437Grid);
 | 
			
		||||
 | 
			
		||||
wrap_methods!(Cp437Grid;
 | 
			
		||||
    /// Gets the current value at the specified position.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
    ///
 | 
			
		||||
    /// - `x` and `y`: position of the cell to read
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Panics
 | 
			
		||||
    ///
 | 
			
		||||
    /// - when accessing `x` or `y` out of bounds
 | 
			
		||||
    ref fn get(x: usize, y: usize) -> u8;
 | 
			
		||||
 | 
			
		||||
    /// Sets the value at the specified position.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
    ///
 | 
			
		||||
    /// - `x` and `y`: position of the cell
 | 
			
		||||
    /// - `value`: the value to write to the cell
 | 
			
		||||
    ///
 | 
			
		||||
    /// returns: old value of the cell
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Panics
 | 
			
		||||
    ///
 | 
			
		||||
    /// - when accessing `x` or `y` out of bounds
 | 
			
		||||
    mut fn set(x: usize, y: usize, value: u8);
 | 
			
		||||
 | 
			
		||||
    /// Sets the value of all cells in the grid.
 | 
			
		||||
    ///
 | 
			
		||||
    /// # Arguments
 | 
			
		||||
    ///
 | 
			
		||||
    /// - `cp437_grid`: instance to write to
 | 
			
		||||
    /// - `value`: the value to set all cells to
 | 
			
		||||
    mut fn fill(value: u8);
 | 
			
		||||
 | 
			
		||||
    /// Gets the width of the grid.
 | 
			
		||||
    ref fn width() -> usize;
 | 
			
		||||
 | 
			
		||||
    /// Gets the height of the grid.
 | 
			
		||||
    ref fn height() -> usize;
 | 
			
		||||
 | 
			
		||||
    /// Gets an unsafe reference to the data of the grid.
 | 
			
		||||
    ///
 | 
			
		||||
    /// The returned memory is valid for the lifetime of the instance.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,6 +12,65 @@ pub use byte_slice::*;
 | 
			
		|||
pub use char_grid::*;
 | 
			
		||||
pub use cp437_grid::*;
 | 
			
		||||
 | 
			
		||||
macro_rules! wrap_container {
 | 
			
		||||
    ($object_type:ident) => {
 | 
			
		||||
        derive_clone!($object_type);
 | 
			
		||||
        derive_free!($object_type);
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
macro_rules! derive_get_width_height {
 | 
			
		||||
    ($object_type:ident) => {
 | 
			
		||||
        $crate::macros::wrap_methods! {$object_type;
 | 
			
		||||
            /// Gets the width.
 | 
			
		||||
            ref fn width() -> usize;
 | 
			
		||||
 | 
			
		||||
            /// Gets the height.
 | 
			
		||||
            ref fn height() -> usize;
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
macro_rules! wrap_grid {
 | 
			
		||||
    ($object_type:ident, $value_type:ident) => {
 | 
			
		||||
        $crate::containers::wrap_container!($object_type);
 | 
			
		||||
        $crate::containers::derive_get_width_height!($object_type);
 | 
			
		||||
        $crate::macros::wrap_methods! {$object_type;
 | 
			
		||||
            /// Gets the current value at the specified position.
 | 
			
		||||
            ///
 | 
			
		||||
            /// # Arguments
 | 
			
		||||
            ///
 | 
			
		||||
            /// - `x` and `y`: position of the cell to read
 | 
			
		||||
            ///
 | 
			
		||||
            /// # Panics
 | 
			
		||||
            ///
 | 
			
		||||
            /// - when accessing `x` or `y` out of bounds
 | 
			
		||||
            ref fn get(x: usize, y: usize) -> $value_type;
 | 
			
		||||
 | 
			
		||||
            /// Sets the value of the specified position.
 | 
			
		||||
            ///
 | 
			
		||||
            /// # Arguments
 | 
			
		||||
            ///
 | 
			
		||||
            /// - `x` and `y`: position of the cell
 | 
			
		||||
            /// - `value`: the value to write to the cell
 | 
			
		||||
            ///
 | 
			
		||||
            /// # Panics
 | 
			
		||||
            ///
 | 
			
		||||
            /// - when accessing `x` or `y` out of bounds
 | 
			
		||||
            mut fn set(x: usize, y: usize, value: $value_type);
 | 
			
		||||
 | 
			
		||||
            /// Sets the state of all cells in the grid.
 | 
			
		||||
            ///
 | 
			
		||||
            /// # Arguments
 | 
			
		||||
            ///
 | 
			
		||||
            /// - `value`: the value to set all cells to
 | 
			
		||||
            mut fn fill(value: $value_type);
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub(crate) use {derive_get_width_height, wrap_container, wrap_grid};
 | 
			
		||||
 | 
			
		||||
mod _hidden {
 | 
			
		||||
    /// This is a type only used by cbindgen to have a type for pointers.
 | 
			
		||||
    #[allow(unused)]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue