cargo fmt

This commit is contained in:
Vinzenz Schroeter 2025-04-11 22:00:01 +02:00
parent 69bed7c665
commit 2ab80d395e
9 changed files with 149 additions and 102 deletions

View file

@ -3,7 +3,7 @@
//! prefix `sp_bitmap_` //! prefix `sp_bitmap_`
use servicepoint::{DataRef, Grid}; use servicepoint::{DataRef, Grid};
use std::ptr::{NonNull}; use std::ptr::NonNull;
use crate::byte_slice::SPByteSlice; use crate::byte_slice::SPByteSlice;
@ -47,7 +47,7 @@ pub unsafe extern "C" fn sp_bitmap_new(
) -> *mut SPBitmap { ) -> *mut SPBitmap {
if let Some(bitmap) = servicepoint::Bitmap::new(width, height) { if let Some(bitmap) = servicepoint::Bitmap::new(width, height) {
Box::leak(Box::new(SPBitmap(bitmap))) Box::leak(Box::new(SPBitmap(bitmap)))
}else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
} }
@ -103,10 +103,10 @@ pub unsafe extern "C" fn sp_bitmap_load(
data_length: usize, data_length: usize,
) -> *mut SPBitmap { ) -> *mut SPBitmap {
assert!(!data.is_null()); assert!(!data.is_null());
let data = unsafe {std::slice::from_raw_parts(data, data_length)}; let data = unsafe { std::slice::from_raw_parts(data, data_length) };
if let Ok(bitmap) = servicepoint::Bitmap::load(width, height, data) { if let Ok(bitmap) = servicepoint::Bitmap::load(width, height, data) {
Box::leak(Box::new(SPBitmap(bitmap))) Box::leak(Box::new(SPBitmap(bitmap)))
}else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
} }
@ -132,7 +132,7 @@ pub unsafe extern "C" fn sp_bitmap_clone(
bitmap: *const SPBitmap, bitmap: *const SPBitmap,
) -> NonNull<SPBitmap> { ) -> NonNull<SPBitmap> {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
let result = Box::new(SPBitmap(unsafe {(*bitmap).0.clone()})); let result = Box::new(SPBitmap(unsafe { (*bitmap).0.clone() }));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -154,7 +154,7 @@ pub unsafe extern "C" fn sp_bitmap_clone(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut SPBitmap) { pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut SPBitmap) {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
_ = unsafe {Box::from_raw(bitmap)}; _ = unsafe { Box::from_raw(bitmap) };
} }
/// Gets the current value at the specified position in the [SPBitmap]. /// Gets the current value at the specified position in the [SPBitmap].
@ -182,7 +182,7 @@ pub unsafe extern "C" fn sp_bitmap_get(
y: usize, y: usize,
) -> bool { ) -> bool {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
unsafe {(*bitmap).0.get(x, y)} unsafe { (*bitmap).0.get(x, y) }
} }
/// Sets the value of the specified position in the [SPBitmap]. /// Sets the value of the specified position in the [SPBitmap].
@ -214,7 +214,7 @@ pub unsafe extern "C" fn sp_bitmap_set(
value: bool, value: bool,
) { ) {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
unsafe {(*bitmap).0.set(x, y, value)}; unsafe { (*bitmap).0.set(x, y, value) };
} }
/// Sets the state of all pixels in the [SPBitmap]. /// Sets the state of all pixels in the [SPBitmap].
@ -237,7 +237,7 @@ pub unsafe extern "C" fn sp_bitmap_set(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut SPBitmap, value: bool) { pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut SPBitmap, value: bool) {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
unsafe {(*bitmap).0.fill(value)}; unsafe { (*bitmap).0.fill(value) };
} }
/// Gets the width in pixels of the [SPBitmap] instance. /// Gets the width in pixels of the [SPBitmap] instance.
@ -258,7 +258,7 @@ pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut SPBitmap, value: bool) {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const SPBitmap) -> usize { pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const SPBitmap) -> usize {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
unsafe {(*bitmap).0.width()} unsafe { (*bitmap).0.width() }
} }
/// Gets the height in pixels of the [SPBitmap] instance. /// Gets the height in pixels of the [SPBitmap] instance.
@ -279,7 +279,7 @@ pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const SPBitmap) -> usize {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_height(bitmap: *const SPBitmap) -> usize { pub unsafe extern "C" fn sp_bitmap_height(bitmap: *const SPBitmap) -> usize {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
unsafe {(*bitmap).0.height()} unsafe { (*bitmap).0.height() }
} }
/// Gets an unsafe reference to the data of the [SPBitmap] instance. /// Gets an unsafe reference to the data of the [SPBitmap] instance.
@ -300,7 +300,7 @@ pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref(
bitmap: *mut SPBitmap, bitmap: *mut SPBitmap,
) -> SPByteSlice { ) -> SPByteSlice {
assert!(!bitmap.is_null()); assert!(!bitmap.is_null());
let data = unsafe {(*bitmap).0.data_ref_mut()}; let data = unsafe { (*bitmap).0.data_ref_mut() };
SPByteSlice { SPByteSlice {
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(), start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
length: data.len(), length: data.len(),

View file

@ -79,7 +79,7 @@ pub unsafe extern "C" fn sp_bitvec_load(
data_length: usize, data_length: usize,
) -> NonNull<SPBitVec> { ) -> NonNull<SPBitVec> {
assert!(!data.is_null()); assert!(!data.is_null());
let data = unsafe {std::slice::from_raw_parts(data, data_length)}; let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let result = Box::new(SPBitVec(servicepoint::BitVec::from_slice(data))); let result = Box::new(SPBitVec(servicepoint::BitVec::from_slice(data)));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -105,7 +105,7 @@ pub unsafe extern "C" fn sp_bitvec_clone(
bit_vec: *const SPBitVec, bit_vec: *const SPBitVec,
) -> NonNull<SPBitVec> { ) -> NonNull<SPBitVec> {
assert!(!bit_vec.is_null()); assert!(!bit_vec.is_null());
let result = Box::new(unsafe {(*bit_vec).clone()}); let result = Box::new(unsafe { (*bit_vec).clone() });
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -127,7 +127,7 @@ pub unsafe extern "C" fn sp_bitvec_clone(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitvec_free(bit_vec: *mut SPBitVec) { pub unsafe extern "C" fn sp_bitvec_free(bit_vec: *mut SPBitVec) {
assert!(!bit_vec.is_null()); assert!(!bit_vec.is_null());
_ = unsafe {Box::from_raw(bit_vec)}; _ = unsafe { Box::from_raw(bit_vec) };
} }
/// Gets the value of a bit from the [SPBitVec]. /// Gets the value of a bit from the [SPBitVec].
@ -156,7 +156,7 @@ pub unsafe extern "C" fn sp_bitvec_get(
index: usize, index: usize,
) -> bool { ) -> bool {
assert!(!bit_vec.is_null()); assert!(!bit_vec.is_null());
unsafe {*(*bit_vec).0.get(index).unwrap()} unsafe { *(*bit_vec).0.get(index).unwrap() }
} }
/// Sets the value of a bit in the [SPBitVec]. /// Sets the value of a bit in the [SPBitVec].
@ -185,7 +185,7 @@ pub unsafe extern "C" fn sp_bitvec_set(
value: bool, value: bool,
) { ) {
assert!(!bit_vec.is_null()); assert!(!bit_vec.is_null());
unsafe {(*bit_vec).0.set(index, value)} unsafe { (*bit_vec).0.set(index, value) }
} }
/// Sets the value of all bits in the [SPBitVec]. /// Sets the value of all bits in the [SPBitVec].
@ -208,7 +208,7 @@ pub unsafe extern "C" fn sp_bitvec_set(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitvec_fill(bit_vec: *mut SPBitVec, value: bool) { pub unsafe extern "C" fn sp_bitvec_fill(bit_vec: *mut SPBitVec, value: bool) {
assert!(!bit_vec.is_null()); assert!(!bit_vec.is_null());
unsafe {(*bit_vec).0.fill(value)} unsafe { (*bit_vec).0.fill(value) }
} }
/// Gets the length of the [SPBitVec] in bits. /// Gets the length of the [SPBitVec] in bits.
@ -229,7 +229,7 @@ pub unsafe extern "C" fn sp_bitvec_fill(bit_vec: *mut SPBitVec, value: bool) {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitvec_len(bit_vec: *const SPBitVec) -> usize { pub unsafe extern "C" fn sp_bitvec_len(bit_vec: *const SPBitVec) -> usize {
assert!(!bit_vec.is_null()); assert!(!bit_vec.is_null());
unsafe {(*bit_vec).0.len()} unsafe { (*bit_vec).0.len() }
} }
/// Returns true if length is 0. /// Returns true if length is 0.
@ -250,7 +250,7 @@ pub unsafe extern "C" fn sp_bitvec_len(bit_vec: *const SPBitVec) -> usize {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitvec_is_empty(bit_vec: *const SPBitVec) -> bool { pub unsafe extern "C" fn sp_bitvec_is_empty(bit_vec: *const SPBitVec) -> bool {
assert!(!bit_vec.is_null()); assert!(!bit_vec.is_null());
unsafe {(*bit_vec).0.is_empty()} unsafe { (*bit_vec).0.is_empty() }
} }
/// Gets an unsafe reference to the data of the [SPBitVec] instance. /// Gets an unsafe reference to the data of the [SPBitVec] instance.
@ -275,7 +275,7 @@ pub unsafe extern "C" fn sp_bitvec_unsafe_data_ref(
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
) -> SPByteSlice { ) -> SPByteSlice {
assert!(!bit_vec.is_null()); assert!(!bit_vec.is_null());
let data = unsafe {(*bit_vec).0.as_raw_mut_slice()}; let data = unsafe { (*bit_vec).0.as_raw_mut_slice() };
SPByteSlice { SPByteSlice {
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(), start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
length: data.len(), length: data.len(),

View file

@ -79,14 +79,14 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
data_length: usize, data_length: usize,
) -> *mut SPBrightnessGrid { ) -> *mut SPBrightnessGrid {
assert!(!data.is_null()); assert!(!data.is_null());
let data =unsafe { std::slice::from_raw_parts(data, data_length)}; let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let grid = match servicepoint::ByteGrid::load(width, height, data) { let grid = match servicepoint::ByteGrid::load(width, height, data) {
None => return std::ptr::null_mut(), None => return std::ptr::null_mut(),
Some(grid) => grid Some(grid) => grid,
}; };
if let Ok(grid )= servicepoint::BrightnessGrid::try_from(grid) { if let Ok(grid) = servicepoint::BrightnessGrid::try_from(grid) {
Box::leak(Box::new(SPBrightnessGrid(grid))) Box::leak(Box::new(SPBrightnessGrid(grid)))
}else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
} }
@ -116,7 +116,7 @@ pub unsafe extern "C" fn sp_brightness_grid_clone(
brightness_grid: *const SPBrightnessGrid, brightness_grid: *const SPBrightnessGrid,
) -> NonNull<SPBrightnessGrid> { ) -> NonNull<SPBrightnessGrid> {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
let result = Box::new(unsafe {(*brightness_grid).clone()}); let result = Box::new(unsafe { (*brightness_grid).clone() });
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -144,7 +144,7 @@ pub unsafe extern "C" fn sp_brightness_grid_free(
brightness_grid: *mut SPBrightnessGrid, brightness_grid: *mut SPBrightnessGrid,
) { ) {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
_ = unsafe {Box::from_raw(brightness_grid)}; _ = unsafe { Box::from_raw(brightness_grid) };
} }
/// Gets the current value at the specified position. /// Gets the current value at the specified position.
@ -174,7 +174,7 @@ pub unsafe extern "C" fn sp_brightness_grid_get(
y: usize, y: usize,
) -> u8 { ) -> u8 {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
unsafe {(*brightness_grid).0.get(x, y)}.into() unsafe { (*brightness_grid).0.get(x, y) }.into()
} }
/// Sets the value of the specified position in the [SPBrightnessGrid]. /// Sets the value of the specified position in the [SPBrightnessGrid].
@ -209,7 +209,7 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
let brightness = servicepoint::Brightness::try_from(value) let brightness = servicepoint::Brightness::try_from(value)
.expect("invalid brightness value"); .expect("invalid brightness value");
unsafe {(*brightness_grid).0.set(x, y, brightness)}; unsafe { (*brightness_grid).0.set(x, y, brightness) };
} }
/// Sets the value of all cells in the [SPBrightnessGrid]. /// Sets the value of all cells in the [SPBrightnessGrid].
@ -238,7 +238,7 @@ pub unsafe extern "C" fn sp_brightness_grid_fill(
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
let brightness = servicepoint::Brightness::try_from(value) let brightness = servicepoint::Brightness::try_from(value)
.expect("invalid brightness value"); .expect("invalid brightness value");
unsafe {(*brightness_grid).0.fill(brightness)}; unsafe { (*brightness_grid).0.fill(brightness) };
} }
/// Gets the width of the [SPBrightnessGrid] instance. /// Gets the width of the [SPBrightnessGrid] instance.
@ -263,7 +263,7 @@ pub unsafe extern "C" fn sp_brightness_grid_width(
brightness_grid: *const SPBrightnessGrid, brightness_grid: *const SPBrightnessGrid,
) -> usize { ) -> usize {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
unsafe {(*brightness_grid).0.width()} unsafe { (*brightness_grid).0.width() }
} }
/// Gets the height of the [SPBrightnessGrid] instance. /// Gets the height of the [SPBrightnessGrid] instance.
@ -288,7 +288,7 @@ pub unsafe extern "C" fn sp_brightness_grid_height(
brightness_grid: *const SPBrightnessGrid, brightness_grid: *const SPBrightnessGrid,
) -> usize { ) -> usize {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
unsafe {(*brightness_grid).0.height()} unsafe { (*brightness_grid).0.height() }
} }
/// Gets an unsafe reference to the data of the [SPBrightnessGrid] instance. /// Gets an unsafe reference to the data of the [SPBrightnessGrid] instance.
@ -316,9 +316,9 @@ pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref(
) -> SPByteSlice { ) -> SPByteSlice {
assert!(!brightness_grid.is_null()); assert!(!brightness_grid.is_null());
assert_eq!(core::mem::size_of::<servicepoint::Brightness>(), 1); assert_eq!(core::mem::size_of::<servicepoint::Brightness>(), 1);
let data = unsafe {(*brightness_grid).0.data_ref_mut()}; let data = unsafe { (*brightness_grid).0.data_ref_mut() };
// this assumes more about the memory layout than rust guarantees. yikes! // this assumes more about the memory layout than rust guarantees. yikes!
let data: &mut [u8] = unsafe {transmute(data)}; let data: &mut [u8] = unsafe { transmute(data) };
SPByteSlice { SPByteSlice {
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(), start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
length: data.len(), length: data.len(),

View file

@ -74,7 +74,7 @@ pub unsafe extern "C" fn sp_char_grid_load(
data_length: usize, data_length: usize,
) -> NonNull<SPCharGrid> { ) -> NonNull<SPCharGrid> {
assert!(data.is_null()); assert!(data.is_null());
let data = unsafe {std::slice::from_raw_parts(data, data_length)}; let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let result = Box::new(SPCharGrid( let result = Box::new(SPCharGrid(
servicepoint::CharGrid::load_utf8(width, height, data.to_vec()) servicepoint::CharGrid::load_utf8(width, height, data.to_vec())
.unwrap(), .unwrap(),
@ -103,7 +103,7 @@ pub unsafe extern "C" fn sp_char_grid_clone(
char_grid: *const SPCharGrid, char_grid: *const SPCharGrid,
) -> NonNull<SPCharGrid> { ) -> NonNull<SPCharGrid> {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
let result = Box::new(unsafe{(*char_grid).clone()}); let result = Box::new(unsafe { (*char_grid).clone() });
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -125,7 +125,7 @@ pub unsafe extern "C" fn sp_char_grid_clone(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut SPCharGrid) { pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut SPCharGrid) {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
_ = unsafe {Box::from_raw(char_grid)}; _ = unsafe { Box::from_raw(char_grid) };
} }
/// Gets the current value at the specified position. /// Gets the current value at the specified position.
@ -153,7 +153,7 @@ pub unsafe extern "C" fn sp_char_grid_get(
y: usize, y: usize,
) -> u32 { ) -> u32 {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
unsafe {(*char_grid).0.get(x, y) as u32} unsafe { (*char_grid).0.get(x, y) as u32 }
} }
/// Sets the value of the specified position in the [SPCharGrid]. /// Sets the value of the specified position in the [SPCharGrid].
@ -187,7 +187,7 @@ pub unsafe extern "C" fn sp_char_grid_set(
value: u32, value: u32,
) { ) {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
unsafe {(*char_grid).0.set(x, y, char::from_u32(value).unwrap())}; unsafe { (*char_grid).0.set(x, y, char::from_u32(value).unwrap()) };
} }
/// Sets the value of all cells in the [SPCharGrid]. /// Sets the value of all cells in the [SPCharGrid].
@ -213,7 +213,7 @@ pub unsafe extern "C" fn sp_char_grid_fill(
value: u32, value: u32,
) { ) {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
unsafe {(*char_grid).0.fill(char::from_u32(value).unwrap())}; unsafe { (*char_grid).0.fill(char::from_u32(value).unwrap()) };
} }
/// Gets the width of the [SPCharGrid] instance. /// Gets the width of the [SPCharGrid] instance.
@ -236,7 +236,7 @@ pub unsafe extern "C" fn sp_char_grid_width(
char_grid: *const SPCharGrid, char_grid: *const SPCharGrid,
) -> usize { ) -> usize {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
unsafe {(*char_grid).0.width()} unsafe { (*char_grid).0.width() }
} }
/// Gets the height of the [SPCharGrid] instance. /// Gets the height of the [SPCharGrid] instance.
@ -259,5 +259,5 @@ pub unsafe extern "C" fn sp_char_grid_height(
char_grid: *const SPCharGrid, char_grid: *const SPCharGrid,
) -> usize { ) -> usize {
assert!(!char_grid.is_null()); assert!(!char_grid.is_null());
unsafe {(*char_grid).0.height()} unsafe { (*char_grid).0.height() }
} }

View file

@ -54,7 +54,7 @@ impl Clone for SPCommand {
pub unsafe extern "C" fn sp_command_try_from_packet( pub unsafe extern "C" fn sp_command_try_from_packet(
packet: *mut SPPacket, packet: *mut SPPacket,
) -> *mut SPCommand { ) -> *mut SPCommand {
let packet = *unsafe{Box::from_raw(packet)}; let packet = *unsafe { Box::from_raw(packet) };
match servicepoint::TypedCommand::try_from(packet.0) { match servicepoint::TypedCommand::try_from(packet.0) {
Err(_) => std::ptr::null_mut(), Err(_) => std::ptr::null_mut(),
Ok(command) => Box::into_raw(Box::new(SPCommand(command))), Ok(command) => Box::into_raw(Box::new(SPCommand(command))),
@ -164,7 +164,8 @@ pub unsafe extern "C" fn sp_command_brightness(
) -> NonNull<SPCommand> { ) -> NonNull<SPCommand> {
let brightness = servicepoint::Brightness::try_from(brightness) let brightness = servicepoint::Brightness::try_from(brightness)
.expect("invalid brightness"); .expect("invalid brightness");
let result = Box::new(SPCommand(GlobalBrightnessCommand::from(brightness).into())); let result =
Box::new(SPCommand(GlobalBrightnessCommand::from(brightness).into()));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -194,10 +195,13 @@ pub unsafe extern "C" fn sp_command_char_brightness(
) -> NonNull<SPCommand> { ) -> NonNull<SPCommand> {
assert!(!grid.is_null()); assert!(!grid.is_null());
let byte_grid = unsafe { *Box::from_raw(grid) }; let byte_grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(SPCommand(servicepoint::BrightnessGridCommand { let result = Box::new(SPCommand(
origin: servicepoint::Origin::new(x, y), servicepoint::BrightnessGridCommand {
grid: byte_grid.0, origin: servicepoint::Origin::new(x, y),
}.into())); grid: byte_grid.0,
}
.into(),
));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -232,7 +236,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Overwrite)} unsafe {
sp_command_bitmap_linear_internal(
offset,
bit_vec,
compression,
BinaryOperation::Overwrite,
)
}
} }
/// Set pixel data according to an and-mask starting at the offset. /// Set pixel data according to an and-mask starting at the offset.
@ -266,7 +277,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Xor)} unsafe {
sp_command_bitmap_linear_internal(
offset,
bit_vec,
compression,
BinaryOperation::Xor,
)
}
} }
/// Set pixel data according to an or-mask starting at the offset. /// Set pixel data according to an or-mask starting at the offset.
@ -300,7 +318,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Or)} unsafe {
sp_command_bitmap_linear_internal(
offset,
bit_vec,
compression,
BinaryOperation::Or,
)
}
} }
/// Set pixel data according to a xor-mask starting at the offset. /// Set pixel data according to a xor-mask starting at the offset.
@ -334,7 +359,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
unsafe {sp_command_bitmap_linear_internal(offset, bit_vec, compression, BinaryOperation::Xor)} unsafe {
sp_command_bitmap_linear_internal(
offset,
bit_vec,
compression,
BinaryOperation::Xor,
)
}
} }
#[inline] #[inline]
@ -342,7 +374,7 @@ unsafe fn sp_command_bitmap_linear_internal(
offset: usize, offset: usize,
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
operation: BinaryOperation operation: BinaryOperation,
) -> *mut SPCommand { ) -> *mut SPCommand {
assert!(!bit_vec.is_null()); assert!(!bit_vec.is_null());
let bit_vec = unsafe { *Box::from_raw(bit_vec) }; let bit_vec = unsafe { *Box::from_raw(bit_vec) };
@ -350,12 +382,15 @@ unsafe fn sp_command_bitmap_linear_internal(
Ok(compression) => compression, Ok(compression) => compression,
Err(_) => return std::ptr::null_mut(), Err(_) => return std::ptr::null_mut(),
}; };
let command = SPCommand(servicepoint::BitVecCommand { let command = SPCommand(
offset, servicepoint::BitVecCommand {
operation, offset,
bitvec: bit_vec.into(), operation,
compression, bitvec: bit_vec.into(),
}.into()); compression,
}
.into(),
);
Box::leak(Box::new(command)) Box::leak(Box::new(command))
} }
@ -384,12 +419,14 @@ pub unsafe extern "C" fn sp_command_cp437_data(
grid: *mut SPCp437Grid, grid: *mut SPCp437Grid,
) -> NonNull<SPCommand> { ) -> NonNull<SPCommand> {
assert!(!grid.is_null()); assert!(!grid.is_null());
let grid = *unsafe{Box::from_raw(grid)}; let grid = *unsafe { Box::from_raw(grid) };
let result = Box::new(SPCommand(servicepoint::Cp437GridCommand { let result = Box::new(SPCommand(
origin: servicepoint::Origin::new(x, servicepoint::Cp437GridCommand {
y), origin: servicepoint::Origin::new(x, y),
grid: grid.0, grid: grid.0,
}.into())); }
.into(),
));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -419,11 +456,13 @@ pub unsafe extern "C" fn sp_command_utf8_data(
) -> NonNull<SPCommand> { ) -> NonNull<SPCommand> {
assert!(!grid.is_null()); assert!(!grid.is_null());
let grid = unsafe { *Box::from_raw(grid) }; let grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(SPCommand(servicepoint::CharGridCommand { let result = Box::new(SPCommand(
origin: servicepoint::Origin::new(x, servicepoint::CharGridCommand {
y), origin: servicepoint::Origin::new(x, y),
grid: grid.0, grid: grid.0,
}.into())); }
.into(),
));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -460,11 +499,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
Ok(compression) => compression, Ok(compression) => compression,
Err(_) => return std::ptr::null_mut(), Err(_) => return std::ptr::null_mut(),
}; };
let command = SPCommand(servicepoint::BitmapCommand { let command = SPCommand(
origin: servicepoint::Origin::new(x, y), servicepoint::BitmapCommand {
bitmap, origin: servicepoint::Origin::new(x, y),
compression, bitmap,
}.into()); compression,
}
.into(),
);
Box::leak(Box::new(command)) Box::leak(Box::new(command))
} }

View file

@ -2,9 +2,9 @@
//! //!
//! prefix `sp_connection_` //! prefix `sp_connection_`
use std::ffi::{c_char, CStr};
use servicepoint::Connection;
use crate::{SPCommand, SPPacket}; use crate::{SPCommand, SPPacket};
use servicepoint::Connection;
use std::ffi::{c_char, CStr};
/// A connection to the display. /// A connection to the display.
/// ///
@ -36,7 +36,9 @@ pub unsafe extern "C" fn sp_connection_open(
host: *const c_char, host: *const c_char,
) -> *mut SPConnection { ) -> *mut SPConnection {
assert!(!host.is_null()); assert!(!host.is_null());
let host = unsafe {CStr::from_ptr(host)}.to_str().expect("Bad encoding"); let host = unsafe { CStr::from_ptr(host) }
.to_str()
.expect("Bad encoding");
let connection = match servicepoint::UdpConnection::open(host) { let connection = match servicepoint::UdpConnection::open(host) {
Err(_) => return std::ptr::null_mut(), Err(_) => return std::ptr::null_mut(),
Ok(value) => value, Ok(value) => value,
@ -86,8 +88,8 @@ pub unsafe extern "C" fn sp_connection_send_packet(
) -> bool { ) -> bool {
assert!(!connection.is_null()); assert!(!connection.is_null());
assert!(!packet.is_null()); assert!(!packet.is_null());
let packet = unsafe {Box::from_raw(packet)}; let packet = unsafe { Box::from_raw(packet) };
unsafe {(*connection).0.send((*packet).0)}.is_ok() unsafe { (*connection).0.send((*packet).0) }.is_ok()
} }
/// Sends a [SPCommand] to the display using the [SPConnection]. /// Sends a [SPCommand] to the display using the [SPConnection].
@ -115,8 +117,8 @@ pub unsafe extern "C" fn sp_connection_send_command(
) -> bool { ) -> bool {
assert!(!connection.is_null()); assert!(!connection.is_null());
assert!(!command.is_null()); assert!(!command.is_null());
let command = (*unsafe {Box::from_raw(command)}).0; let command = (*unsafe { Box::from_raw(command) }).0;
unsafe{(*connection).0.send(command)}.is_ok() unsafe { (*connection).0.send(command) }.is_ok()
} }
/// Closes and deallocates a [SPConnection]. /// Closes and deallocates a [SPConnection].
@ -134,5 +136,5 @@ pub unsafe extern "C" fn sp_connection_send_command(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_connection_free(connection: *mut SPConnection) { pub unsafe extern "C" fn sp_connection_free(connection: *mut SPConnection) {
assert!(!connection.is_null()); assert!(!connection.is_null());
_ = unsafe {Box::from_raw(connection)}; _ = unsafe { Box::from_raw(connection) };
} }

View file

@ -43,6 +43,6 @@ impl TryFrom<SPCompressionCode> for CompressionCode {
type Error = (); type Error = ();
fn try_from(value: SPCompressionCode) -> Result<Self, Self::Error> { fn try_from(value: SPCompressionCode) -> Result<Self, Self::Error> {
CompressionCode::try_from(value as u16).map_err(|_|()) CompressionCode::try_from(value as u16).map_err(|_| ())
} }
} }

View file

@ -71,9 +71,9 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
data_length: usize, data_length: usize,
) -> *mut SPCp437Grid { ) -> *mut SPCp437Grid {
assert!(data.is_null()); assert!(data.is_null());
let data = unsafe{std::slice::from_raw_parts(data, data_length)}; let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let grid = servicepoint::Cp437Grid::load( width, height, data, ); let grid = servicepoint::Cp437Grid::load(width, height, data);
if let Some(grid ) = grid { if let Some(grid) = grid {
Box::leak(Box::new(SPCp437Grid(grid))) Box::leak(Box::new(SPCp437Grid(grid)))
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
@ -101,7 +101,7 @@ pub unsafe extern "C" fn sp_cp437_grid_clone(
cp437_grid: *const SPCp437Grid, cp437_grid: *const SPCp437Grid,
) -> NonNull<SPCp437Grid> { ) -> NonNull<SPCp437Grid> {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
let result = Box::new(unsafe {(*cp437_grid).clone()}); let result = Box::new(unsafe { (*cp437_grid).clone() });
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -123,7 +123,7 @@ pub unsafe extern "C" fn sp_cp437_grid_clone(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut SPCp437Grid) { pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut SPCp437Grid) {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
_ = unsafe {Box::from_raw(cp437_grid)}; _ = unsafe { Box::from_raw(cp437_grid) };
} }
/// Gets the current value at the specified position. /// Gets the current value at the specified position.
@ -151,7 +151,7 @@ pub unsafe extern "C" fn sp_cp437_grid_get(
y: usize, y: usize,
) -> u8 { ) -> u8 {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
unsafe{(*cp437_grid).0.get(x, y)} unsafe { (*cp437_grid).0.get(x, y) }
} }
/// Sets the value of the specified position in the [SPCp437Grid]. /// Sets the value of the specified position in the [SPCp437Grid].
@ -185,7 +185,7 @@ pub unsafe extern "C" fn sp_cp437_grid_set(
value: u8, value: u8,
) { ) {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
unsafe {(*cp437_grid).0.set(x, y, value)}; unsafe { (*cp437_grid).0.set(x, y, value) };
} }
/// Sets the value of all cells in the [SPCp437Grid]. /// Sets the value of all cells in the [SPCp437Grid].
@ -211,7 +211,7 @@ pub unsafe extern "C" fn sp_cp437_grid_fill(
value: u8, value: u8,
) { ) {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
unsafe {(*cp437_grid).0.fill(value)}; unsafe { (*cp437_grid).0.fill(value) };
} }
/// Gets the width of the [SPCp437Grid] instance. /// Gets the width of the [SPCp437Grid] instance.
@ -234,7 +234,7 @@ pub unsafe extern "C" fn sp_cp437_grid_width(
cp437_grid: *const SPCp437Grid, cp437_grid: *const SPCp437Grid,
) -> usize { ) -> usize {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
unsafe {(*cp437_grid).0.width()} unsafe { (*cp437_grid).0.width() }
} }
/// Gets the height of the [SPCp437Grid] instance. /// Gets the height of the [SPCp437Grid] instance.
@ -257,7 +257,7 @@ pub unsafe extern "C" fn sp_cp437_grid_height(
cp437_grid: *const SPCp437Grid, cp437_grid: *const SPCp437Grid,
) -> usize { ) -> usize {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
unsafe {(*cp437_grid).0.height()} unsafe { (*cp437_grid).0.height() }
} }
/// Gets an unsafe reference to the data of the [SPCp437Grid] instance. /// Gets an unsafe reference to the data of the [SPCp437Grid] instance.
@ -279,7 +279,7 @@ pub unsafe extern "C" fn sp_cp437_grid_height(
pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref( pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref(
cp437_grid: *mut SPCp437Grid, cp437_grid: *mut SPCp437Grid,
) -> SPByteSlice { ) -> SPByteSlice {
let data = unsafe {(*cp437_grid).0.data_ref_mut()}; let data = unsafe { (*cp437_grid).0.data_ref_mut() };
SPByteSlice { SPByteSlice {
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(), start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
length: data.len(), length: data.len(),

View file

@ -2,7 +2,7 @@
//! //!
//! prefix `sp_packet_` //! prefix `sp_packet_`
use std::ptr::{NonNull}; use std::ptr::NonNull;
use crate::SPCommand; use crate::SPCommand;
@ -31,10 +31,12 @@ pub unsafe extern "C" fn sp_packet_from_command(
command: *mut SPCommand, command: *mut SPCommand,
) -> *mut SPPacket { ) -> *mut SPPacket {
assert!(!command.is_null()); assert!(!command.is_null());
let command = unsafe {*Box::from_raw(command)}; let command = unsafe { *Box::from_raw(command) };
if let Ok(packet) = command.0.try_into() { if let Ok(packet) = command.0.try_into() {
Box::leak(Box::new(SPPacket(packet))) Box::leak(Box::new(SPPacket(packet)))
} else { std::ptr::null_mut() } } else {
std::ptr::null_mut()
}
} }
/// Tries to load a [SPPacket] from the passed array with the specified length. /// Tries to load a [SPPacket] from the passed array with the specified length.
@ -59,7 +61,7 @@ pub unsafe extern "C" fn sp_packet_try_load(
length: usize, length: usize,
) -> *mut SPPacket { ) -> *mut SPPacket {
assert!(!data.is_null()); assert!(!data.is_null());
let data = unsafe {std::slice::from_raw_parts(data, length)}; let data = unsafe { std::slice::from_raw_parts(data, length) };
match servicepoint::Packet::try_from(data) { match servicepoint::Packet::try_from(data) {
Err(_) => std::ptr::null_mut(), Err(_) => std::ptr::null_mut(),
Ok(packet) => Box::into_raw(Box::new(SPPacket(packet))), Ok(packet) => Box::into_raw(Box::new(SPPacket(packet))),
@ -105,7 +107,8 @@ pub unsafe extern "C" fn sp_packet_from_parts(
let payload = if payload.is_null() { let payload = if payload.is_null() {
vec![] vec![]
} else { } else {
let payload = unsafe {std::slice::from_raw_parts(payload, payload_len) }; let payload =
unsafe { std::slice::from_raw_parts(payload, payload_len) };
Vec::from(payload) Vec::from(payload)
}; };
@ -144,7 +147,7 @@ pub unsafe extern "C" fn sp_packet_clone(
packet: *const SPPacket, packet: *const SPPacket,
) -> NonNull<SPPacket> { ) -> NonNull<SPPacket> {
assert!(!packet.is_null()); assert!(!packet.is_null());
let result = Box::new(SPPacket(unsafe {(*packet).0.clone()})); let result = Box::new(SPPacket(unsafe { (*packet).0.clone() }));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -163,5 +166,5 @@ pub unsafe extern "C" fn sp_packet_clone(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_packet_free(packet: *mut SPPacket) { pub unsafe extern "C" fn sp_packet_free(packet: *mut SPPacket) {
assert!(!packet.is_null()); assert!(!packet.is_null());
_ = unsafe {Box::from_raw(packet)} _ = unsafe { Box::from_raw(packet) }
} }