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::{
commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions},
mem::heap_move_nonnull,
};
use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin};
use std::ptr::NonNull;
@ -26,19 +25,19 @@ wrap_functions!(associate BitmapCommand;
origin_x: val usize,
origin_y: val usize,
compression: val CompressionCode,
) -> NonNull<BitmapCommand> {
heap_move_nonnull(BitmapCommand {
) -> move NonNull<BitmapCommand> {
BitmapCommand {
bitmap,
origin: Origin::new(origin_x, origin_y),
compression,
})
}
};
/// Move the provided [Bitmap] into a new [BitmapCommand],
/// leaving other fields as their default values.
///
/// Rust equivalent: `BitmapCommand::from(bitmap)`
fn from_bitmap(bitmap: move NonNull<Bitmap>) -> NonNull<BitmapCommand> {
heap_move_nonnull(bitmap.into())
fn from_bitmap(bitmap: move NonNull<Bitmap>) -> move NonNull<BitmapCommand> {
bitmap.into()
};
);

View file

@ -1,7 +1,6 @@
use crate::{
commands::wrap_command,
macros::{wrap_fields, wrap_functions},
mem::heap_move_nonnull,
};
use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset,
@ -35,12 +34,12 @@ wrap_functions!(associate BitVecCommand;
offset: val usize,
operation: val BinaryOperation,
compression: val CompressionCode,
) -> NonNull<BitVecCommand> {
heap_move_nonnull(BitVecCommand {
) -> move NonNull<BitVecCommand> {
BitVecCommand {
bitvec,
offset,
operation,
compression,
})
}
};
);

View file

@ -1,7 +1,6 @@
use crate::{
commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions},
mem::heap_move_nonnull,
};
use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin};
use std::ptr::NonNull;
@ -24,16 +23,16 @@ wrap_functions!(associate BrightnessGridCommand;
grid: move NonNull<BrightnessGrid>,
origin_x: val usize,
origin_y: val usize
) -> NonNull<BrightnessGridCommand> {
heap_move_nonnull(BrightnessGridCommand {
) -> move NonNull<BrightnessGridCommand> {
BrightnessGridCommand {
grid,
origin: Origin::new(origin_x, origin_y),
})
}
};
/// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand],
/// leaving other fields as their default values.
fn from_grid(grid: move NonNull<BrightnessGrid>) -> NonNull<BrightnessGridCommand> {
heap_move_nonnull(grid.into())
fn from_grid(grid: move NonNull<BrightnessGrid>) -> move NonNull<BrightnessGridCommand> {
grid.into()
};
);

View file

@ -1,6 +1,4 @@
use crate::{
commands::wrap_command, macros::wrap_functions, mem::heap_move_nonnull,
};
use crate::{commands::wrap_command, macros::wrap_functions};
use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand};
macro_rules! wrap_cc_only {
@ -12,8 +10,8 @@ macro_rules! wrap_cc_only {
$(#[$meta])*
///
#[doc = " Returns: a new [`" [< $command Command >] "`] instance."]
fn new() -> ::core::ptr::NonNull<[< $command Command >]> {
heap_move_nonnull([< $command Command >])
fn new() -> move ::core::ptr::NonNull<[< $command Command >]> {
[< $command Command >]
};
);
}

View file

@ -1,7 +1,6 @@
use crate::{
commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions},
mem::heap_move_nonnull,
};
use servicepoint::{CharGrid, CharGridCommand, Origin};
use std::ptr::NonNull;
@ -24,16 +23,16 @@ wrap_functions!(associate CharGridCommand;
grid: move NonNull<CharGrid>,
origin_x: val usize,
origin_y: val usize,
) -> NonNull<CharGridCommand> {
heap_move_nonnull(CharGridCommand {
) -> move NonNull<CharGridCommand> {
CharGridCommand {
grid,
origin: Origin::new(origin_x, origin_y),
})
}
};
/// Moves the provided [CharGrid] into a new [CharGridCommand],
/// leaving other fields as their default values.
fn from_grid(grid: move NonNull<CharGrid>) -> NonNull<CharGridCommand> {
heap_move_nonnull(grid.into())
fn from_grid(grid: move NonNull<CharGrid>) -> move NonNull<CharGridCommand> {
grid.into()
};
);

View file

@ -1,7 +1,6 @@
use crate::{
commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions},
mem::heap_move_nonnull,
};
use servicepoint::{Cp437Grid, Cp437GridCommand, Origin};
use std::ptr::NonNull;
@ -24,16 +23,16 @@ wrap_functions!(associate Cp437GridCommand;
grid: move NonNull<Cp437Grid>,
origin_x: val usize,
origin_y: val usize,
) -> NonNull<Cp437GridCommand> {
heap_move_nonnull(Cp437GridCommand {
) -> move NonNull<Cp437GridCommand> {
Cp437GridCommand {
grid,
origin: Origin::new(origin_x, origin_y),
})
}
};
/// Moves the provided [Cp437Grid] into a new [Cp437GridCommand],
/// leaving other fields as their default values.
fn from_grid(grid: move NonNull<Cp437Grid>) -> NonNull<Cp437GridCommand> {
heap_move_nonnull(grid.into())
fn from_grid(grid: move NonNull<Cp437Grid>) -> move NonNull<Cp437GridCommand> {
grid.into()
};
);

View file

@ -178,8 +178,8 @@ wrap_functions!(associate GenericCommand;
/// The packet is dropped in the process.
///
/// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed.
fn try_from_packet(packet: move NonNull<Packet>) -> NonNull<GenericCommand> {
let result = servicepoint::TypedCommand::try_from(packet)
fn try_from_packet(packet: move NonNull<Packet>) -> move NonNull<GenericCommand> {
servicepoint::TypedCommand::try_from(packet)
.map(|value| match value {
TypedCommand::Clear(clear) => GenericCommand {
tag: CommandTag::Clear,
@ -246,8 +246,7 @@ wrap_functions!(associate GenericCommand;
.unwrap_or_else(move |_| GenericCommand {
tag: CommandTag::Invalid,
data: CommandUnion { null: null_mut() },
});
heap_move_nonnull(result)
})
};
);
@ -256,7 +255,7 @@ wrap_methods! { GenericCommand;
/// The [GenericCommand] gets consumed.
///
/// Returns tag [CommandTag::Invalid] in case of an error.
fn try_into_packet(move command) -> *mut Packet {
fn try_into_packet(move command) -> val *mut Packet {
match command.tag {
CommandTag::Invalid => null_mut(),
CommandTag::Bitmap => {

View file

@ -1,7 +1,6 @@
use crate::{
commands::wrap_command,
macros::{wrap_fields, wrap_functions},
mem::heap_move_nonnull,
};
use servicepoint::{Brightness, GlobalBrightnessCommand};
use std::ptr::NonNull;
@ -10,8 +9,8 @@ wrap_functions!(associate GlobalBrightnessCommand;
/// Set the brightness of all tiles to the same value.
///
/// Returns: a new [GlobalBrightnessCommand] instance.
fn new(brightness: val Brightness) -> NonNull<GlobalBrightnessCommand> {
heap_move_nonnull(GlobalBrightnessCommand::from(brightness))
fn new(brightness: val Brightness) -> move NonNull<GlobalBrightnessCommand> {
GlobalBrightnessCommand::from(brightness)
};
);

View file

@ -62,8 +62,8 @@ macro_rules! derive_command_into_packet {
#[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."]
///
/// Returns: NULL or a [Packet] containing the command.
fn try_into_packet(move instance) -> *mut ::servicepoint::Packet {
$crate::mem::heap_move_ok(instance.try_into())
fn try_into_packet(move instance) -> move_ok *mut ::servicepoint::Packet {
instance.try_into()
};
);
}

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)),
)
.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.
///

View file

@ -17,8 +17,8 @@ macro_rules! derive_clone {
::paste::paste! {
$crate::macros::wrap_method!($object_type;
#[doc = "Clones a [`" $object_type "`] instance."]
fn clone(ref instance) -> ::core::ptr::NonNull<$object_type> {
$crate::mem::heap_move_nonnull(instance.clone())
fn clone(ref instance) -> move ::core::ptr::NonNull<$object_type> {
instance.clone()
};
);
}
@ -30,14 +30,14 @@ macro_rules! wrap_method {
$object_type:ident;
$(#[$meta:meta])+
fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?)
$(-> $return_type:ty)?;
$(-> $return_modifier:ident $return_type:ty)?;
) => {
::paste::paste!{
$crate::macros::wrap_method!(
$object_type;
$(#[$meta])+
fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?)
$(-> $return_type)? {
$(-> $return_modifier $return_type)? {
$instance.$function($($($param_name),*)?)
};
);
@ -46,7 +46,7 @@ macro_rules! wrap_method {
($object_type:ident;
$(#[$meta:meta])+
fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?)
$(-> $return_type:ty)?
$(-> $return_modifier:ident $return_type:ty)?
$impl:block;
) => {
paste::paste! {
@ -57,7 +57,7 @@ macro_rules! wrap_method {
fn $function(
$instance: $ref_or_mut ::core::ptr::NonNull<$object_type>
$(,$($param_name: $param_modifier $param_type),*)?
) $(-> $return_type)?
) $(-> $return_modifier $return_type)?
$impl;
);
}
@ -70,7 +70,7 @@ macro_rules! wrap_methods {
$(
$(#[$meta:meta])+
fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?)
$(-> $return_type:ty)?
$(-> $return_modifier:ident $return_type:ty)?
$($impl:block)?;
)+
) => {
@ -79,7 +79,7 @@ macro_rules! wrap_methods {
$crate::macros::wrap_method!($object_type;
$(#[$meta])*
fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?)
$(-> $return_type)?
$(-> $return_modifier $return_type)?
$($impl)?;
);
)+
@ -93,8 +93,8 @@ macro_rules! wrap_fields_accessor {
$crate::macros::wrap_method! {$object_type;
#[doc = " Gets the value of field `" $prop_name
"` of the [`servicepoint::" $object_type "`]."]
fn [<get _ $prop_name>](ref instance) -> $prop_type {
return instance.$prop_name;
fn [<get _ $prop_name>](ref instance) -> val $prop_type {
instance.$prop_name
};
}
}
@ -107,8 +107,8 @@ macro_rules! wrap_fields_accessor {
///
/// - The returned reference inherits the lifetime of object in which it is contained.
/// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command.
fn [<get _ $prop_name _mut>](mut instance) -> ::core::ptr::NonNull<$prop_type> {
return ::core::ptr::NonNull::from(&mut instance.$prop_name);
fn [<get _ $prop_name _mut>](mut instance) -> val ::core::ptr::NonNull<$prop_type> {
::core::ptr::NonNull::from(&mut instance.$prop_name)
};
}
}
@ -171,16 +171,31 @@ macro_rules! apply_param_modifier {
};
}
macro_rules! apply_return_modifier {
(val, $result:ident) => {
$result
};
(move, $result:ident) => {
$crate::mem::heap_move_nonnull($result)
};
(move_ok, $result:ident) => {
$crate::mem::heap_move_ok($result)
};
(move_some, $result:ident) => {
$crate::mem::heap_move_some($result)
};
}
macro_rules! wrap_function {
(
$module:ident;
$(#[$meta:meta])+
fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?)
$(-> $return_type:ty)?
$(-> $return_modifier:ident $return_type:ty)?
$block:block;
) => {
::paste::paste! {
$(#[$meta])*
$(#[$meta])+
#[doc = ""]
#[doc = " This function is part of the `" $module "` module."]
#[no_mangle]
@ -191,10 +206,14 @@ macro_rules! wrap_function {
$(
let $param_name = $crate::macros::apply_param_modifier!($param_modifier, $param_name);
)*
$block
}
let result = $block;
$(
let result = $crate::macros::apply_return_modifier!($return_modifier, result);
)?
result
}
}
};
}
macro_rules! wrap_functions {
@ -203,7 +222,7 @@ macro_rules! wrap_functions {
$(
$(#[$meta:meta])+
fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?)
$(-> $return_type:ty)?
$(-> $return_modifier:ident $return_type:ty)?
$block:block;
)+
) => {
@ -211,7 +230,7 @@ macro_rules! wrap_functions {
$(
$crate::macros::wrap_function!($module;
$(#[$meta])+
fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)?
fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_modifier $return_type)?
$block;
);
)+
@ -222,7 +241,7 @@ macro_rules! wrap_functions {
$(
$(#[$meta:meta])+
fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?)
$(-> $return_type:ty)?
$(-> $return_modifier:ident $return_type:ty)?
$block:block;
)+
) => {
@ -230,7 +249,7 @@ macro_rules! wrap_functions {
$crate::macros::wrap_functions!{[< $object_type:lower >];
$(
$(#[$meta])+
fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)?
fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_modifier $return_type)?
$block;
)+
}
@ -239,7 +258,7 @@ macro_rules! wrap_functions {
}
pub(crate) use {
apply_param_modifier, derive_clone, derive_free,
wrap_fields, wrap_fields_accessor, wrap_function,
wrap_functions, wrap_method, wrap_methods,
apply_param_modifier, apply_return_modifier, derive_clone, derive_free,
wrap_fields, wrap_fields_accessor, wrap_function, wrap_functions,
wrap_method, wrap_methods,
};

View file

@ -3,7 +3,6 @@ use crate::{
macros::{
derive_clone, derive_free, wrap_fields, wrap_functions, wrap_methods,
},
mem::{heap_move_nonnull, heap_move_ok},
};
use servicepoint::{CommandCode, Header, Packet};
use std::ptr::NonNull;
@ -12,22 +11,22 @@ wrap_functions!(associate Packet;
/// Tries to load a [Packet] from the passed array with the specified length.
///
/// returns: NULL in case of an error, pointer to the allocated packet otherwise
fn try_load(data: val ByteSlice) -> *mut Packet {
fn try_load(data: val ByteSlice) -> move_ok *mut Packet {
let data = unsafe { data.as_slice() };
heap_move_ok(servicepoint::Packet::try_from(data))
servicepoint::Packet::try_from(data)
};
/// Creates a raw [Packet] from parts.
///
/// returns: new instance. Will never return null.
fn from_parts(header: val Header, payload: val ByteSlice) -> NonNull<Packet> {
fn from_parts(header: val Header, payload: val ByteSlice) -> move NonNull<Packet> {
let payload = if payload == ByteSlice::INVALID {
None
} else {
Some(Vec::from(unsafe { payload.as_slice() }))
};
heap_move_nonnull(Packet { header, payload })
Packet { header, payload }
};
);
@ -44,7 +43,7 @@ wrap_methods! { Packet;
/// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload.
///
/// The returned memory can be changed and will be valid until a new payload is set.
fn get_payload(mut packet) -> ByteSlice {
fn get_payload(mut packet) -> val ByteSlice {
match &mut packet.payload {
None => ByteSlice::INVALID,
Some(payload) => unsafe { ByteSlice::from_slice(payload) },
@ -67,7 +66,7 @@ wrap_methods! { Packet;
/// # Panics
///
/// - if the buffer is not big enough to hold header+payload.
fn serialize_to(mut packet, buffer: val ByteSlice) -> usize {
fn serialize_to(mut packet, buffer: val ByteSlice) -> val usize {
unsafe {
packet.serialize_to(buffer.as_slice_mut()).unwrap_or(0)
}
@ -79,7 +78,7 @@ wrap_functions!(sp;
/// Converts u16 into [CommandCode].
///
/// If the provided value is not valid, false is returned and result is not changed.
fn u16_to_command_code(code: val u16, result: mut NonNull<CommandCode>) -> bool {
fn u16_to_command_code(code: val u16, result: mut NonNull<CommandCode>) -> val bool {
match CommandCode::try_from(code) {
Ok(code) => {
*result = code;

View file

@ -1,7 +1,7 @@
use crate::{
commands::{CommandTag, GenericCommand},
macros::{derive_free, wrap_functions, wrap_methods},
mem::{heap_move_ok, heap_remove},
mem::heap_remove,
};
use servicepoint::{Header, Packet, UdpSocketExt};
use std::{
@ -13,7 +13,6 @@ use std::{
derive_free!(UdpSocket);
wrap_functions!(associate UdpSocket;
/// Creates a new instance of [UdpSocket].
///
/// returns: NULL if connection fails, or connected instance
@ -25,11 +24,11 @@ wrap_functions!(associate UdpSocket;
/// if (connection != NULL)
/// sp_udp_send_command(connection, sp_command_clear());
/// ```
fn open(host: val NonNull<c_char>) -> *mut UdpSocket {
fn open(host: val NonNull<c_char>) -> move_ok *mut UdpSocket {
let host = unsafe { CStr::from_ptr(host.as_ptr()) }
.to_str()
.expect("Bad encoding");
heap_move_ok(UdpSocket::bind_connect(host))
UdpSocket::bind_connect(host)
};
/// Creates a new instance of [UdpSocket].
@ -43,11 +42,10 @@ wrap_functions!(associate UdpSocket;
/// if (connection != NULL)
/// sp_udp_send_command(connection, sp_command_clear());
/// ```
fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> *mut UdpSocket {
fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> move_ok *mut UdpSocket {
let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port);
heap_move_ok(UdpSocket::bind_connect(addr))
UdpSocket::bind_connect(addr)
};
);
wrap_methods! {UdpSocket;
@ -56,7 +54,7 @@ wrap_methods! {UdpSocket;
/// The passed `packet` gets consumed.
///
/// returns: true in case of success
fn send_packet(ref connection, packet: move NonNull<Packet>) -> bool {
fn send_packet(ref connection, packet: move NonNull<Packet>) -> val bool {
connection.send(&Vec::from(packet)).is_ok()
};
@ -71,7 +69,7 @@ wrap_methods! {UdpSocket;
/// ```C
/// sp_udp_send_command(connection, sp_command_brightness(5));
/// ```
fn send_command(ref connection, command: mut NonNull<GenericCommand>) -> bool {
fn send_command(ref connection, command: mut NonNull<GenericCommand>) -> val bool {
unsafe {
let result = match command.tag {
CommandTag::Invalid => return false,
@ -100,7 +98,7 @@ wrap_methods! {UdpSocket;
/// ```C
/// sp_udp_send_header(connection, sp_command_brightness(5));
/// ```
fn send_header(ref udp_connection, header: val Header) -> bool {
fn send_header(ref udp_connection, header: val Header) -> val bool {
let packet = Packet {
header,
payload: None,