derive try_into_packet automatically
This commit is contained in:
parent
8116375fd0
commit
5ecb84ed16
9 changed files with 102 additions and 100 deletions
|
@ -1,9 +1,9 @@
|
|||
use crate::{
|
||||
commands::{wrap_command, wrap_origin_accessors},
|
||||
macros::{wrap_fields, wrap_functions},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
|
||||
mem::{heap_move_nonnull, heap_remove},
|
||||
};
|
||||
use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet};
|
||||
use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
wrap_command!(Bitmap);
|
||||
|
@ -43,13 +43,4 @@ wrap_functions!(associate BitmapCommand;
|
|||
) -> NonNull<BitmapCommand> {
|
||||
heap_move_nonnull(unsafe { heap_remove(bitmap) }.into())
|
||||
}
|
||||
|
||||
/// Tries to turn a [BitmapCommand] into a [Packet].
|
||||
///
|
||||
/// Returns: NULL or a [Packet] containing the command.
|
||||
fn try_into_packet(
|
||||
command: NonNull<BitmapCommand>,
|
||||
) -> *mut Packet {
|
||||
heap_move_ok(unsafe { heap_remove(command) }.try_into())
|
||||
}
|
||||
);
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
use crate::{
|
||||
commands::wrap_command,
|
||||
macros::{wrap_fields, wrap_functions},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
|
||||
mem::{heap_move_nonnull, heap_remove},
|
||||
};
|
||||
use servicepoint::{
|
||||
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset,
|
||||
Packet,
|
||||
};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
|
@ -46,13 +45,4 @@ wrap_functions!(associate BitVecCommand;
|
|||
})
|
||||
}
|
||||
|
||||
/// Tries to turn a [BitVecCommand] into a [Packet].
|
||||
///
|
||||
/// Returns: NULL or a [Packet] containing the command.
|
||||
fn try_into_packet(
|
||||
command: NonNull<BitVecCommand>,
|
||||
) -> *mut Packet {
|
||||
heap_move_ok(unsafe { heap_remove(command) }.try_into())
|
||||
}
|
||||
|
||||
);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use crate::{
|
||||
commands::{wrap_command, wrap_origin_accessors},
|
||||
macros::{wrap_fields, wrap_functions},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
|
||||
mem::{heap_move_nonnull, heap_remove},
|
||||
};
|
||||
use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin, Packet};
|
||||
use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
wrap_command!(BrightnessGrid);
|
||||
|
@ -40,13 +40,4 @@ wrap_functions!(associate BrightnessGridCommand;
|
|||
heap_move_nonnull(unsafe { heap_remove(grid) }.into())
|
||||
}
|
||||
|
||||
/// Tries to turn a [BrightnessGridCommand] into a [Packet].
|
||||
///
|
||||
/// Returns: NULL or a [Packet] containing the command.
|
||||
fn into_packet(
|
||||
command: NonNull<BrightnessGridCommand>,
|
||||
) -> *mut Packet {
|
||||
heap_move_ok(unsafe { heap_remove(command) }.try_into())
|
||||
}
|
||||
|
||||
);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use crate::{
|
||||
commands::{wrap_command, wrap_origin_accessors},
|
||||
macros::{wrap_fields, wrap_functions},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
|
||||
mem::{heap_move_nonnull, heap_remove},
|
||||
};
|
||||
use servicepoint::{CharGrid, CharGridCommand, Origin, Packet};
|
||||
use servicepoint::{CharGrid, CharGridCommand, Origin};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
wrap_command!(CharGrid);
|
||||
|
@ -40,13 +40,4 @@ wrap_functions!(cmd_chargrid;
|
|||
heap_move_nonnull(unsafe { heap_remove(grid) }.into())
|
||||
}
|
||||
|
||||
/// Tries to turn a [CharGridCommand] into a [Packet].
|
||||
///
|
||||
/// Returns: NULL or a [Packet] containing the command.
|
||||
fn try_into_packet(
|
||||
command: NonNull<CharGridCommand>,
|
||||
) -> *mut Packet {
|
||||
heap_move_ok(unsafe { heap_remove(command) }.try_into())
|
||||
}
|
||||
|
||||
);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use crate::{
|
||||
commands::{wrap_command, wrap_origin_accessors},
|
||||
macros::{wrap_fields, wrap_functions},
|
||||
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
|
||||
mem::{heap_move_nonnull, heap_remove},
|
||||
};
|
||||
use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet};
|
||||
use servicepoint::{Cp437Grid, Cp437GridCommand, Origin};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
wrap_command!(Cp437Grid);
|
||||
|
@ -40,13 +40,4 @@ wrap_functions!(cmd_cp437grid;
|
|||
heap_move_nonnull(unsafe { heap_remove(grid) }.into())
|
||||
}
|
||||
|
||||
/// Tries to turn a [Cp437GridCommand] into a [Packet].
|
||||
///
|
||||
/// Returns: NULL or a [Packet] containing the command.
|
||||
fn try_into_packet(
|
||||
command: NonNull<Cp437GridCommand>,
|
||||
) -> *mut Packet {
|
||||
heap_move_ok(unsafe { heap_remove(command) }.try_into())
|
||||
}
|
||||
|
||||
);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use crate::{
|
||||
commands::wrap_command,
|
||||
macros::{wrap_fields, wrap_functions},
|
||||
mem::{heap_move_nonnull, heap_remove},
|
||||
mem::heap_move_nonnull,
|
||||
};
|
||||
use servicepoint::{Brightness, GlobalBrightnessCommand, Packet};
|
||||
use servicepoint::{Brightness, GlobalBrightnessCommand};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
wrap_functions!(associate GlobalBrightnessCommand;
|
||||
|
@ -15,11 +15,6 @@ wrap_functions!(associate GlobalBrightnessCommand;
|
|||
heap_move_nonnull(GlobalBrightnessCommand::from(brightness))
|
||||
}
|
||||
|
||||
/// Turns the command into a packet
|
||||
fn into_packet(command: NonNull<GlobalBrightnessCommand>) -> NonNull<Packet> {
|
||||
heap_move_nonnull(unsafe { heap_remove(command) }.into())
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
wrap_command!(GlobalBrightness);
|
||||
|
|
|
@ -64,11 +64,29 @@ macro_rules! derive_command_from {
|
|||
};
|
||||
}
|
||||
|
||||
macro_rules! derive_command_into_packet {
|
||||
($command_type:ident) => {
|
||||
::paste::paste! {
|
||||
wrap_functions!(associate $command_type;
|
||||
#[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."]
|
||||
///
|
||||
/// Returns: NULL or a [Packet] containing the command.
|
||||
fn try_into_packet(
|
||||
command: NonNull<$command_type>,
|
||||
) -> *mut ::servicepoint::Packet {
|
||||
$crate::mem::heap_move_ok(unsafe { $crate::mem::heap_remove(command) }.try_into())
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! wrap_command {
|
||||
($command:ident, $object_type:ident) => {
|
||||
$crate::macros::wrap_clone!($object_type);
|
||||
$crate::macros::wrap_free!($object_type);
|
||||
$crate::commands::derive_command_from!($command);
|
||||
$crate::commands::derive_command_into_packet!($object_type);
|
||||
};
|
||||
($command:ident) => {
|
||||
::paste::paste! {
|
||||
|
@ -77,4 +95,7 @@ macro_rules! wrap_command {
|
|||
};
|
||||
}
|
||||
|
||||
pub(crate) use {derive_command_from, wrap_command, wrap_origin_accessors};
|
||||
pub(crate) use {
|
||||
derive_command_from, derive_command_into_packet, wrap_command,
|
||||
wrap_origin_accessors,
|
||||
};
|
||||
|
|
|
@ -36,7 +36,10 @@ impl ByteSlice {
|
|||
unsafe { std::slice::from_raw_parts(self.start, self.length) }
|
||||
}
|
||||
|
||||
#[allow(clippy::mut_from_ref, reason = "This is used to get a pointer from the C side")]
|
||||
#[allow(
|
||||
clippy::mut_from_ref,
|
||||
reason = "This is used to get a pointer from the C side."
|
||||
)]
|
||||
pub(crate) unsafe fn as_slice_mut(&self) -> &mut [u8] {
|
||||
unsafe { std::slice::from_raw_parts_mut(self.start, self.length) }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue