diff --git a/src/bitmap.rs b/src/bitmap.rs index de094d2..d030ea8 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -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::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) -> Arc { - 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(); diff --git a/src/brightness.rs b/src/brightness.rs new file mode 100644 index 0000000..0ea4591 --- /dev/null +++ b/src/brightness.rs @@ -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 + where + Self: Sized, + { + Ok(Brightness::saturating_from(val)) + } + + fn from_custom(obj: Self) -> Self::Builtin { + obj.into() + } +} diff --git a/src/brightness_grid.rs b/src/brightness_grid.rs index 04f6321..697a71f 100644 --- a/src/brightness_grid.rs +++ b/src/brightness_grid.rs @@ -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) -> Arc { - 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(); diff --git a/src/cp437_grid.rs b/src/cp437_grid.rs index b7ba623..eeeb8b6 100644 --- a/src/cp437_grid.rs +++ b/src/cp437_grid.rs @@ -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) -> Arc { - 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 { diff --git a/src/lib.rs b/src/lib.rs index b981f9d..c116ccb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ uniffi::setup_scaffolding!(); mod bitmap; mod bitvec; +mod brightness; mod brightness_grid; mod char_grid; mod command; diff --git a/src/macros.rs b/src/macros.rs index 9a4de90..799a95f 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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, } impl $new_t { - pub(crate) fn internal_new(actual: servicepoint::$orig_t) -> Arc { + pub(crate) fn internal_new( + actual: servicepoint::$orig_t, + ) -> Arc { 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; \ No newline at end of file +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;