replace bit_vec module with bitvec library

This commit is contained in:
Vinzenz Schroeter 2024-06-03 21:08:26 +02:00
parent c600761f29
commit 947a3fe60e
16 changed files with 187 additions and 369 deletions

View file

@ -2,9 +2,28 @@
//!
//! prefix `sp_bit_vec_`
use servicepoint::{BitVec, DataRef};
use crate::c_slice::CByteSlice;
use servicepoint::bitvec::prelude::{BitVec, Msb0};
/// cbindgen:no-export
type SpBitVec = BitVec<u8, Msb0>;
#[derive(Clone)]
pub struct CBitVec {
actual: SpBitVec,
}
impl From<SpBitVec> for CBitVec {
fn from(actual: SpBitVec) -> Self {
Self { actual }
}
}
impl From<CBitVec> for SpBitVec {
fn from(value: CBitVec) -> Self {
value.actual
}
}
/// Creates a new `BitVec` instance.
///
@ -25,8 +44,10 @@ use crate::c_slice::CByteSlice;
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_bit_vec_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_new(size: usize) -> *mut BitVec {
Box::into_raw(Box::new(BitVec::new(size)))
pub unsafe extern "C" fn sp_bit_vec_new(size: usize) -> *mut CBitVec {
Box::into_raw(Box::new(CBitVec {
actual: SpBitVec::repeat(false, size),
}))
}
/// Interpret the data as a series of bits and load then into a new `BitVec` instance.
@ -43,9 +64,11 @@ pub unsafe extern "C" fn sp_bit_vec_new(size: usize) -> *mut BitVec {
pub unsafe extern "C" fn sp_bit_vec_load(
data: *const u8,
data_length: usize,
) -> *mut BitVec {
) -> *mut CBitVec {
let data = std::slice::from_raw_parts(data, data_length);
Box::into_raw(Box::new(BitVec::from(data)))
Box::into_raw(Box::new(CBitVec {
actual: SpBitVec::from_slice(data),
}))
}
/// Clones a `BitVec`.
@ -59,7 +82,9 @@ pub unsafe extern "C" fn sp_bit_vec_load(
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_bit_vec_dealloc`.
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_clone(this: *const BitVec) -> *mut BitVec {
pub unsafe extern "C" fn sp_bit_vec_clone(
this: *const CBitVec,
) -> *mut CBitVec {
Box::into_raw(Box::new((*this).clone()))
}
@ -73,7 +98,7 @@ pub unsafe extern "C" fn sp_bit_vec_clone(this: *const BitVec) -> *mut BitVec {
/// - `this` is not used concurrently or after this call
/// - `this` was not passed to another consuming function, e.g. to create a `Command`
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_dealloc(this: *mut BitVec) {
pub unsafe extern "C" fn sp_bit_vec_dealloc(this: *mut CBitVec) {
_ = Box::from_raw(this);
}
@ -98,10 +123,10 @@ pub unsafe extern "C" fn sp_bit_vec_dealloc(this: *mut BitVec) {
/// - `this` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_get(
this: *const BitVec,
this: *const CBitVec,
index: usize,
) -> bool {
(*this).get(index)
*(*this).actual.get(index).unwrap()
}
/// Sets the value of a bit in the `BitVec`.
@ -126,11 +151,11 @@ pub unsafe extern "C" fn sp_bit_vec_get(
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_set(
this: *mut BitVec,
this: *mut CBitVec,
index: usize,
value: bool,
) -> bool {
(*this).set(index, value)
) {
(*this).actual.set(index, value)
}
/// Sets the value of all bits in the `BitVec`.
@ -146,8 +171,8 @@ pub unsafe extern "C" fn sp_bit_vec_set(
/// - `this` points to a valid `BitVec`
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_fill(this: *mut BitVec, value: bool) {
(*this).fill(value)
pub unsafe extern "C" fn sp_bit_vec_fill(this: *mut CBitVec, value: bool) {
(*this).actual.fill(value)
}
/// Gets the length of the `BitVec` in bits.
@ -158,8 +183,8 @@ pub unsafe extern "C" fn sp_bit_vec_fill(this: *mut BitVec, value: bool) {
///
/// - `this` points to a valid `BitVec`
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_len(this: *const BitVec) -> usize {
(*this).len()
pub unsafe extern "C" fn sp_bit_vec_len(this: *const CBitVec) -> usize {
(*this).actual.len()
}
/// Returns true if length is 0.
@ -170,8 +195,8 @@ pub unsafe extern "C" fn sp_bit_vec_len(this: *const BitVec) -> usize {
///
/// - `this` points to a valid `BitVec`
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_is_empty(this: *const BitVec) -> bool {
(*this).is_empty()
pub unsafe extern "C" fn sp_bit_vec_is_empty(this: *const CBitVec) -> bool {
(*this).actual.is_empty()
}
/// Gets an unsafe reference to the data of the `BitVec` instance.
@ -185,9 +210,9 @@ pub unsafe extern "C" fn sp_bit_vec_is_empty(this: *const BitVec) -> bool {
/// - the returned memory range is never accessed concurrently, either via the `BitVec` or directly
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_unsafe_data_ref(
this: *mut BitVec,
this: *mut CBitVec,
) -> CByteSlice {
let data = (*this).data_ref_mut();
let data = (*this).actual.as_raw_mut_slice();
CByteSlice {
start: data.as_mut_ptr_range().start,
length: data.len(),

View file

@ -5,10 +5,12 @@
use std::ptr::null_mut;
use servicepoint::{
BitVec, Brightness, ByteGrid, Command, CompressionCode, Offset, Origin,
Packet, PixelGrid,
Brightness, ByteGrid, Command, CompressionCode, Offset, Origin, Packet,
PixelGrid,
};
use crate::bit_vec::CBitVec;
/// Tries to turn a `Packet` into a `Command`. The packet is deallocated in the process.
///
/// Returns: pointer to new `Command` instance or NULL
@ -140,13 +142,13 @@ pub unsafe extern "C" fn sp_command_char_brightness(
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear(
offset: Offset,
bit_vec: *mut BitVec,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinear(
offset,
bit_vec,
bit_vec.into(),
compression,
)))
}
@ -166,13 +168,13 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_and(
offset: Offset,
bit_vec: *mut BitVec,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearAnd(
offset,
bit_vec,
bit_vec.into(),
compression,
)))
}
@ -192,13 +194,13 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_or(
offset: Offset,
bit_vec: *mut BitVec,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearOr(
offset,
bit_vec,
bit_vec.into(),
compression,
)))
}
@ -218,13 +220,13 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
#[no_mangle]
pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
offset: Offset,
bit_vec: *mut BitVec,
bit_vec: *mut CBitVec,
compression: CompressionCode,
) -> *mut Command {
let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(Command::BitmapLinearXor(
offset,
bit_vec,
bit_vec.into(),
compression,
)))
}

View file

@ -22,4 +22,4 @@ pub mod pixel_grid;
/// The minimum time needed for the display to refresh the screen in ms.
pub const FRAME_PACING_MS: u32 = servicepoint::FRAME_PACING.as_millis() as u32;
mod c_slice;
pub mod c_slice;