cargo fmt
All checks were successful
Rust / build (pull_request) Successful in 1m53s

This commit is contained in:
Vinzenz Schroeter 2025-04-11 22:00:01 +02:00
parent 4221ebd406
commit 8fe4038a43
9 changed files with 149 additions and 102 deletions

View file

@ -3,7 +3,7 @@
//! prefix `sp_bitmap_`
use servicepoint::{DataRef, Grid};
use std::ptr::{NonNull};
use std::ptr::NonNull;
use crate::byte_slice::SPByteSlice;
@ -47,7 +47,7 @@ pub unsafe extern "C" fn sp_bitmap_new(
) -> *mut SPBitmap {
if let Some(bitmap) = servicepoint::Bitmap::new(width, height) {
Box::leak(Box::new(SPBitmap(bitmap)))
}else {
} else {
std::ptr::null_mut()
}
}
@ -103,10 +103,10 @@ pub unsafe extern "C" fn sp_bitmap_load(
data_length: usize,
) -> *mut SPBitmap {
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) {
Box::leak(Box::new(SPBitmap(bitmap)))
}else {
} else {
std::ptr::null_mut()
}
}
@ -132,7 +132,7 @@ pub unsafe extern "C" fn sp_bitmap_clone(
bitmap: *const SPBitmap,
) -> NonNull<SPBitmap> {
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))
}
@ -154,7 +154,7 @@ pub unsafe extern "C" fn sp_bitmap_clone(
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut SPBitmap) {
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].
@ -182,7 +182,7 @@ pub unsafe extern "C" fn sp_bitmap_get(
y: usize,
) -> bool {
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].
@ -214,7 +214,7 @@ pub unsafe extern "C" fn sp_bitmap_set(
value: bool,
) {
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].
@ -237,7 +237,7 @@ pub unsafe extern "C" fn sp_bitmap_set(
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_fill(bitmap: *mut SPBitmap, value: bool) {
assert!(!bitmap.is_null());
unsafe {(*bitmap).0.fill(value)};
unsafe { (*bitmap).0.fill(value) };
}
/// 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]
pub unsafe extern "C" fn sp_bitmap_width(bitmap: *const SPBitmap) -> usize {
assert!(!bitmap.is_null());
unsafe {(*bitmap).0.width()}
unsafe { (*bitmap).0.width() }
}
/// 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]
pub unsafe extern "C" fn sp_bitmap_height(bitmap: *const SPBitmap) -> usize {
assert!(!bitmap.is_null());
unsafe {(*bitmap).0.height()}
unsafe { (*bitmap).0.height() }
}
/// 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,
) -> SPByteSlice {
assert!(!bitmap.is_null());
let data = unsafe {(*bitmap).0.data_ref_mut()};
let data = unsafe { (*bitmap).0.data_ref_mut() };
SPByteSlice {
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
length: data.len(),

View file

@ -79,7 +79,7 @@ pub unsafe extern "C" fn sp_bitvec_load(
data_length: usize,
) -> NonNull<SPBitVec> {
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)));
NonNull::from(Box::leak(result))
}
@ -105,7 +105,7 @@ pub unsafe extern "C" fn sp_bitvec_clone(
bit_vec: *const SPBitVec,
) -> NonNull<SPBitVec> {
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))
}
@ -127,7 +127,7 @@ pub unsafe extern "C" fn sp_bitvec_clone(
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_free(bit_vec: *mut SPBitVec) {
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].
@ -156,7 +156,7 @@ pub unsafe extern "C" fn sp_bitvec_get(
index: usize,
) -> bool {
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].
@ -185,7 +185,7 @@ pub unsafe extern "C" fn sp_bitvec_set(
value: bool,
) {
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].
@ -208,7 +208,7 @@ pub unsafe extern "C" fn sp_bitvec_set(
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_fill(bit_vec: *mut SPBitVec, value: bool) {
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.
@ -229,7 +229,7 @@ pub unsafe extern "C" fn sp_bitvec_fill(bit_vec: *mut SPBitVec, value: bool) {
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_len(bit_vec: *const SPBitVec) -> usize {
assert!(!bit_vec.is_null());
unsafe {(*bit_vec).0.len()}
unsafe { (*bit_vec).0.len() }
}
/// 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]
pub unsafe extern "C" fn sp_bitvec_is_empty(bit_vec: *const SPBitVec) -> bool {
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.
@ -275,7 +275,7 @@ pub unsafe extern "C" fn sp_bitvec_unsafe_data_ref(
bit_vec: *mut SPBitVec,
) -> SPByteSlice {
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 {
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
length: data.len(),

View file

@ -79,14 +79,14 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
data_length: usize,
) -> *mut SPBrightnessGrid {
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) {
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)))
}else {
} else {
std::ptr::null_mut()
}
}
@ -116,7 +116,7 @@ pub unsafe extern "C" fn sp_brightness_grid_clone(
brightness_grid: *const SPBrightnessGrid,
) -> NonNull<SPBrightnessGrid> {
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))
}
@ -144,7 +144,7 @@ pub unsafe extern "C" fn sp_brightness_grid_free(
brightness_grid: *mut SPBrightnessGrid,
) {
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.
@ -174,7 +174,7 @@ pub unsafe extern "C" fn sp_brightness_grid_get(
y: usize,
) -> u8 {
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].
@ -209,7 +209,7 @@ pub unsafe extern "C" fn sp_brightness_grid_set(
assert!(!brightness_grid.is_null());
let brightness = servicepoint::Brightness::try_from(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].
@ -238,7 +238,7 @@ pub unsafe extern "C" fn sp_brightness_grid_fill(
assert!(!brightness_grid.is_null());
let brightness = servicepoint::Brightness::try_from(value)
.expect("invalid brightness value");
unsafe {(*brightness_grid).0.fill(brightness)};
unsafe { (*brightness_grid).0.fill(brightness) };
}
/// Gets the width of the [SPBrightnessGrid] instance.
@ -263,7 +263,7 @@ pub unsafe extern "C" fn sp_brightness_grid_width(
brightness_grid: *const SPBrightnessGrid,
) -> usize {
assert!(!brightness_grid.is_null());
unsafe {(*brightness_grid).0.width()}
unsafe { (*brightness_grid).0.width() }
}
/// Gets the height of the [SPBrightnessGrid] instance.
@ -288,7 +288,7 @@ pub unsafe extern "C" fn sp_brightness_grid_height(
brightness_grid: *const SPBrightnessGrid,
) -> usize {
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.
@ -316,9 +316,9 @@ pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref(
) -> SPByteSlice {
assert!(!brightness_grid.is_null());
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!
let data: &mut [u8] = unsafe {transmute(data)};
let data: &mut [u8] = unsafe { transmute(data) };
SPByteSlice {
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
length: data.len(),

View file

@ -74,7 +74,7 @@ pub unsafe extern "C" fn sp_char_grid_load(
data_length: usize,
) -> NonNull<SPCharGrid> {
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(
servicepoint::CharGrid::load_utf8(width, height, data.to_vec())
.unwrap(),
@ -103,7 +103,7 @@ pub unsafe extern "C" fn sp_char_grid_clone(
char_grid: *const SPCharGrid,
) -> NonNull<SPCharGrid> {
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))
}
@ -125,7 +125,7 @@ pub unsafe extern "C" fn sp_char_grid_clone(
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut SPCharGrid) {
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.
@ -153,7 +153,7 @@ pub unsafe extern "C" fn sp_char_grid_get(
y: usize,
) -> u32 {
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].
@ -187,7 +187,7 @@ pub unsafe extern "C" fn sp_char_grid_set(
value: u32,
) {
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].
@ -213,7 +213,7 @@ pub unsafe extern "C" fn sp_char_grid_fill(
value: u32,
) {
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.
@ -236,7 +236,7 @@ pub unsafe extern "C" fn sp_char_grid_width(
char_grid: *const SPCharGrid,
) -> usize {
assert!(!char_grid.is_null());
unsafe {(*char_grid).0.width()}
unsafe { (*char_grid).0.width() }
}
/// Gets the height of the [SPCharGrid] instance.
@ -259,5 +259,5 @@ pub unsafe extern "C" fn sp_char_grid_height(
char_grid: *const SPCharGrid,
) -> usize {
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(
packet: *mut SPPacket,
) -> *mut SPCommand {
let packet = *unsafe{Box::from_raw(packet)};
let packet = *unsafe { Box::from_raw(packet) };
match servicepoint::TypedCommand::try_from(packet.0) {
Err(_) => std::ptr::null_mut(),
Ok(command) => Box::into_raw(Box::new(SPCommand(command))),
@ -164,7 +164,8 @@ pub unsafe extern "C" fn sp_command_brightness(
) -> NonNull<SPCommand> {
let brightness = servicepoint::Brightness::try_from(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))
}
@ -194,10 +195,13 @@ pub unsafe extern "C" fn sp_command_char_brightness(
) -> NonNull<SPCommand> {
assert!(!grid.is_null());
let byte_grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(SPCommand(servicepoint::BrightnessGridCommand {
origin: servicepoint::Origin::new(x, y),
grid: byte_grid.0,
}.into()));
let result = Box::new(SPCommand(
servicepoint::BrightnessGridCommand {
origin: servicepoint::Origin::new(x, y),
grid: byte_grid.0,
}
.into(),
));
NonNull::from(Box::leak(result))
}
@ -232,7 +236,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *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.
@ -266,7 +277,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *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.
@ -300,7 +318,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *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.
@ -334,7 +359,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
) -> *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]
@ -342,7 +374,7 @@ unsafe fn sp_command_bitmap_linear_internal(
offset: usize,
bit_vec: *mut SPBitVec,
compression: SPCompressionCode,
operation: BinaryOperation
operation: BinaryOperation,
) -> *mut SPCommand {
assert!(!bit_vec.is_null());
let bit_vec = unsafe { *Box::from_raw(bit_vec) };
@ -350,12 +382,15 @@ unsafe fn sp_command_bitmap_linear_internal(
Ok(compression) => compression,
Err(_) => return std::ptr::null_mut(),
};
let command = SPCommand(servicepoint::BitVecCommand {
offset,
operation,
bitvec: bit_vec.into(),
compression,
}.into());
let command = SPCommand(
servicepoint::BitVecCommand {
offset,
operation,
bitvec: bit_vec.into(),
compression,
}
.into(),
);
Box::leak(Box::new(command))
}
@ -384,12 +419,14 @@ pub unsafe extern "C" fn sp_command_cp437_data(
grid: *mut SPCp437Grid,
) -> NonNull<SPCommand> {
assert!(!grid.is_null());
let grid = *unsafe{Box::from_raw(grid)};
let result = Box::new(SPCommand(servicepoint::Cp437GridCommand {
origin: servicepoint::Origin::new(x,
y),
grid: grid.0,
}.into()));
let grid = *unsafe { Box::from_raw(grid) };
let result = Box::new(SPCommand(
servicepoint::Cp437GridCommand {
origin: servicepoint::Origin::new(x, y),
grid: grid.0,
}
.into(),
));
NonNull::from(Box::leak(result))
}
@ -419,11 +456,13 @@ pub unsafe extern "C" fn sp_command_utf8_data(
) -> NonNull<SPCommand> {
assert!(!grid.is_null());
let grid = unsafe { *Box::from_raw(grid) };
let result = Box::new(SPCommand(servicepoint::CharGridCommand {
origin: servicepoint::Origin::new(x,
y),
grid: grid.0,
}.into()));
let result = Box::new(SPCommand(
servicepoint::CharGridCommand {
origin: servicepoint::Origin::new(x, y),
grid: grid.0,
}
.into(),
));
NonNull::from(Box::leak(result))
}
@ -460,11 +499,14 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
Ok(compression) => compression,
Err(_) => return std::ptr::null_mut(),
};
let command = SPCommand(servicepoint::BitmapCommand {
origin: servicepoint::Origin::new(x, y),
bitmap,
compression,
}.into());
let command = SPCommand(
servicepoint::BitmapCommand {
origin: servicepoint::Origin::new(x, y),
bitmap,
compression,
}
.into(),
);
Box::leak(Box::new(command))
}

View file

@ -2,9 +2,9 @@
//!
//! prefix `sp_connection_`
use std::ffi::{c_char, CStr};
use servicepoint::Connection;
use crate::{SPCommand, SPPacket};
use servicepoint::Connection;
use std::ffi::{c_char, CStr};
/// A connection to the display.
///
@ -36,7 +36,9 @@ pub unsafe extern "C" fn sp_connection_open(
host: *const c_char,
) -> *mut SPConnection {
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) {
Err(_) => return std::ptr::null_mut(),
Ok(value) => value,
@ -86,8 +88,8 @@ pub unsafe extern "C" fn sp_connection_send_packet(
) -> bool {
assert!(!connection.is_null());
assert!(!packet.is_null());
let packet = unsafe {Box::from_raw(packet)};
unsafe {(*connection).0.send((*packet).0)}.is_ok()
let packet = unsafe { Box::from_raw(packet) };
unsafe { (*connection).0.send((*packet).0) }.is_ok()
}
/// Sends a [SPCommand] to the display using the [SPConnection].
@ -115,8 +117,8 @@ pub unsafe extern "C" fn sp_connection_send_command(
) -> bool {
assert!(!connection.is_null());
assert!(!command.is_null());
let command = (*unsafe {Box::from_raw(command)}).0;
unsafe{(*connection).0.send(command)}.is_ok()
let command = (*unsafe { Box::from_raw(command) }).0;
unsafe { (*connection).0.send(command) }.is_ok()
}
/// Closes and deallocates a [SPConnection].
@ -134,5 +136,5 @@ pub unsafe extern "C" fn sp_connection_send_command(
#[no_mangle]
pub unsafe extern "C" fn sp_connection_free(connection: *mut SPConnection) {
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 = ();
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,
) -> *mut SPCp437Grid {
assert!(data.is_null());
let data = unsafe{std::slice::from_raw_parts(data, data_length)};
let grid = servicepoint::Cp437Grid::load( width, height, data, );
if let Some(grid ) = grid {
let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let grid = servicepoint::Cp437Grid::load(width, height, data);
if let Some(grid) = grid {
Box::leak(Box::new(SPCp437Grid(grid)))
} else {
std::ptr::null_mut()
@ -101,7 +101,7 @@ pub unsafe extern "C" fn sp_cp437_grid_clone(
cp437_grid: *const SPCp437Grid,
) -> NonNull<SPCp437Grid> {
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))
}
@ -123,7 +123,7 @@ pub unsafe extern "C" fn sp_cp437_grid_clone(
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut SPCp437Grid) {
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.
@ -151,7 +151,7 @@ pub unsafe extern "C" fn sp_cp437_grid_get(
y: usize,
) -> u8 {
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].
@ -185,7 +185,7 @@ pub unsafe extern "C" fn sp_cp437_grid_set(
value: u8,
) {
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].
@ -211,7 +211,7 @@ pub unsafe extern "C" fn sp_cp437_grid_fill(
value: u8,
) {
assert!(!cp437_grid.is_null());
unsafe {(*cp437_grid).0.fill(value)};
unsafe { (*cp437_grid).0.fill(value) };
}
/// Gets the width of the [SPCp437Grid] instance.
@ -234,7 +234,7 @@ pub unsafe extern "C" fn sp_cp437_grid_width(
cp437_grid: *const SPCp437Grid,
) -> usize {
assert!(!cp437_grid.is_null());
unsafe {(*cp437_grid).0.width()}
unsafe { (*cp437_grid).0.width() }
}
/// Gets the height of the [SPCp437Grid] instance.
@ -257,7 +257,7 @@ pub unsafe extern "C" fn sp_cp437_grid_height(
cp437_grid: *const SPCp437Grid,
) -> usize {
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.
@ -279,7 +279,7 @@ pub unsafe extern "C" fn sp_cp437_grid_height(
pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref(
cp437_grid: *mut SPCp437Grid,
) -> SPByteSlice {
let data = unsafe {(*cp437_grid).0.data_ref_mut()};
let data = unsafe { (*cp437_grid).0.data_ref_mut() };
SPByteSlice {
start: NonNull::new(data.as_mut_ptr_range().start).unwrap(),
length: data.len(),

View file

@ -2,7 +2,7 @@
//!
//! prefix `sp_packet_`
use std::ptr::{NonNull};
use std::ptr::NonNull;
use crate::SPCommand;
@ -31,10 +31,12 @@ pub unsafe extern "C" fn sp_packet_from_command(
command: *mut SPCommand,
) -> *mut SPPacket {
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() {
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.
@ -59,7 +61,7 @@ pub unsafe extern "C" fn sp_packet_try_load(
length: usize,
) -> *mut SPPacket {
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) {
Err(_) => std::ptr::null_mut(),
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() {
vec![]
} 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)
};
@ -144,7 +147,7 @@ pub unsafe extern "C" fn sp_packet_clone(
packet: *const SPPacket,
) -> NonNull<SPPacket> {
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))
}
@ -163,5 +166,5 @@ pub unsafe extern "C" fn sp_packet_clone(
#[no_mangle]
pub unsafe extern "C" fn sp_packet_free(packet: *mut SPPacket) {
assert!(!packet.is_null());
_ = unsafe {Box::from_raw(packet)}
_ = unsafe { Box::from_raw(packet) }
}