add slice modifier
This commit is contained in:
parent
39c7c27c86
commit
82696b2d1a
|
@ -56,9 +56,8 @@ wrap_functions!(associate Bitmap;
|
||||||
fn load(
|
fn load(
|
||||||
width: val usize,
|
width: val usize,
|
||||||
height: val usize,
|
height: val usize,
|
||||||
data: val ByteSlice,
|
data: slice ByteSlice,
|
||||||
) -> move_ok *mut Bitmap {
|
) -> move_ok *mut Bitmap {
|
||||||
let data = unsafe { data.as_slice() };
|
|
||||||
Bitmap::load(width, height, data)
|
Bitmap::load(width, height, data)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -97,7 +96,5 @@ wrap_methods!(Bitmap;
|
||||||
/// Gets an unsafe reference to the data of the [Bitmap] instance.
|
/// Gets an unsafe reference to the data of the [Bitmap] instance.
|
||||||
///
|
///
|
||||||
/// The returned memory is valid for the lifetime of the bitmap.
|
/// The returned memory is valid for the lifetime of the bitmap.
|
||||||
fn data_ref_mut(mut instance) -> val ByteSlice {
|
fn data_ref_mut(mut instance) -> slice ByteSlice;
|
||||||
unsafe { ByteSlice::from_slice(instance.data_ref_mut()) }
|
|
||||||
};
|
|
||||||
);
|
);
|
||||||
|
|
|
@ -28,8 +28,7 @@ wrap_functions!(associate DisplayBitVec;
|
||||||
/// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance.
|
/// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance.
|
||||||
///
|
///
|
||||||
/// returns: [DisplayBitVec] instance containing data.
|
/// returns: [DisplayBitVec] instance containing data.
|
||||||
fn load(data: val ByteSlice) -> move NonNull<DisplayBitVec> {
|
fn load(data: slice ByteSlice) -> move NonNull<DisplayBitVec> {
|
||||||
let data = unsafe { data.as_slice() };
|
|
||||||
DisplayBitVec::from_slice(data)
|
DisplayBitVec::from_slice(data)
|
||||||
};
|
};
|
||||||
);
|
);
|
||||||
|
@ -98,7 +97,5 @@ wrap_methods!(DisplayBitVec;
|
||||||
/// Gets an unsafe reference to the data of the [DisplayBitVec] instance.
|
/// Gets an unsafe reference to the data of the [DisplayBitVec] instance.
|
||||||
///
|
///
|
||||||
/// The returned memory is valid for the lifetime of the bitvec.
|
/// The returned memory is valid for the lifetime of the bitvec.
|
||||||
fn as_raw_mut_slice(mut instance) -> val ByteSlice {
|
fn as_raw_mut_slice(mut instance) -> slice ByteSlice;
|
||||||
unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) }
|
|
||||||
};
|
|
||||||
);
|
);
|
||||||
|
|
|
@ -40,9 +40,8 @@ wrap_functions!(associate BrightnessGrid;
|
||||||
fn load(
|
fn load(
|
||||||
width: val usize,
|
width: val usize,
|
||||||
height: val usize,
|
height: val usize,
|
||||||
data: val ByteSlice,
|
data: slice ByteSlice,
|
||||||
) -> move_some *mut BrightnessGrid {
|
) -> move_some *mut BrightnessGrid {
|
||||||
let data = unsafe { data.as_slice() };
|
|
||||||
ByteGrid::load(width, height, data)
|
ByteGrid::load(width, height, data)
|
||||||
.map(move |grid| grid.map(Brightness::saturating_from))
|
.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.
|
/// Gets an unsafe reference to the data of the instance.
|
||||||
///
|
///
|
||||||
/// The returned memory is valid for the lifetime of the grid.
|
/// 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
|
//noinspection RsAssertEqual
|
||||||
const _: () = assert!(size_of::<Brightness>() == 1);
|
const _: () = assert!(size_of::<Brightness>() == 1);
|
||||||
|
|
||||||
let br_slice = instance.data_ref_mut();
|
let br_slice = instance.data_ref_mut();
|
||||||
unsafe {
|
unsafe {
|
||||||
ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice))
|
transmute::<&mut [Brightness], &mut [u8]>(br_slice)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
);
|
);
|
||||||
|
|
|
@ -33,6 +33,7 @@ impl ByteSlice {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) unsafe fn as_slice(&self) -> &[u8] {
|
pub(crate) unsafe fn as_slice(&self) -> &[u8] {
|
||||||
|
assert!(!self.start.is_null());
|
||||||
unsafe { std::slice::from_raw_parts(self.start, self.length) }
|
unsafe { std::slice::from_raw_parts(self.start, self.length) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,8 +28,7 @@ wrap_functions!(associate CharGrid;
|
||||||
/// Loads a [CharGrid] with the specified dimensions from the provided data.
|
/// Loads a [CharGrid] with the specified dimensions from the provided data.
|
||||||
///
|
///
|
||||||
/// returns: new CharGrid or NULL in case of an error
|
/// 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 {
|
fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_ok *mut CharGrid {
|
||||||
let data = unsafe { data.as_slice() };
|
|
||||||
CharGrid::load_utf8(width, height, data.to_vec())
|
CharGrid::load_utf8(width, height, data.to_vec())
|
||||||
};
|
};
|
||||||
);
|
);
|
||||||
|
|
|
@ -18,8 +18,7 @@ wrap_functions!(associate Cp437Grid;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Loads a [Cp437Grid] with the specified dimensions from the provided data.
|
/// 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 {
|
fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_some *mut Cp437Grid {
|
||||||
let data = unsafe { data.as_slice() };
|
|
||||||
Cp437Grid::load(width, height, data)
|
Cp437Grid::load(width, height, data)
|
||||||
};
|
};
|
||||||
);
|
);
|
||||||
|
@ -40,7 +39,5 @@ wrap_methods!(Cp437Grid;
|
||||||
/// Gets an unsafe reference to the data of the grid.
|
/// Gets an unsafe reference to the data of the grid.
|
||||||
///
|
///
|
||||||
/// The returned memory is valid for the lifetime of the instance.
|
/// The returned memory is valid for the lifetime of the instance.
|
||||||
fn data_ref_mut(mut instance) -> val ByteSlice {
|
fn data_ref_mut(mut instance) -> slice ByteSlice;
|
||||||
unsafe { ByteSlice::from_slice(instance.data_ref_mut()) }
|
|
||||||
};
|
|
||||||
);
|
);
|
||||||
|
|
|
@ -169,6 +169,9 @@ macro_rules! apply_param_modifier {
|
||||||
(ref, $param_name:ident) => {
|
(ref, $param_name:ident) => {
|
||||||
unsafe { $param_name.as_ref() }
|
unsafe { $param_name.as_ref() }
|
||||||
};
|
};
|
||||||
|
(slice, $param_name:ident) => {
|
||||||
|
unsafe { $param_name.as_slice() }
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! apply_return_modifier {
|
macro_rules! apply_return_modifier {
|
||||||
|
@ -184,6 +187,9 @@ macro_rules! apply_return_modifier {
|
||||||
(move_some, $result:ident) => {
|
(move_some, $result:ident) => {
|
||||||
$crate::mem::heap_move_some($result)
|
$crate::mem::heap_move_some($result)
|
||||||
};
|
};
|
||||||
|
(slice, $result:ident) => {
|
||||||
|
unsafe { ByteSlice::from_slice($result) }
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! wrap_function {
|
macro_rules! wrap_function {
|
||||||
|
|
|
@ -11,8 +11,7 @@ wrap_functions!(associate Packet;
|
||||||
/// Tries to load a [Packet] from the passed array with the specified length.
|
/// 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
|
/// returns: NULL in case of an error, pointer to the allocated packet otherwise
|
||||||
fn try_load(data: val ByteSlice) -> move_ok *mut Packet {
|
fn try_load(data: slice ByteSlice) -> move_ok *mut Packet {
|
||||||
let data = unsafe { data.as_slice() };
|
|
||||||
servicepoint::Packet::try_from(data)
|
servicepoint::Packet::try_from(data)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue