add get set fill macro

This commit is contained in:
Vinzenz Schroeter 2025-06-13 09:52:07 +02:00
parent 0b3f243ffa
commit 7fe5ef07a8
6 changed files with 86 additions and 83 deletions

View file

@ -1,18 +1,20 @@
use crate::macros::{
wrap_get_set_fill_2d, wrap_uniffi_object, wrap_width_height,
};
use servicepoint::{DataRef, Grid};
use std::sync::{Arc};
use crate::macros::{wrap_width_height, wrap_uniffi_object};
use std::sync::Arc;
wrap_uniffi_object!(Bitmap);
wrap_width_height!(Bitmap);
wrap_get_set_fill_2d!(Bitmap, bool);
#[uniffi::export]
impl Bitmap {
#[uniffi::constructor]
pub fn new(width: u64, height: u64) -> Arc<Self> {
Self::internal_new(servicepoint::Bitmap::new(
width as usize,
height as usize,
).unwrap())
Self::internal_new(
servicepoint::Bitmap::new(width as usize, height as usize).unwrap(),
)
}
#[uniffi::constructor]
@ -22,28 +24,12 @@ impl Bitmap {
#[uniffi::constructor]
pub fn load(width: u64, height: u64, data: Vec<u8>) -> Arc<Self> {
Self::internal_new(servicepoint::Bitmap::load(
width as usize,
height as usize,
&data,
).unwrap())
Self::internal_new(
servicepoint::Bitmap::load(width as usize, height as usize, &data)
.unwrap(),
)
}
pub fn set(&self, x: u64, y: u64, value: bool) {
self.actual
.write()
.unwrap()
.set(x as usize, y as usize, value)
}
pub fn get(&self, x: u64, y: u64) -> bool {
self.actual.read().unwrap().get(x as usize, y as usize)
}
pub fn fill(&self, value: bool) {
self.actual.write().unwrap().fill(value)
}
pub fn equals(&self, other: &Bitmap) -> bool {
let a = self.actual.read().unwrap();
let b = other.actual.read().unwrap();

19
src/brightness.rs Normal file
View file

@ -0,0 +1,19 @@
use crate::UniffiCustomTypeConverter;
use servicepoint::Brightness;
uniffi::custom_type!(Brightness, u8);
impl UniffiCustomTypeConverter for Brightness {
type Builtin = u8;
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self>
where
Self: Sized,
{
Ok(Brightness::saturating_from(val))
}
fn from_custom(obj: Self) -> Self::Builtin {
obj.into()
}
}

View file

@ -1,9 +1,12 @@
use crate::macros::{
wrap_get_set_fill_2d, wrap_uniffi_object, wrap_width_height,
};
use servicepoint::{Brightness, DataRef, Grid};
use std::sync::{Arc};
use crate::macros::{wrap_width_height, wrap_uniffi_object};
use std::sync::Arc;
wrap_uniffi_object!(BrightnessGrid);
wrap_width_height!(BrightnessGrid);
wrap_get_set_fill_2d!(BrightnessGrid, Brightness);
#[uniffi::export]
impl BrightnessGrid {
@ -17,36 +20,16 @@ impl BrightnessGrid {
#[uniffi::constructor]
pub fn load(width: u64, height: u64, data: Vec<u8>) -> Arc<Self> {
Self::internal_new(servicepoint::BrightnessGrid::saturating_load(
width as usize,
height as usize,
&data,
).unwrap())
}
pub fn set(&self, x: u64, y: u64, value: u8) {
self.actual.write().unwrap().set(
x as usize,
y as usize,
Brightness::saturating_from(value),
Self::internal_new(
servicepoint::BrightnessGrid::saturating_load(
width as usize,
height as usize,
&data,
)
.unwrap(),
)
}
pub fn get(&self, x: u64, y: u64) -> u8 {
self.actual
.read()
.unwrap()
.get(x as usize, y as usize)
.into()
}
pub fn fill(&self, value: u8) {
self.actual
.write()
.unwrap()
.fill(Brightness::saturating_from(value))
}
pub fn equals(&self, other: &BrightnessGrid) -> bool {
let a = self.actual.read().unwrap();
let b = other.actual.read().unwrap();

View file

@ -1,10 +1,11 @@
use crate::char_grid::CharGrid;
use crate::macros::{wrap_get_set_fill_2d, wrap_uniffi_object, wrap_width_height};
use servicepoint::{DataRef, Grid};
use std::sync::{Arc};
use crate::macros::{wrap_width_height, wrap_uniffi_object};
use std::sync::Arc;
wrap_uniffi_object!(Cp437Grid);
wrap_width_height!(Cp437Grid);
wrap_get_set_fill_2d!(Cp437Grid, u8);
#[uniffi::export]
impl Cp437Grid {
@ -18,26 +19,14 @@ impl Cp437Grid {
#[uniffi::constructor]
pub fn load(width: u64, height: u64, data: Vec<u8>) -> Arc<Self> {
Self::internal_new(servicepoint::Cp437Grid::load(
width as usize,
height as usize,
&data,
).unwrap())
}
pub fn set(&self, x: u64, y: u64, value: u8) {
self.actual
.write()
.unwrap()
.set(x as usize, y as usize, value)
}
pub fn get(&self, x: u64, y: u64) -> u8 {
self.actual.read().unwrap().get(x as usize, y as usize)
}
pub fn fill(&self, value: u8) {
self.actual.write().unwrap().fill(value)
Self::internal_new(
servicepoint::Cp437Grid::load(
width as usize,
height as usize,
&data,
)
.unwrap(),
)
}
pub fn equals(&self, other: &Cp437Grid) -> bool {

View file

@ -2,6 +2,7 @@ uniffi::setup_scaffolding!();
mod bitmap;
mod bitvec;
mod brightness;
mod brightness_grid;
mod char_grid;
mod command;

View file

@ -1,13 +1,14 @@
macro_rules! wrap_uniffi_object {
($orig_t:ident, $new_t:ident) => {
($orig_t:ident, $new_t:ident) => {
#[derive(uniffi::Object)]
pub struct $new_t {
pub(crate) actual: std::sync::RwLock<servicepoint::$orig_t>,
}
impl $new_t {
pub(crate) fn internal_new(actual: servicepoint::$orig_t) -> Arc<Self> {
pub(crate) fn internal_new(
actual: servicepoint::$orig_t,
) -> Arc<Self> {
Arc::new(Self {
actual: std::sync::RwLock::new(actual),
})
@ -21,7 +22,7 @@ macro_rules! wrap_uniffi_object {
Self::internal_new(other.actual.read().unwrap().clone())
}
}
};
};
($t:ident) => {
wrap_uniffi_object!($t, $t);
};
@ -36,7 +37,7 @@ macro_rules! wrap_width_height {
pub fn width(&self) -> u64 {
self.actual.read().unwrap().width() as u64
}
pub fn height(&self) -> u64 {
self.actual.read().unwrap().height() as u64
}
@ -44,4 +45,28 @@ macro_rules! wrap_width_height {
};
}
pub(crate) use wrap_width_height;
pub(crate) use wrap_width_height;
macro_rules! wrap_get_set_fill_2d {
($t:ident, $contained:ident) => {
#[uniffi::export]
impl $t {
pub fn set(&self, x: u64, y: u64, value: $contained) {
self.actual
.write()
.unwrap()
.set(x as usize, y as usize, value)
}
pub fn get(&self, x: u64, y: u64) -> $contained {
self.actual.read().unwrap().get(x as usize, y as usize)
}
pub fn fill(&self, value: $contained) {
self.actual.write().unwrap().fill(value)
}
}
};
}
pub(crate) use wrap_get_set_fill_2d;