add parameter modifiers

This commit is contained in:
Vinzenz Schroeter 2025-06-23 23:28:30 +02:00
parent 664625402f
commit e8f11c08ea
17 changed files with 214 additions and 230 deletions

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
commands::{wrap_command, wrap_origin_accessors}, commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions}, macros::{wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_remove}, mem::heap_move_nonnull,
}; };
use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin}; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin};
use std::ptr::NonNull; use std::ptr::NonNull;
@ -22,13 +22,13 @@ wrap_functions!(associate BitmapCommand;
/// ///
/// Returns: a new [BitmapCommand] instance. /// Returns: a new [BitmapCommand] instance.
fn new( fn new(
bitmap: NonNull<Bitmap>, bitmap: move NonNull<Bitmap>,
origin_x: usize, origin_x: val usize,
origin_y: usize, origin_y: val usize,
compression: CompressionCode, compression: val CompressionCode,
) -> NonNull<BitmapCommand> { ) -> NonNull<BitmapCommand> {
heap_move_nonnull(BitmapCommand { heap_move_nonnull(BitmapCommand {
bitmap: unsafe { heap_remove(bitmap) }, bitmap,
origin: Origin::new(origin_x, origin_y), origin: Origin::new(origin_x, origin_y),
compression, compression,
}) })
@ -38,9 +38,7 @@ wrap_functions!(associate BitmapCommand;
/// leaving other fields as their default values. /// leaving other fields as their default values.
/// ///
/// Rust equivalent: `BitmapCommand::from(bitmap)` /// Rust equivalent: `BitmapCommand::from(bitmap)`
fn from_bitmap( fn from_bitmap(bitmap: move NonNull<Bitmap>) -> NonNull<BitmapCommand> {
bitmap: NonNull<Bitmap>, heap_move_nonnull(bitmap.into())
) -> NonNull<BitmapCommand> {
heap_move_nonnull(unsafe { heap_remove(bitmap) }.into())
} }
); );

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
commands::wrap_command, commands::wrap_command,
macros::{wrap_fields, wrap_functions}, macros::{wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_remove}, mem::heap_move_nonnull,
}; };
use servicepoint::{ use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset,
@ -32,13 +32,13 @@ wrap_functions!(associate BitVecCommand;
/// ///
/// The contained [`DisplayBitVec`] is always uncompressed. /// The contained [`DisplayBitVec`] is always uncompressed.
fn new( fn new(
bitvec: NonNull<DisplayBitVec>, bitvec: move NonNull<DisplayBitVec>,
offset: usize, offset: val usize,
operation: BinaryOperation, operation: val BinaryOperation,
compression: CompressionCode, compression: val CompressionCode,
) -> NonNull<BitVecCommand> { ) -> NonNull<BitVecCommand> {
heap_move_nonnull(BitVecCommand { heap_move_nonnull(BitVecCommand {
bitvec: unsafe { heap_remove(bitvec) }, bitvec,
offset, offset,
operation, operation,
compression, compression,

View file

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

View file

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

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
commands::{wrap_command, wrap_origin_accessors}, commands::{wrap_command, wrap_origin_accessors},
macros::{wrap_fields, wrap_functions}, macros::{wrap_fields, wrap_functions},
mem::{heap_move_nonnull, heap_remove}, mem::heap_move_nonnull,
}; };
use servicepoint::{Cp437Grid, Cp437GridCommand, Origin}; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin};
use std::ptr::NonNull; use std::ptr::NonNull;
@ -22,22 +22,20 @@ wrap_functions!(associate Cp437GridCommand;
/// ///
/// The origin is relative to the top-left of the display. /// The origin is relative to the top-left of the display.
fn new( fn new(
grid: NonNull<Cp437Grid>, grid: move NonNull<Cp437Grid>,
origin_x: usize, origin_x: val usize,
origin_y: usize, origin_y: val usize,
) -> NonNull<Cp437GridCommand> { ) -> NonNull<Cp437GridCommand> {
heap_move_nonnull(Cp437GridCommand { heap_move_nonnull(Cp437GridCommand {
grid: unsafe { heap_remove(grid) }, grid,
origin: Origin::new(origin_x, origin_y), origin: Origin::new(origin_x, origin_y),
}) })
} }
/// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand],
/// leaving other fields as their default values. /// leaving other fields as their default values.
fn from_grid( fn from_grid(grid: move NonNull<Cp437Grid>) -> NonNull<Cp437GridCommand> {
grid: NonNull<Cp437Grid>, heap_move_nonnull(grid.into())
) -> NonNull<Cp437GridCommand> {
heap_move_nonnull(unsafe { heap_remove(grid) }.into())
} }
); );

View file

@ -178,10 +178,7 @@ wrap_functions!(associate GenericCommand;
/// The packet is dropped in the process. /// The packet is dropped in the process.
/// ///
/// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. /// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed.
fn try_from_packet( fn try_from_packet(packet: move NonNull<Packet>) -> NonNull<GenericCommand> {
packet: NonNull<Packet>,
) -> NonNull<GenericCommand> {
let packet = unsafe { heap_remove(packet) };
let result = servicepoint::TypedCommand::try_from(packet) let result = servicepoint::TypedCommand::try_from(packet)
.map(|value| match value { .map(|value| match value {
TypedCommand::Clear(clear) => GenericCommand { TypedCommand::Clear(clear) => GenericCommand {

View file

@ -11,7 +11,7 @@ wrap_functions!(associate GlobalBrightnessCommand;
/// Set the brightness of all tiles to the same value. /// Set the brightness of all tiles to the same value.
/// ///
/// Returns: a new [GlobalBrightnessCommand] instance. /// Returns: a new [GlobalBrightnessCommand] instance.
fn new(brightness: Brightness) -> NonNull<GlobalBrightnessCommand> { fn new(brightness: val Brightness) -> NonNull<GlobalBrightnessCommand> {
heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) heap_move_nonnull(GlobalBrightnessCommand::from(brightness))
} }

View file

@ -17,31 +17,22 @@ pub use generic_command::*;
macro_rules! wrap_origin_accessors { macro_rules! wrap_origin_accessors {
( $object_type:ident ) => { ( $object_type:ident ) => {
::paste::paste! { ::paste::paste! {
$crate::macros::wrap_functions!(associate $object_type; $crate::macros::wrap_methods!($object_type;
#[doc = " Reads the origin field of the [`" $object_type "`]."] #[doc = " Reads the origin field of the [`" $object_type "`]."]
fn get_origin( fn get_origin(
command: ::core::ptr::NonNull<$object_type>, ref command,
origin_x: ::core::ptr::NonNull<usize>, origin_x: mut ::core::ptr::NonNull<usize>,
origin_y: ::core::ptr::NonNull<usize>, origin_y: mut ::core::ptr::NonNull<usize>
) { ) {
unsafe { let origin = command.origin;
let origin = &command.as_ref().origin; *origin_x = origin.x;
*origin_x.as_ptr() = origin.x; *origin_y = origin.y;
*origin_y.as_ptr() = origin.y; };
}
}
#[doc = " Overwrites the origin field of the [`" $object_type "`]."] #[doc = " Overwrites the origin field of the [`" $object_type "`]."]
fn set_origin( fn set_origin(mut command, origin_x: val usize, origin_y: val usize) {
command: ::core::ptr::NonNull<$object_type>, command.origin = ::servicepoint::Origin::new(origin_x, origin_y);
origin_x: usize, };
origin_y: usize,
) {
unsafe {
$crate::macros::nonnull_as_mut!(command).origin =
::servicepoint::Origin::new(origin_x, origin_y);
}
}
); );
} }
}; };

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
containers::{wrap_grid, ByteSlice}, containers::{wrap_grid, ByteSlice},
macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, macros::{wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some},
}; };
use servicepoint::{ use servicepoint::{
Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid,
@ -35,7 +35,7 @@ wrap_functions!(associate Bitmap;
/// sp_bitmap_set(grid, 0, 0, false); /// sp_bitmap_set(grid, 0, 0, false);
/// sp_bitmap_free(grid); /// sp_bitmap_free(grid);
/// ``` /// ```
fn new(width: usize, height: usize) -> *mut Bitmap { fn new(width: val usize, height: val usize) -> *mut Bitmap {
heap_move_some(Bitmap::new(width, height)) heap_move_some(Bitmap::new(width, height))
} }
@ -55,9 +55,9 @@ wrap_functions!(associate Bitmap;
/// ///
/// returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. /// returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error.
fn load( fn load(
width: usize, width: val usize,
height: usize, height: val usize,
data: ByteSlice, data: val ByteSlice,
) -> *mut Bitmap { ) -> *mut Bitmap {
let data = unsafe { data.as_slice() }; let data = unsafe { data.as_slice() };
heap_move_ok(Bitmap::load(width, height, data)) heap_move_ok(Bitmap::load(width, height, data))
@ -69,10 +69,9 @@ wrap_functions!(associate Bitmap;
/// ///
/// Returns NULL in case of error. /// Returns NULL in case of error.
fn from_bitvec( fn from_bitvec(
width: usize, width: val usize,
bitvec: NonNull<DisplayBitVec>, bitvec: move NonNull<DisplayBitVec>,
) -> *mut Bitmap { ) -> *mut Bitmap {
let bitvec = unsafe { heap_remove(bitvec) };
heap_move_ok(Bitmap::from_bitvec(width, bitvec)) heap_move_ok(Bitmap::from_bitvec(width, bitvec))
} }
); );
@ -88,7 +87,7 @@ wrap_methods!(Bitmap;
/// The provided [Bitmap] gets consumed. /// The provided [Bitmap] gets consumed.
/// ///
/// Returns NULL in case of an error. /// Returns NULL in case of an error.
fn try_into_packet(move bitmap, x: usize, y: usize, compression: CompressionCode) -> *mut Packet { fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> *mut Packet {
heap_move_ok(Packet::try_from(BitmapCommand { heap_move_ok(Packet::try_from(BitmapCommand {
bitmap, bitmap,
origin: Origin::new(x, y), origin: Origin::new(x, y),

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
containers::{wrap_container, ByteSlice}, containers::{wrap_container, ByteSlice},
macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, macros::{wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok}, mem::{heap_move_nonnull, heap_move_ok},
}; };
use servicepoint::{ use servicepoint::{
@ -23,14 +23,14 @@ wrap_functions!(associate DisplayBitVec;
/// # Panics /// # Panics
/// ///
/// - when `size` is not divisible by 8. /// - when `size` is not divisible by 8.
fn new(size: usize) -> NonNull<DisplayBitVec> { fn new(size: val usize) -> NonNull<DisplayBitVec> {
heap_move_nonnull(DisplayBitVec::repeat(false, size)) heap_move_nonnull(DisplayBitVec::repeat(false, size))
} }
/// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance.
/// ///
/// returns: [DisplayBitVec] instance containing data. /// returns: [DisplayBitVec] instance containing data.
fn load(data: ByteSlice) -> NonNull<DisplayBitVec> { fn load(data: val ByteSlice) -> NonNull<DisplayBitVec> {
let data = unsafe { data.as_slice() }; let data = unsafe { data.as_slice() };
heap_move_nonnull(DisplayBitVec::from_slice(data)) heap_move_nonnull(DisplayBitVec::from_slice(data))
} }
@ -45,9 +45,9 @@ wrap_methods!(DisplayBitVec;
/// Returns NULL in case of an error. /// Returns NULL in case of an error.
fn try_into_packet( fn try_into_packet(
move bitvec, move bitvec,
offset: usize, offset: val usize,
operation: BinaryOperation, operation: val BinaryOperation,
compression: CompressionCode compression: val CompressionCode
) -> *mut Packet { ) -> *mut Packet {
heap_move_ok(Packet::try_from(BitVecCommand { heap_move_ok(Packet::try_from(BitVecCommand {
bitvec, bitvec,
@ -69,7 +69,7 @@ wrap_methods!(DisplayBitVec;
/// # Panics /// # Panics
/// ///
/// - when accessing `index` out of bounds /// - when accessing `index` out of bounds
fn get(ref instance, index: usize) -> bool { fn get(ref instance, index: val usize) -> bool {
instance.get(index).map(|x| *x).unwrap_or(false) instance.get(index).map(|x| *x).unwrap_or(false)
}; };
@ -83,14 +83,14 @@ wrap_methods!(DisplayBitVec;
/// # Panics /// # Panics
/// ///
/// - when accessing `index` out of bounds /// - when accessing `index` out of bounds
fn set(mut instance, index: usize, value: bool); fn set(mut instance, index: val usize, value: val bool);
/// Sets the value of all bits. /// Sets the value of all bits.
/// ///
/// # Arguments /// # Arguments
/// ///
/// - `value`: the value to set all bits to /// - `value`: the value to set all bits to
fn fill(mut instance, value: bool); fn fill(mut instance, value: val bool);
/// Gets the length in bits. /// Gets the length in bits.
fn len(ref instance) -> usize; fn len(ref instance) -> usize;

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
containers::{wrap_grid, ByteSlice}, containers::{wrap_grid, ByteSlice},
macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, macros::{wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some},
}; };
use servicepoint::{ use servicepoint::{
@ -30,10 +30,7 @@ wrap_functions!(associate BrightnessGrid;
/// TypedCommand *command = sp_command_char_brightness(grid); /// TypedCommand *command = sp_command_char_brightness(grid);
/// sp_udp_free(connection); /// sp_udp_free(connection);
/// ``` /// ```
fn new( fn new(width: val usize, height: val usize) -> NonNull<BrightnessGrid> {
width: usize,
height: usize,
) -> NonNull<BrightnessGrid> {
heap_move_nonnull(BrightnessGrid::new(width, height)) heap_move_nonnull(BrightnessGrid::new(width, height))
} }
@ -43,9 +40,9 @@ wrap_functions!(associate BrightnessGrid;
/// ///
/// returns: new [BrightnessGrid] instance, or NULL in case of an error. /// returns: new [BrightnessGrid] instance, or NULL in case of an error.
fn load( fn load(
width: usize, width: val usize,
height: usize, height: val usize,
data: ByteSlice, data: val ByteSlice,
) -> *mut BrightnessGrid { ) -> *mut BrightnessGrid {
let data = unsafe { data.as_slice() }; let data = unsafe { data.as_slice() };
heap_move_some( heap_move_some(
@ -62,7 +59,7 @@ wrap_methods!(BrightnessGrid;
/// The provided [BrightnessGrid] gets consumed. /// The provided [BrightnessGrid] gets consumed.
/// ///
/// Returns NULL in case of an error. /// Returns NULL in case of an error.
fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet {
heap_move_ok(Packet::try_from(BrightnessGridCommand { heap_move_ok(Packet::try_from(BrightnessGridCommand {
grid, grid,
origin: Origin::new(x, y), origin: Origin::new(x, y),

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
containers::{derive_get_width_height, wrap_container, ByteSlice}, containers::{derive_get_width_height, wrap_container, ByteSlice},
macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, macros::{wrap_functions, wrap_methods},
mem::{heap_move_nonnull, heap_move_ok}, mem::{heap_move_nonnull, heap_move_ok},
}; };
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
@ -23,14 +23,14 @@ wrap_functions!(associate CharGrid;
/// sp_char_grid_set(grid, 0, 0, '!'); /// sp_char_grid_set(grid, 0, 0, '!');
/// sp_char_grid_free(grid); /// sp_char_grid_free(grid);
/// ``` /// ```
fn new(width: usize, height: usize) -> NonNull<CharGrid> { fn new(width: val usize, height: val usize) -> NonNull<CharGrid> {
heap_move_nonnull(CharGrid::new(width, height)) heap_move_nonnull(CharGrid::new(width, height))
} }
/// Loads a [CharGrid] with the specified dimensions from the provided data. /// Loads a [CharGrid] with the specified dimensions from the provided data.
/// ///
/// returns: new CharGrid or NULL in case of an error /// returns: new CharGrid or NULL in case of an error
fn load(width: usize, height: usize, data: ByteSlice) -> *mut CharGrid { fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut CharGrid {
let data = unsafe { data.as_slice() }; let data = unsafe { data.as_slice() };
heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec()))
} }
@ -49,7 +49,7 @@ wrap_methods!(CharGrid;
/// # Panics /// # Panics
/// ///
/// - when accessing `x` or `y` out of bounds /// - when accessing `x` or `y` out of bounds
fn get(ref instance, x: usize, y: usize) -> u32 { fn get(ref instance, x: val usize, y: val usize) -> u32 {
instance.get(x, y) as u32 instance.get(x, y) as u32
}; };
@ -66,7 +66,7 @@ wrap_methods!(CharGrid;
/// ///
/// - when accessing `x` or `y` out of bounds /// - when accessing `x` or `y` out of bounds
/// - when providing values that cannot be converted to Rust's `char`. /// - when providing values that cannot be converted to Rust's `char`.
fn set(mut instance, x: usize, y: usize, value: u32) { fn set(mut instance, x: val usize, y: val usize, value: val u32) {
instance.set(x, y, char::from_u32(value).unwrap()) instance.set(x, y, char::from_u32(value).unwrap())
}; };
@ -76,7 +76,7 @@ wrap_methods!(CharGrid;
/// ///
/// - `value`: the value to set all cells to /// - `value`: the value to set all cells to
/// - when providing values that cannot be converted to Rust's `char`. /// - when providing values that cannot be converted to Rust's `char`.
fn fill(mut instance, value: u32) { fn fill(mut instance, value: val u32) {
instance.fill(char::from_u32(value).unwrap()) instance.fill(char::from_u32(value).unwrap())
}; };
@ -85,7 +85,7 @@ wrap_methods!(CharGrid;
/// The provided [CharGrid] gets consumed. /// The provided [CharGrid] gets consumed.
/// ///
/// Returns NULL in case of an error. /// Returns NULL in case of an error.
fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet {
heap_move_ok(Packet::try_from(CharGridCommand { heap_move_ok(Packet::try_from(CharGridCommand {
grid, grid,
origin: Origin::new(x, y), origin: Origin::new(x, y),

View file

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

View file

@ -14,8 +14,8 @@ pub use cp437_grid::*;
macro_rules! wrap_container { macro_rules! wrap_container {
($object_type:ident) => { ($object_type:ident) => {
derive_clone!($object_type); $crate::macros::derive_clone!($object_type);
derive_free!($object_type); $crate::macros::derive_free!($object_type);
}; };
} }
@ -45,7 +45,7 @@ macro_rules! wrap_grid {
/// # Panics /// # Panics
/// ///
/// - when accessing `x` or `y` out of bounds /// - when accessing `x` or `y` out of bounds
fn get(ref instance, x: usize, y: usize) -> $value_type; fn get(ref instance, x: val usize, y: val usize) -> $value_type;
/// Sets the value of the specified position. /// Sets the value of the specified position.
/// ///
@ -57,14 +57,14 @@ macro_rules! wrap_grid {
/// # Panics /// # Panics
/// ///
/// - when accessing `x` or `y` out of bounds /// - when accessing `x` or `y` out of bounds
fn set(mut instance, x: usize, y: usize, value: $value_type); fn set(mut instance, x: val usize, y: val usize, value: val $value_type);
/// Sets the state of all cells in the grid. /// Sets the state of all cells in the grid.
/// ///
/// # Arguments /// # Arguments
/// ///
/// - `value`: the value to set all cells to /// - `value`: the value to set all cells to
fn fill(mut instance, value: $value_type); fn fill(mut instance, value: val $value_type);
} }
}; };
} }

View file

@ -3,8 +3,9 @@ macro_rules! derive_free {
::paste::paste! { ::paste::paste! {
$crate::macros::wrap_functions!([< $typ:lower >]; $crate::macros::wrap_functions!([< $typ:lower >];
#[doc = "Deallocates a [`" $typ "`] instance."] #[doc = "Deallocates a [`" $typ "`] instance."]
fn free(instance: ::core::ptr::NonNull<$typ>) { #[allow(dropping_copy_types)]
unsafe { $crate::mem::heap_drop(instance) } fn free(instance: move ::core::ptr::NonNull<$typ>) {
::std::mem::drop(instance)
} }
); );
} }
@ -16,8 +17,8 @@ macro_rules! derive_clone {
::paste::paste! { ::paste::paste! {
$crate::macros::wrap_functions!([< $typ:lower >]; $crate::macros::wrap_functions!([< $typ:lower >];
#[doc = "Clones a [`" $typ "`] instance."] #[doc = "Clones a [`" $typ "`] instance."]
fn clone(instance: ::core::ptr::NonNull<$typ>) -> ::core::ptr::NonNull<$typ> { fn clone(instance: ref ::core::ptr::NonNull<$typ>) -> ::core::ptr::NonNull<$typ> {
unsafe { $crate::mem::heap_clone(instance) } $crate::mem::heap_move_nonnull(instance.clone())
} }
); );
} }
@ -36,24 +37,18 @@ macro_rules! nonnull_as_mut {
}; };
} }
macro_rules! nonnull_as_move {
($ident:ident) => {
$crate::mem::heap_remove($ident)
};
}
macro_rules! wrap_method { macro_rules! wrap_method {
( (
$object_type:ident; $object_type:ident;
$(#[$meta:meta])+ $(#[$meta:meta])+
fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?)
$(-> $return_type:ty)? $(-> $return_type:ty)?
) => { ) => {
::paste::paste!{ ::paste::paste!{
$crate::macros::wrap_method!( $crate::macros::wrap_method!(
$object_type; $object_type;
$(#[$meta])+ $(#[$meta])+
fn $function($ref_or_mut $instance $(, $($param_name: $param_type),*)?) fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?)
$(-> $return_type)? { $(-> $return_type)? {
$instance.$function($($($param_name),*)?) $instance.$function($($($param_name),*)?)
} }
@ -62,7 +57,7 @@ macro_rules! wrap_method {
}; };
($object_type:ident; ($object_type:ident;
$(#[$meta:meta])+ $(#[$meta:meta])+
fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?)
$(-> $return_type:ty)? $(-> $return_type:ty)?
$impl:block $impl:block
) => { ) => {
@ -72,12 +67,10 @@ macro_rules! wrap_method {
/// ///
$(#[$meta])* $(#[$meta])*
fn $function( fn $function(
$instance: ::core::ptr::NonNull<$object_type> $instance: $ref_or_mut ::core::ptr::NonNull<$object_type>
$(,$($param_name: $param_type),*)? $(,$($param_name: $param_modifier $param_type),*)?
) $(-> $return_type)? { ) $(-> $return_type)?
let $instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !($instance) }; $impl
$impl
}
); );
} }
}; };
@ -88,7 +81,7 @@ macro_rules! wrap_methods {
$object_type:ident; $object_type:ident;
$( $(
$(#[$meta:meta])+ $(#[$meta:meta])+
fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?)
$(-> $return_type:ty)? $(-> $return_type:ty)?
$($impl:block)?; $($impl:block)?;
)+ )+
@ -97,7 +90,7 @@ macro_rules! wrap_methods {
$( $(
$crate::macros::wrap_method!($object_type; $crate::macros::wrap_method!($object_type;
$(#[$meta])* $(#[$meta])*
fn $function($ref_or_mut $instance $(, $($param_name: $param_type),*)?) fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?)
$(-> $return_type)? $(-> $return_type)?
$($impl)? $($impl)?
); );
@ -109,45 +102,35 @@ macro_rules! wrap_methods {
macro_rules! wrap_fields_accessor { macro_rules! wrap_fields_accessor {
(get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { (get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => {
paste::paste! { paste::paste! {
$crate::macros::wrap_functions! {associate $object_type; $crate::macros::wrap_method! {$object_type;
#[doc = " Gets the value of field `" $prop_name #[doc = " Gets the value of field `" $prop_name
"` of the [`servicepoint::" $object_type "`]."] "` of the [`servicepoint::" $object_type "`]."]
fn [<get _ $prop_name>]( fn [<get _ $prop_name>](ref instance) -> $prop_type {
instance: ::core::ptr::NonNull<$object_type> return instance.$prop_name;
) -> $prop_type {
let $prop_name = unsafe { $crate::macros::nonnull_as_ref!(instance).$prop_name };
return $prop_name;
} }
} }
} }
}; };
(mut get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { (mut get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => {
paste::paste! { paste::paste! {
$crate::macros::wrap_functions! {associate $object_type; $crate::macros::wrap_method! {$object_type;
#[doc = " Gets a reference to the field `" $prop_name #[doc = " Gets a reference to the field `" $prop_name
"` of the [`servicepoint::" $object_type "`]."] "` of the [`servicepoint::" $object_type "`]."]
/// ///
/// - The returned reference inherits the lifetime of object in which it is contained. /// - 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. /// - 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>]( fn [<get _ $prop_name _mut>](mut instance) -> ::core::ptr::NonNull<$prop_type> {
instance: ::core::ptr::NonNull<$object_type> return ::core::ptr::NonNull::from(&mut instance.$prop_name);
) -> ::core::ptr::NonNull<$prop_type> {
let $prop_name = unsafe { &mut $crate::macros::nonnull_as_mut!(instance).$prop_name };
return ::core::ptr::NonNull::from($prop_name);
} }
} }
} }
}; };
(set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { (set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => {
paste::paste! { paste::paste! {
$crate::macros::wrap_functions! {associate $object_type; $crate::macros::wrap_method! {$object_type;
#[doc = " Sets the value of field `" $prop_name #[doc = " Sets the value of field `" $prop_name
"` of the [`servicepoint::" $object_type "`]."] "` of the [`servicepoint::" $object_type "`]."]
fn [<set _ $prop_name>]( fn [<set _ $prop_name>](mut instance, value: val $prop_type) {
instance: ::core::ptr::NonNull<$object_type>,
value: $prop_type,
) {
let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) };
instance.$prop_name = value; instance.$prop_name = value;
} }
} }
@ -155,17 +138,12 @@ macro_rules! wrap_fields_accessor {
}; };
(move set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { (move set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => {
paste::paste! { paste::paste! {
$crate::macros::wrap_functions! {associate $object_type; $crate::macros::wrap_method! {$object_type;
#[doc = " Sets the value of field `" $prop_name #[doc = " Sets the value of field `" $prop_name
"` of the [`servicepoint::" $object_type "`]."] "` of the [`servicepoint::" $object_type "`]."]
/// The provided value is moved into the instance, potentially invalidating previously taken references. /// The provided value is moved into the instance, potentially invalidating previously taken references.
fn [<set _ $prop_name>]( fn [<set _ $prop_name>](mut instance, value: move ::core::ptr::NonNull<$prop_type>) {
instance: ::core::ptr::NonNull<$object_type>, instance.$prop_name = value;
value: ::core::ptr::NonNull<$prop_type>,
) {
let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) };
let $prop_name = unsafe { $crate::mem::heap_remove(value) };
instance.$prop_name = $prop_name;
} }
} }
} }
@ -190,18 +168,30 @@ macro_rules! wrap_fields {
}; };
} }
macro_rules! wrap_functions { macro_rules! apply_param_modifier {
(move, $param_name:ident) => {
unsafe { $crate::mem::heap_remove($param_name) }
};
(val, $param_name:ident) => {
$param_name
};
(mut, $param_name:ident) => {
unsafe { $crate::macros::nonnull_as_mut!($param_name) }
};
(ref, $param_name:ident) => {
unsafe { $crate::macros::nonnull_as_ref!($param_name) }
};
}
macro_rules! wrap_function {
( (
$module:ident; $module:ident;
$( $(#[$meta:meta])+
$(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?)
fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) $(-> $return_type:ty)?
$(-> $return_type:ty)? $block:block
$block:block
)+
) => { ) => {
::paste::paste! { ::paste::paste! {
$(
$(#[$meta])* $(#[$meta])*
#[doc = ""] #[doc = ""]
#[doc = " This function is part of the `" $module "` module."] #[doc = " This function is part of the `" $module "` module."]
@ -209,7 +199,33 @@ macro_rules! wrap_functions {
pub unsafe extern "C" fn [< sp_ $module _ $function >]( pub unsafe extern "C" fn [< sp_ $module _ $function >](
$($param_name: $param_type),* $($param_name: $param_type),*
) $(-> $return_type)? ) $(-> $return_type)?
$block {
$(
let $param_name = $crate::macros::apply_param_modifier!($param_modifier, $param_name);
)*
$block
}
}
}
}
macro_rules! wrap_functions {
(
$module:ident;
$(
$(#[$meta:meta])+
fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?)
$(-> $return_type:ty)?
$block:block
)+
) => {
::paste::paste! {
$(
$crate::macros::wrap_function!($module;
$(#[$meta])+
fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)?
$block
);
)+ )+
} }
}; };
@ -217,7 +233,7 @@ macro_rules! wrap_functions {
associate $object_type:ident; associate $object_type:ident;
$( $(
$(#[$meta:meta])+ $(#[$meta:meta])+
fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?)
$(-> $return_type:ty)? $(-> $return_type:ty)?
$block:block $block:block
)+ )+
@ -226,7 +242,7 @@ macro_rules! wrap_functions {
$crate::macros::wrap_functions!{[< $object_type:lower >]; $crate::macros::wrap_functions!{[< $object_type:lower >];
$( $(
$(#[$meta])+ $(#[$meta])+
fn $function($($param_name: $param_type),*) $(-> $return_type)? fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)?
$block $block
)+ )+
} }
@ -235,7 +251,7 @@ macro_rules! wrap_functions {
} }
pub(crate) use { pub(crate) use {
derive_clone, derive_free, nonnull_as_move, nonnull_as_mut, nonnull_as_ref, apply_param_modifier, derive_clone, derive_free, nonnull_as_mut,
wrap_fields, wrap_fields_accessor, wrap_functions, wrap_method, nonnull_as_ref, wrap_fields, wrap_fields_accessor, wrap_function,
wrap_methods, wrap_functions, wrap_method, wrap_methods,
}; };

View file

@ -1,17 +1,18 @@
use crate::{ use crate::{
containers::ByteSlice, containers::ByteSlice,
macros::{derive_clone, derive_free, wrap_fields, wrap_functions}, macros::{
derive_clone, derive_free, wrap_fields, wrap_functions, wrap_methods,
},
mem::{heap_move_nonnull, heap_move_ok}, mem::{heap_move_nonnull, heap_move_ok},
}; };
use servicepoint::{CommandCode, Header, Packet}; use servicepoint::{CommandCode, Header, Packet};
use std::ptr::NonNull; use std::ptr::NonNull;
wrap_functions!(packet; wrap_functions!(associate Packet;
/// Tries to load a [Packet] from the passed array with the specified length. /// 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 /// returns: NULL in case of an error, pointer to the allocated packet otherwise
fn try_load(data: ByteSlice) -> *mut Packet { fn try_load(data: val ByteSlice) -> *mut Packet {
let data = unsafe { data.as_slice() }; let data = unsafe { data.as_slice() };
heap_move_ok(servicepoint::Packet::try_from(data)) heap_move_ok(servicepoint::Packet::try_from(data))
} }
@ -19,7 +20,7 @@ wrap_functions!(packet;
/// Creates a raw [Packet] from parts. /// Creates a raw [Packet] from parts.
/// ///
/// returns: new instance. Will never return null. /// returns: new instance. Will never return null.
fn from_parts(header: Header, payload: ByteSlice) -> NonNull<Packet> { fn from_parts(header: val Header, payload: val ByteSlice) -> NonNull<Packet> {
let payload = if payload == ByteSlice::INVALID { let payload = if payload == ByteSlice::INVALID {
None None
} else { } else {
@ -28,44 +29,6 @@ wrap_functions!(packet;
heap_move_nonnull(Packet { header, payload }) heap_move_nonnull(Packet { header, payload })
} }
/// Returns a pointer to the current payload of the provided 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(packet: NonNull<Packet>) -> ByteSlice {
unsafe {
match &mut (*packet.as_ptr()).payload {
None => ByteSlice::INVALID,
Some(payload) => ByteSlice::from_slice(payload),
}
}
}
/// Sets the payload of the provided packet to the provided data.
///
/// This makes previous payload pointers invalid.
fn set_payload(packet: NonNull<Packet>, data: ByteSlice) {
unsafe {
(*packet.as_ptr()).payload = if data == ByteSlice::INVALID {
None
} else {
Some(data.as_slice().to_vec())
}
}
}
/// Serialize the packet into the provided buffer.
///
/// # Panics
///
/// - if the buffer is not big enough to hold header+payload.
fn serialize_to(packet: NonNull<Packet>, buffer: ByteSlice) -> usize {
unsafe {
packet.as_ref().serialize_to(buffer.as_slice_mut()).unwrap_or(0)
}
}
); );
derive_clone!(Packet); derive_clone!(Packet);
@ -75,20 +38,51 @@ wrap_fields!(Packet;
prop header: Header { get; get mut; set; }; prop header: Header { get; get mut; set; };
); );
wrap_methods! { Packet;
/// Returns a pointer to the current payload of the provided 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 {
match &mut packet.payload {
None => ByteSlice::INVALID,
Some(payload) => unsafe { ByteSlice::from_slice(payload) },
}
};
/// Sets the payload of the provided packet to the provided data.
///
/// This makes previous payload pointers invalid.
fn set_payload(mut packet, data: val ByteSlice) {
packet.payload = if data == ByteSlice::INVALID {
None
} else {
Some(unsafe { data.as_slice().to_vec() })
}
};
/// Serialize the packet into the provided buffer.
///
/// # Panics
///
/// - if the buffer is not big enough to hold header+payload.
fn serialize_to(mut packet, buffer: val ByteSlice) -> usize {
unsafe {
packet.serialize_to(buffer.as_slice_mut()).unwrap_or(0)
}
};
}
wrap_functions!(sp; wrap_functions!(sp;
/// Converts u16 into [CommandCode]. /// Converts u16 into [CommandCode].
/// ///
/// If the provided value is not valid, false is returned and result is not changed. /// If the provided value is not valid, false is returned and result is not changed.
fn u16_to_command_code( fn u16_to_command_code(code: val u16, result: mut NonNull<CommandCode>) -> bool {
code: u16,
result: *mut CommandCode,
) -> bool {
match CommandCode::try_from(code) { match CommandCode::try_from(code) {
Ok(code) => { Ok(code) => {
unsafe { *result = code;
*result = code;
}
true true
} }
Err(_) => false, Err(_) => false,

View file

@ -25,7 +25,7 @@ wrap_functions!(associate UdpSocket;
/// if (connection != NULL) /// if (connection != NULL)
/// sp_udp_send_command(connection, sp_command_clear()); /// sp_udp_send_command(connection, sp_command_clear());
/// ``` /// ```
fn open(host: NonNull<c_char>) -> *mut UdpSocket { fn open(host: val NonNull<c_char>) -> *mut UdpSocket {
let host = unsafe { CStr::from_ptr(host.as_ptr()) } let host = unsafe { CStr::from_ptr(host.as_ptr()) }
.to_str() .to_str()
.expect("Bad encoding"); .expect("Bad encoding");
@ -44,7 +44,7 @@ wrap_functions!(associate UdpSocket;
/// if (connection != NULL) /// if (connection != NULL)
/// sp_udp_send_command(connection, sp_command_clear()); /// sp_udp_send_command(connection, sp_command_clear());
/// ``` /// ```
fn open_ipv4(ip1: u8, ip2: u8, ip3: u8, ip4: u8, port: u16) -> *mut UdpSocket { fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> *mut UdpSocket {
let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port);
heap_move_ok(UdpSocket::bind_connect(addr)) heap_move_ok(UdpSocket::bind_connect(addr))
} }
@ -57,8 +57,7 @@ wrap_methods! {UdpSocket;
/// The passed `packet` gets consumed. /// The passed `packet` gets consumed.
/// ///
/// returns: true in case of success /// returns: true in case of success
fn send_packet(ref connection, packet: NonNull<Packet>) -> bool { fn send_packet(ref connection, packet: move NonNull<Packet>) -> bool {
let packet = unsafe { heap_remove(packet) };
connection.send(&Vec::from(packet)).is_ok() connection.send(&Vec::from(packet)).is_ok()
}; };
@ -73,9 +72,8 @@ wrap_methods! {UdpSocket;
/// ```C /// ```C
/// sp_udp_send_command(connection, sp_command_brightness(5)); /// sp_udp_send_command(connection, sp_command_brightness(5));
/// ``` /// ```
fn send_command(ref connection, command: NonNull<GenericCommand>) -> bool { fn send_command(ref connection, command: mut NonNull<GenericCommand>) -> bool {
unsafe { unsafe {
let command = crate::macros::nonnull_as_mut!(command);
let result = match command.tag { let result = match command.tag {
CommandTag::Invalid => return false, CommandTag::Invalid => return false,
CommandTag::Bitmap => connection.send_command(heap_remove(command.data.bitmap)), CommandTag::Bitmap => connection.send_command(heap_remove(command.data.bitmap)),
@ -103,7 +101,7 @@ wrap_methods! {UdpSocket;
/// ```C /// ```C
/// sp_udp_send_header(connection, sp_command_brightness(5)); /// sp_udp_send_header(connection, sp_command_brightness(5));
/// ``` /// ```
fn send_header(ref udp_connection, header: Header) -> bool { fn send_header(ref udp_connection, header: val Header) -> bool {
let packet = Packet { let packet = Packet {
header, header,
payload: None, payload: None,