implement return modifiers

This commit is contained in:
Vinzenz Schroeter 2025-06-26 18:59:46 +02:00
parent 0968605d0b
commit 39c7c27c86
18 changed files with 144 additions and 146 deletions

View file

@ -1,7 +1,6 @@
use crate::{
containers::{wrap_grid, ByteSlice},
macros::{wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some},
};
use servicepoint::{
Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid,
@ -35,15 +34,15 @@ wrap_functions!(associate Bitmap;
/// sp_bitmap_set(grid, 0, 0, false);
/// sp_bitmap_free(grid);
/// ```
fn new(width: val usize, height: val usize) -> *mut Bitmap {
heap_move_some(Bitmap::new(width, height))
fn new(width: val usize, height: val usize) -> move_some *mut Bitmap {
Bitmap::new(width, height)
};
/// Creates a new [Bitmap] with a size matching the screen.
///
/// returns: [Bitmap] initialized to all pixels off.
fn new_max_sized() -> NonNull<Bitmap> {
heap_move_nonnull(Bitmap::max_sized())
fn new_max_sized() -> move NonNull<Bitmap> {
Bitmap::max_sized()
};
/// Loads a [Bitmap] with the specified dimensions from the provided data.
@ -58,9 +57,9 @@ wrap_functions!(associate Bitmap;
width: val usize,
height: val usize,
data: val ByteSlice,
) -> *mut Bitmap {
) -> move_ok *mut Bitmap {
let data = unsafe { data.as_slice() };
heap_move_ok(Bitmap::load(width, height, data))
Bitmap::load(width, height, data)
};
/// Tries to convert the BitVec to a Bitmap.
@ -71,15 +70,15 @@ wrap_functions!(associate Bitmap;
fn from_bitvec(
width: val usize,
bitvec: move NonNull<DisplayBitVec>,
) -> *mut Bitmap {
heap_move_ok(Bitmap::from_bitvec(width, bitvec))
) -> move_ok *mut Bitmap {
Bitmap::from_bitvec(width, bitvec)
};
);
wrap_methods!(Bitmap;
/// Consumes the Bitmap and returns the contained BitVec.
fn into_bitvec(move bitmap) -> NonNull<DisplayBitVec> {
heap_move_nonnull(bitmap.into())
fn into_bitvec(move bitmap) -> move NonNull<DisplayBitVec> {
bitmap.into()
};
/// Creates a [BitmapCommand] and immediately turns that into a [Packet].
@ -87,18 +86,18 @@ wrap_methods!(Bitmap;
/// The provided [Bitmap] gets consumed.
///
/// Returns NULL in case of an error.
fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> *mut Packet {
heap_move_ok(Packet::try_from(BitmapCommand {
fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> move_ok *mut Packet {
Packet::try_from(BitmapCommand {
bitmap,
origin: Origin::new(x, y),
compression,
}))
})
};
/// Gets an unsafe reference to the data of the [Bitmap] instance.
///
/// The returned memory is valid for the lifetime of the bitmap.
fn data_ref_mut(mut instance) -> ByteSlice {
fn data_ref_mut(mut instance) -> val ByteSlice {
unsafe { ByteSlice::from_slice(instance.data_ref_mut()) }
};
);

View file

@ -1,7 +1,6 @@
use crate::{
containers::{wrap_container, ByteSlice},
macros::{wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok},
};
use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet,
@ -22,16 +21,16 @@ wrap_functions!(associate DisplayBitVec;
/// # Panics
///
/// - when `size` is not divisible by 8.
fn new(size: val usize) -> NonNull<DisplayBitVec> {
heap_move_nonnull(DisplayBitVec::repeat(false, size))
fn new(size: val usize) -> move NonNull<DisplayBitVec> {
DisplayBitVec::repeat(false, size)
};
/// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance.
///
/// returns: [DisplayBitVec] instance containing data.
fn load(data: val ByteSlice) -> NonNull<DisplayBitVec> {
fn load(data: val ByteSlice) -> move NonNull<DisplayBitVec> {
let data = unsafe { data.as_slice() };
heap_move_nonnull(DisplayBitVec::from_slice(data))
DisplayBitVec::from_slice(data)
};
);
@ -46,13 +45,13 @@ wrap_methods!(DisplayBitVec;
offset: val usize,
operation: val BinaryOperation,
compression: val CompressionCode
) -> *mut Packet {
heap_move_ok(Packet::try_from(BitVecCommand {
) -> move_ok *mut Packet {
Packet::try_from(BitVecCommand {
bitvec,
offset,
operation,
compression,
}))
})
};
/// Gets the value of a bit.
@ -67,7 +66,7 @@ wrap_methods!(DisplayBitVec;
/// # Panics
///
/// - when accessing `index` out of bounds
fn get(ref instance, index: val usize) -> bool {
fn get(ref instance, index: val usize) -> val bool {
instance.get(index).map(|x| *x).unwrap_or(false)
};
@ -91,15 +90,15 @@ wrap_methods!(DisplayBitVec;
fn fill(mut instance, value: val bool);
/// Gets the length in bits.
fn len(ref instance) -> usize;
fn len(ref instance) -> val usize;
/// Returns true if length is 0.
fn is_empty(ref instance) -> bool;
fn is_empty(ref instance) -> val bool;
/// Gets an unsafe reference to the data of the [DisplayBitVec] instance.
///
/// The returned memory is valid for the lifetime of the bitvec.
fn as_raw_mut_slice(mut instance) -> ByteSlice {
fn as_raw_mut_slice(mut instance) -> val ByteSlice {
unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) }
};
);

View file

@ -1,7 +1,6 @@
use crate::{
containers::{wrap_grid, ByteSlice},
macros::{wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some},
};
use servicepoint::{
Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid,
@ -12,7 +11,6 @@ use std::{mem::transmute, ptr::NonNull};
wrap_grid!(BrightnessGrid, Brightness);
wrap_functions!(associate BrightnessGrid;
/// Creates a new [BrightnessGrid] with the specified dimensions.
///
/// returns: [BrightnessGrid] initialized to 0.
@ -30,8 +28,8 @@ wrap_functions!(associate BrightnessGrid;
/// TypedCommand *command = sp_command_char_brightness(grid);
/// sp_udp_free(connection);
/// ```
fn new(width: val usize, height: val usize) -> NonNull<BrightnessGrid> {
heap_move_nonnull(BrightnessGrid::new(width, height))
fn new(width: val usize, height: val usize) -> move NonNull<BrightnessGrid> {
BrightnessGrid::new(width, height)
};
/// Loads a [BrightnessGrid] with the specified dimensions from the provided data.
@ -43,14 +41,11 @@ wrap_functions!(associate BrightnessGrid;
width: val usize,
height: val usize,
data: val ByteSlice,
) -> *mut BrightnessGrid {
) -> move_some *mut BrightnessGrid {
let data = unsafe { data.as_slice() };
heap_move_some(
ByteGrid::load(width, height, data)
.map(move |grid| grid.map(Brightness::saturating_from)),
)
ByteGrid::load(width, height, data)
.map(move |grid| grid.map(Brightness::saturating_from))
};
);
wrap_methods!(BrightnessGrid;
@ -59,17 +54,17 @@ wrap_methods!(BrightnessGrid;
/// The provided [BrightnessGrid] gets consumed.
///
/// Returns NULL in case of an error.
fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet {
heap_move_ok(Packet::try_from(BrightnessGridCommand {
fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet {
Packet::try_from(BrightnessGridCommand {
grid,
origin: Origin::new(x, y),
}))
})
};
/// Gets an unsafe reference to the data of the instance.
///
/// The returned memory is valid for the lifetime of the grid.
fn data_ref_mut(mut instance) -> ByteSlice {
fn data_ref_mut(mut instance) -> val ByteSlice {
//noinspection RsAssertEqual
const _: () = assert!(size_of::<Brightness>() == 1);

View file

@ -1,7 +1,6 @@
use crate::{
containers::{derive_get_width_height, wrap_container, ByteSlice},
macros::{wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok},
};
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
use std::ptr::NonNull;
@ -22,16 +21,16 @@ wrap_functions!(associate CharGrid;
/// sp_char_grid_set(grid, 0, 0, '!');
/// sp_char_grid_free(grid);
/// ```
fn new(width: val usize, height: val usize) -> NonNull<CharGrid> {
heap_move_nonnull(CharGrid::new(width, height))
fn new(width: val usize, height: val usize) -> move NonNull<CharGrid> {
CharGrid::new(width, height)
};
/// Loads a [CharGrid] with the specified dimensions from the provided data.
///
/// returns: new CharGrid or NULL in case of an error
fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut CharGrid {
fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_ok *mut CharGrid {
let data = unsafe { data.as_slice() };
heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec()))
CharGrid::load_utf8(width, height, data.to_vec())
};
);
@ -46,7 +45,7 @@ wrap_methods!(CharGrid;
/// # Panics
///
/// - when accessing `x` or `y` out of bounds
fn get(ref instance, x: val usize, y: val usize) -> u32 {
fn get(ref instance, x: val usize, y: val usize) -> val u32 {
instance.get(x, y) as u32
};
@ -82,10 +81,10 @@ wrap_methods!(CharGrid;
/// The provided [CharGrid] gets consumed.
///
/// Returns NULL in case of an error.
fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet {
heap_move_ok(Packet::try_from(CharGridCommand {
fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet {
Packet::try_from(CharGridCommand {
grid,
origin: Origin::new(x, y),
}))
})
};
);

View file

@ -1,7 +1,6 @@
use crate::{
containers::{wrap_grid, ByteSlice},
macros::{wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some},
};
use servicepoint::{
Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet,
@ -14,14 +13,14 @@ wrap_functions!(associate Cp437Grid;
/// Creates a new [Cp437Grid] with the specified dimensions.
///
/// returns: [Cp437Grid] initialized to 0.
fn new(width: val usize, height: val usize) -> NonNull<Cp437Grid> {
heap_move_nonnull(Cp437Grid::new(width, height))
fn new(width: val usize, height: val usize) -> move NonNull<Cp437Grid> {
Cp437Grid::new(width, height)
};
/// Loads a [Cp437Grid] with the specified dimensions from the provided data.
fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut Cp437Grid {
fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_some *mut Cp437Grid {
let data = unsafe { data.as_slice() };
heap_move_some(Cp437Grid::load(width, height, data))
Cp437Grid::load(width, height, data)
};
);
@ -31,17 +30,17 @@ wrap_methods!(Cp437Grid;
/// The provided [Cp437Grid] gets consumed.
///
/// Returns NULL in case of an error.
fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet {
heap_move_ok(Packet::try_from(Cp437GridCommand {
fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet {
Packet::try_from(Cp437GridCommand {
grid,
origin: Origin::new(x, y),
}))
})
};
/// Gets an unsafe reference to the data of the grid.
///
/// The returned memory is valid for the lifetime of the instance.
fn data_ref_mut(mut instance) -> ByteSlice {
fn data_ref_mut(mut instance) -> val ByteSlice {
unsafe { ByteSlice::from_slice(instance.data_ref_mut()) }
};
);

View file

@ -23,10 +23,10 @@ macro_rules! derive_get_width_height {
($object_type:ident) => {
$crate::macros::wrap_methods! {$object_type;
/// Gets the width.
fn width(ref instance) -> usize;
fn width(ref instance) -> val usize;
/// Gets the height.
fn height(ref instance) -> usize;
fn height(ref instance) -> val usize;
}
};
}
@ -45,7 +45,7 @@ macro_rules! wrap_grid {
/// # Panics
///
/// - when accessing `x` or `y` out of bounds
fn get(ref instance, x: val usize, y: val usize) -> $value_type;
fn get(ref instance, x: val usize, y: val usize) -> val $value_type;
/// Sets the value of the specified position.
///