servicepoint/crates/servicepoint_binding_c/src/cp437_grid.rs

228 lines
5.5 KiB
Rust
Raw Normal View History

2024-06-23 15:42:15 +02:00
//! C functions for interacting with `Cp437Grid`s
//!
2024-06-23 15:42:15 +02:00
//! prefix `sp_cp437_grid_`
use crate::c_slice::CByteSlice;
2024-08-29 22:21:21 +02:00
use servicepoint::{Cp437Grid, DataRef, Grid};
2024-06-23 15:42:15 +02:00
/// A C-wrapper for grid containing codepage 437 characters.
///
/// The encoding is currently not enforced.
2024-08-29 22:21:21 +02:00
pub struct CCp437Grid {
pub(crate) actual: Cp437Grid,
}
impl Clone for CCp437Grid {
fn clone(&self) -> Self {
CCp437Grid {
actual: self.actual.clone(),
}
}
}
2024-06-23 15:12:03 +02:00
2024-06-23 15:42:15 +02:00
/// Creates a new `Cp437Grid` with the specified dimensions.
2024-05-28 20:37:55 +02:00
///
2024-06-23 15:42:15 +02:00
/// returns: `Cp437Grid` initialized to 0.
2024-05-28 20:37:55 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - the returned instance is freed in some way, either by using a consuming function or
2024-06-23 15:42:15 +02:00
/// by explicitly calling `sp_cp437_grid_dealloc`.
#[no_mangle]
2024-06-23 15:42:15 +02:00
pub unsafe extern "C" fn sp_cp437_grid_new(
width: usize,
height: usize,
2024-06-23 15:42:15 +02:00
) -> *mut CCp437Grid {
2024-08-29 22:21:21 +02:00
Box::into_raw(Box::new(CCp437Grid {
actual: Cp437Grid::new(width, height),
}))
}
2024-06-23 15:42:15 +02:00
/// Loads a `Cp437Grid` with the specified dimensions from the provided data.
2024-05-28 20:37:55 +02:00
///
/// # Panics
///
/// When the provided `data_length` is not sufficient for the `height` and `width`
///
/// # 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
2024-06-23 15:42:15 +02:00
/// by explicitly calling `sp_cp437_grid_dealloc`.
#[no_mangle]
2024-06-23 15:42:15 +02:00
pub unsafe extern "C" fn sp_cp437_grid_load(
width: usize,
height: usize,
data: *const u8,
data_length: usize,
2024-06-23 15:42:15 +02:00
) -> *mut CCp437Grid {
let data = std::slice::from_raw_parts(data, data_length);
2024-08-29 22:21:21 +02:00
Box::into_raw(Box::new(CCp437Grid {
actual: Cp437Grid::load(width, height, data),
}))
}
2024-06-23 15:42:15 +02:00
/// Clones a `Cp437Grid`.
2024-05-28 20:37:55 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-06-23 15:42:15 +02:00
/// - `this` points to a valid `Cp437Grid`
2024-05-28 20:37:55 +02:00
/// - `this` is not written to concurrently
/// - the returned instance is freed in some way, either by using a consuming function or
2024-06-23 15:42:15 +02:00
/// by explicitly calling `sp_cp437_grid_dealloc`.
#[no_mangle]
2024-06-23 15:42:15 +02:00
pub unsafe extern "C" fn sp_cp437_grid_clone(
this: *const CCp437Grid,
) -> *mut CCp437Grid {
Box::into_raw(Box::new((*this).clone()))
}
2024-06-23 15:42:15 +02:00
/// Deallocates a `Cp437Grid`.
///
2024-05-28 20:37:55 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
2024-06-23 15:42:15 +02:00
/// - `this` points to a valid `Cp437Grid`
2024-05-28 20:37:55 +02:00
/// - `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]
2024-06-23 15:42:15 +02:00
pub unsafe extern "C" fn sp_cp437_grid_dealloc(this: *mut CCp437Grid) {
_ = Box::from_raw(this);
}
2024-05-28 20:37:55 +02:00
/// Gets the current value at the specified position.
///
/// # Arguments
///
/// - `this`: instance to read from
/// - `x` and `y`: position of the cell to read
2024-05-28 20:37:55 +02:00
///
/// # Panics
///
/// When accessing `x` or `y` out of bounds.
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-06-23 15:42:15 +02:00
/// - `this` points to a valid `Cp437Grid`
2024-05-28 20:37:55 +02:00
/// - `this` is not written to concurrently
#[no_mangle]
2024-06-23 15:42:15 +02:00
pub unsafe extern "C" fn sp_cp437_grid_get(
this: *const CCp437Grid,
x: usize,
y: usize,
) -> u8 {
2024-08-29 22:21:21 +02:00
(*this).actual.get(x, y)
}
2024-06-23 15:42:15 +02:00
/// Sets the value of the specified position in the `Cp437Grid`.
2024-05-28 20:37:55 +02:00
///
/// # Arguments
///
/// - `this`: instance to write to
/// - `x` and `y`: position of the cell
/// - `value`: the value to write to the cell
2024-05-28 20:37:55 +02:00
///
/// returns: old value of the cell
///
/// # Panics
///
/// When accessing `x` or `y` out of bounds.
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `this` points to a valid `BitVec`
/// - `this` is not written to or read from concurrently
#[no_mangle]
2024-06-23 15:42:15 +02:00
pub unsafe extern "C" fn sp_cp437_grid_set(
this: *mut CCp437Grid,
x: usize,
y: usize,
value: u8,
) {
2024-08-29 22:21:21 +02:00
(*this).actual.set(x, y, value);
}
2024-06-23 15:42:15 +02:00
/// Sets the value of all cells in the `Cp437Grid`.
2024-05-28 20:37:55 +02:00
///
/// # Arguments
///
/// - `this`: instance to write to
/// - `value`: the value to set all cells to
2024-05-28 20:37:55 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-06-23 15:42:15 +02:00
/// - `this` points to a valid `Cp437Grid`
2024-05-28 20:37:55 +02:00
/// - `this` is not written to or read from concurrently
#[no_mangle]
2024-06-23 15:42:15 +02:00
pub unsafe extern "C" fn sp_cp437_grid_fill(this: *mut CCp437Grid, value: u8) {
2024-08-29 22:21:21 +02:00
(*this).actual.fill(value);
}
2024-06-23 15:42:15 +02:00
/// Gets the width of the `Cp437Grid` instance.
2024-05-28 20:37:55 +02:00
///
/// # Arguments
///
/// - `this`: instance to read from
2024-05-28 20:37:55 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-06-23 15:42:15 +02:00
/// - `this` points to a valid `Cp437Grid`
#[no_mangle]
2024-06-23 15:42:15 +02:00
pub unsafe extern "C" fn sp_cp437_grid_width(this: *const CCp437Grid) -> usize {
2024-08-29 22:21:21 +02:00
(*this).actual.width()
}
2024-06-23 15:42:15 +02:00
/// Gets the height of the `Cp437Grid` instance.
2024-05-28 20:37:55 +02:00
///
/// # Arguments
///
/// - `this`: instance to read from
2024-05-28 20:37:55 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-06-23 15:42:15 +02:00
/// - `this` points to a valid `Cp437Grid`
#[no_mangle]
2024-06-23 16:01:11 +02:00
pub unsafe extern "C" fn sp_cp437_grid_height(
this: *const CCp437Grid,
) -> usize {
2024-08-29 22:21:21 +02:00
(*this).actual.height()
}
2024-06-23 15:42:15 +02:00
/// Gets an unsafe reference to the data of the `Cp437Grid` instance.
///
/// ## Safety
///
2024-05-28 20:37:55 +02:00
/// The caller has to make sure that:
///
2024-06-23 15:42:15 +02:00
/// - `this` points to a valid `Cp437Grid`
/// - the returned memory range is never accessed after the passed `Cp437Grid` has been freed
/// - the returned memory range is never accessed concurrently, either via the `Cp437Grid` or directly
#[no_mangle]
2024-06-23 15:42:15 +02:00
pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref(
this: *mut CCp437Grid,
) -> CByteSlice {
2024-08-29 22:21:21 +02:00
let data = (*this).actual.data_ref_mut();
CByteSlice {
start: data.as_mut_ptr_range().start,
length: data.len(),
}
}