2024-05-28 22:23:21 +02:00
|
|
|
//! C functions for interacting with `BitVec`s
|
|
|
|
//!
|
|
|
|
//! prefix `sp_bit_vec_`
|
|
|
|
|
2024-05-26 13:15:11 +02:00
|
|
|
use crate::c_slice::CByteSlice;
|
2024-06-03 21:08:26 +02:00
|
|
|
use servicepoint::bitvec::prelude::{BitVec, Msb0};
|
|
|
|
|
|
|
|
/// cbindgen:no-export
|
|
|
|
type SpBitVec = BitVec<u8, Msb0>;
|
|
|
|
|
2024-08-29 22:02:53 +02:00
|
|
|
/// A vector of bits
|
2024-06-03 21:08:26 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2024-05-26 13:15:11 +02:00
|
|
|
|
2024-08-29 22:21:21 +02:00
|
|
|
impl Clone for CBitVec {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
CBitVec {
|
|
|
|
actual: self.actual.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-26 13:15:11 +02:00
|
|
|
/// Creates a new `BitVec` instance.
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `size`: size in bits.
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// returns: `BitVec` with all bits set to false.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// When `size` is not divisible by 8.
|
2024-05-28 19:38:43 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// The caller has to make sure that:
|
2024-05-28 19:38:43 +02:00
|
|
|
///
|
|
|
|
/// - the returned instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_bit_vec_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-03 21:08:26 +02:00
|
|
|
pub unsafe extern "C" fn sp_bit_vec_new(size: usize) -> *mut CBitVec {
|
|
|
|
Box::into_raw(Box::new(CBitVec {
|
|
|
|
actual: SpBitVec::repeat(false, size),
|
|
|
|
}))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 20:37:55 +02:00
|
|
|
/// Interpret the data as a series of bits and load then into a new `BitVec` instance.
|
2024-05-28 19:38:43 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `data` points to a valid memory location of at least `data_length`
|
|
|
|
/// bytes in size.
|
|
|
|
/// - the returned instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_bit_vec_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_bit_vec_load(
|
|
|
|
data: *const u8,
|
|
|
|
data_length: usize,
|
2024-06-03 21:08:26 +02:00
|
|
|
) -> *mut CBitVec {
|
2024-05-26 13:15:11 +02:00
|
|
|
let data = std::slice::from_raw_parts(data, data_length);
|
2024-06-03 21:08:26 +02:00
|
|
|
Box::into_raw(Box::new(CBitVec {
|
|
|
|
actual: SpBitVec::from_slice(data),
|
|
|
|
}))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clones a `BitVec`.
|
2024-05-28 19:38:43 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `BitVec`
|
|
|
|
/// - `this` is not written to concurrently
|
|
|
|
/// - the returned instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_bit_vec_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-03 21:08:26 +02:00
|
|
|
pub unsafe extern "C" fn sp_bit_vec_clone(
|
|
|
|
this: *const CBitVec,
|
|
|
|
) -> *mut CBitVec {
|
2024-05-26 13:15:11 +02:00
|
|
|
Box::into_raw(Box::new((*this).clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deallocates a `BitVec`.
|
|
|
|
///
|
2024-05-28 19:38:43 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `BitVec`
|
|
|
|
/// - `this` is not used concurrently or after this call
|
|
|
|
/// - `this` was not passed to another consuming function, e.g. to create a `Command`
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-03 21:08:26 +02:00
|
|
|
pub unsafe extern "C" fn sp_bit_vec_dealloc(this: *mut CBitVec) {
|
2024-05-26 13:15:11 +02:00
|
|
|
_ = Box::from_raw(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the value of a bit from the `BitVec`.
|
2024-05-28 19:38:43 +02:00
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `this`: instance to read from
|
|
|
|
/// - `index`: the bit index to read
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// returns: value of the bit
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// When accessing `index` out of bounds.
|
|
|
|
///
|
2024-05-28 19:38:43 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `BitVec`
|
|
|
|
/// - `this` is not written to concurrently
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_bit_vec_get(
|
2024-06-03 21:08:26 +02:00
|
|
|
this: *const CBitVec,
|
2024-05-26 13:15:11 +02:00
|
|
|
index: usize,
|
|
|
|
) -> bool {
|
2024-06-03 21:08:26 +02:00
|
|
|
*(*this).actual.get(index).unwrap()
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the value of a bit in the `BitVec`.
|
2024-05-28 19:38:43 +02:00
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `this`: instance to write to
|
|
|
|
/// - `index`: the bit index to edit
|
|
|
|
/// - `value`: the value to set the bit to
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// returns: old value of the bit
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// When accessing `index` out of bounds.
|
|
|
|
///
|
2024-05-28 19:38:43 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `BitVec`
|
|
|
|
/// - `this` is not written to or read from concurrently
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_bit_vec_set(
|
2024-06-03 21:08:26 +02:00
|
|
|
this: *mut CBitVec,
|
2024-05-26 13:15:11 +02:00
|
|
|
index: usize,
|
|
|
|
value: bool,
|
2024-06-03 21:08:26 +02:00
|
|
|
) {
|
|
|
|
(*this).actual.set(index, value)
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the value of all bits in the `BitVec`.
|
2024-05-28 19:38:43 +02:00
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `value`: the value to set all bits to
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
2024-05-28 19:38:43 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `BitVec`
|
|
|
|
/// - `this` is not written to or read from concurrently
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-03 21:08:26 +02:00
|
|
|
pub unsafe extern "C" fn sp_bit_vec_fill(this: *mut CBitVec, value: bool) {
|
|
|
|
(*this).actual.fill(value)
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the length of the `BitVec` in bits.
|
2024-05-28 19:38:43 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `BitVec`
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-03 21:08:26 +02:00
|
|
|
pub unsafe extern "C" fn sp_bit_vec_len(this: *const CBitVec) -> usize {
|
|
|
|
(*this).actual.len()
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if length is 0.
|
2024-05-28 19:38:43 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `BitVec`
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-03 21:08:26 +02:00
|
|
|
pub unsafe extern "C" fn sp_bit_vec_is_empty(this: *const CBitVec) -> bool {
|
|
|
|
(*this).actual.is_empty()
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets an unsafe reference to the data of the `BitVec` instance.
|
|
|
|
///
|
|
|
|
/// ## Safety
|
|
|
|
///
|
2024-05-28 19:38:43 +02:00
|
|
|
/// The caller has to make sure that:
|
2024-05-26 13:15:11 +02:00
|
|
|
///
|
2024-05-28 19:38:43 +02:00
|
|
|
/// - `this` points to a valid `BitVec`
|
|
|
|
/// - the returned memory range is never accessed after the passed `BitVec` has been freed
|
|
|
|
/// - the returned memory range is never accessed concurrently, either via the `BitVec` or directly
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_bit_vec_unsafe_data_ref(
|
2024-06-03 21:08:26 +02:00
|
|
|
this: *mut CBitVec,
|
2024-05-26 13:15:11 +02:00
|
|
|
) -> CByteSlice {
|
2024-06-03 21:08:26 +02:00
|
|
|
let data = (*this).actual.as_raw_mut_slice();
|
2024-05-26 13:15:11 +02:00
|
|
|
CByteSlice {
|
|
|
|
start: data.as_mut_ptr_range().start,
|
|
|
|
length: data.len(),
|
|
|
|
}
|
|
|
|
}
|