diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index b47cfbc..b6ed4f3 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -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; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 50a4eee..b16dd92 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -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 { - let data = unsafe { data.as_slice() }; + fn load(data: slice ByteSlice) -> move NonNull { 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; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index f7cd099..19f6b7a 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -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::() == 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) } }; ); diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index d12cd88..81380e9 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -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) } } diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index a43f93d..98a2c5b 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -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()) }; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index e19ff72..62f0eb2 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -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; ); diff --git a/src/macros.rs b/src/macros.rs index 086c901..79cd3af 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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 { diff --git a/src/packet.rs b/src/packet.rs index abfa562..bec75e5 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -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) };