add slice modifier
Some checks failed
Rust / build-gnu-apt (pull_request) Successful in 2m7s
Rust / build-size-gnu-unstable (pull_request) Failing after 2m48s

This commit is contained in:
Vinzenz Schroeter 2025-06-26 19:26:41 +02:00
parent 39c7c27c86
commit 82696b2d1a
8 changed files with 18 additions and 23 deletions

View file

@ -56,9 +56,8 @@ wrap_functions!(associate Bitmap;
fn load(
width: val usize,
height: val usize,
data: val ByteSlice,
data: slice ByteSlice,
) -> move_ok *mut Bitmap {
let data = unsafe { data.as_slice() };
Bitmap::load(width, height, data)
};
@ -97,7 +96,5 @@ wrap_methods!(Bitmap;
/// Gets an unsafe reference to the data of the [Bitmap] instance.
///
/// The returned memory is valid for the lifetime of the bitmap.
fn data_ref_mut(mut instance) -> val ByteSlice {
unsafe { ByteSlice::from_slice(instance.data_ref_mut()) }
};
fn data_ref_mut(mut instance) -> slice ByteSlice;
);

View file

@ -28,8 +28,7 @@ wrap_functions!(associate DisplayBitVec;
/// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance.
///
/// returns: [DisplayBitVec] instance containing data.
fn load(data: val ByteSlice) -> move NonNull<DisplayBitVec> {
let data = unsafe { data.as_slice() };
fn load(data: slice ByteSlice) -> move NonNull<DisplayBitVec> {
DisplayBitVec::from_slice(data)
};
);
@ -98,7 +97,5 @@ wrap_methods!(DisplayBitVec;
/// Gets an unsafe reference to the data of the [DisplayBitVec] instance.
///
/// The returned memory is valid for the lifetime of the bitvec.
fn as_raw_mut_slice(mut instance) -> val ByteSlice {
unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) }
};
fn as_raw_mut_slice(mut instance) -> slice ByteSlice;
);

View file

@ -40,9 +40,8 @@ wrap_functions!(associate BrightnessGrid;
fn load(
width: val usize,
height: val usize,
data: val ByteSlice,
data: slice ByteSlice,
) -> move_some *mut BrightnessGrid {
let data = unsafe { data.as_slice() };
ByteGrid::load(width, height, data)
.map(move |grid| grid.map(Brightness::saturating_from))
};
@ -64,13 +63,13 @@ wrap_methods!(BrightnessGrid;
/// Gets an unsafe reference to the data of the instance.
///
/// The returned memory is valid for the lifetime of the grid.
fn data_ref_mut(mut instance) -> val ByteSlice {
fn data_ref_mut(mut instance) -> slice ByteSlice {
//noinspection RsAssertEqual
const _: () = assert!(size_of::<Brightness>() == 1);
let br_slice = instance.data_ref_mut();
unsafe {
ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice))
transmute::<&mut [Brightness], &mut [u8]>(br_slice)
}
};
);

View file

@ -33,6 +33,7 @@ impl ByteSlice {
};
pub(crate) unsafe fn as_slice(&self) -> &[u8] {
assert!(!self.start.is_null());
unsafe { std::slice::from_raw_parts(self.start, self.length) }
}

View file

@ -28,8 +28,7 @@ wrap_functions!(associate CharGrid;
/// Loads a [CharGrid] with the specified dimensions from the provided data.
///
/// returns: new CharGrid or NULL in case of an error
fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_ok *mut CharGrid {
let data = unsafe { data.as_slice() };
fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_ok *mut CharGrid {
CharGrid::load_utf8(width, height, data.to_vec())
};
);

View file

@ -18,8 +18,7 @@ wrap_functions!(associate Cp437Grid;
};
/// Loads a [Cp437Grid] with the specified dimensions from the provided data.
fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_some *mut Cp437Grid {
let data = unsafe { data.as_slice() };
fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_some *mut Cp437Grid {
Cp437Grid::load(width, height, data)
};
);
@ -40,7 +39,5 @@ wrap_methods!(Cp437Grid;
/// Gets an unsafe reference to the data of the grid.
///
/// The returned memory is valid for the lifetime of the instance.
fn data_ref_mut(mut instance) -> val ByteSlice {
unsafe { ByteSlice::from_slice(instance.data_ref_mut()) }
};
fn data_ref_mut(mut instance) -> slice ByteSlice;
);

View file

@ -169,6 +169,9 @@ macro_rules! apply_param_modifier {
(ref, $param_name:ident) => {
unsafe { $param_name.as_ref() }
};
(slice, $param_name:ident) => {
unsafe { $param_name.as_slice() }
};
}
macro_rules! apply_return_modifier {
@ -184,6 +187,9 @@ macro_rules! apply_return_modifier {
(move_some, $result:ident) => {
$crate::mem::heap_move_some($result)
};
(slice, $result:ident) => {
unsafe { ByteSlice::from_slice($result) }
};
}
macro_rules! wrap_function {

View file

@ -11,8 +11,7 @@ wrap_functions!(associate Packet;
/// Tries to load a [Packet] from the passed array with the specified length.
///
/// returns: NULL in case of an error, pointer to the allocated packet otherwise
fn try_load(data: val ByteSlice) -> move_ok *mut Packet {
let data = unsafe { data.as_slice() };
fn try_load(data: slice ByteSlice) -> move_ok *mut Packet {
servicepoint::Packet::try_from(data)
};