servicepoint-binding-c/src/containers/bitvec.rs
Vinzenz Schroeter c65b735f57
Some checks failed
Rust / build-gnu-apt (pull_request) Successful in 2m4s
Rust / build-size-gnu-unstable (pull_request) Failing after 2m44s
generic wrap
2025-06-26 22:36:57 +02:00

104 lines
2.9 KiB
Rust

use crate::{
containers::{wrap_container, ByteSlice},
macros::wrap,
};
use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet,
};
use std::ptr::NonNull;
wrap_container!(DisplayBitVec);
wrap! {
DisplayBitVec {
functions:
/// Creates a new [DisplayBitVec] instance.
///
/// # Arguments
///
/// - `size`: size in bits.
///
/// returns: [DisplayBitVec] with all bits set to false.
///
/// # Panics
///
/// - when `size` is not divisible by 8.
fn new(size: val usize) -> move NonNull<DisplayBitVec> {
DisplayBitVec::repeat(false, size)
};
/// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance.
///
/// returns: [DisplayBitVec] instance containing data.
fn load(data: slice ByteSlice) -> move NonNull<DisplayBitVec> {
DisplayBitVec::from_slice(data)
};
methods:
/// Creates a [BitVecCommand] and immediately turns that into a [Packet].
///
/// The provided [DisplayBitVec] gets consumed.
///
/// Returns NULL in case of an error.
fn try_into_packet(
move bitvec,
offset: val usize,
operation: val BinaryOperation,
compression: val CompressionCode
) -> move_ok *mut Packet {
Packet::try_from(BitVecCommand {
bitvec,
offset,
operation,
compression,
})
};
/// Gets the value of a bit.
///
/// # Arguments
///
/// - `bit_vec`: instance to read from
/// - `index`: the bit index to read
///
/// returns: value of the bit
///
/// # Panics
///
/// - when accessing `index` out of bounds
fn get(ref instance, index: val usize) -> val bool {
instance.get(index).map(|x| *x).unwrap_or(false)
};
/// Sets the value of a bit.
///
/// # Arguments
///
/// - `index`: the bit index to edit
/// - `value`: the value to set the bit to
///
/// # Panics
///
/// - when accessing `index` out of bounds
fn set(mut instance, index: val usize, value: val bool);
/// Sets the value of all bits.
///
/// # Arguments
///
/// - `value`: the value to set all bits to
fn fill(mut instance, value: val bool);
/// Gets the length in bits.
fn len(ref instance) -> val usize;
/// Returns true if length is 0.
fn is_empty(ref instance) -> val bool;
/// 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) -> slice ByteSlice;
}
}