From 0b3f243ffa0f49e50e2b6e06b8c7962b543bf191 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 13 Jun 2025 01:27:57 +0200 Subject: [PATCH] add width and height macro --- src/bitmap.rs | 12 +++--------- src/brightness_grid.rs | 10 ++-------- src/char_grid.rs | 11 ++--------- src/cp437_grid.rs | 10 ++-------- src/macros.rs | 21 +++++++++++++++++++-- 5 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/bitmap.rs b/src/bitmap.rs index aa2309c..de094d2 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -1,8 +1,9 @@ use servicepoint::{DataRef, Grid}; use std::sync::{Arc}; -use crate::macros::wrap_uniffi_object; +use crate::macros::{wrap_width_height, wrap_uniffi_object}; wrap_uniffi_object!(Bitmap); +wrap_width_height!(Bitmap); #[uniffi::export] impl Bitmap { @@ -42,14 +43,7 @@ impl Bitmap { pub fn fill(&self, value: bool) { self.actual.write().unwrap().fill(value) } - pub fn width(&self) -> u64 { - self.actual.read().unwrap().width() as u64 - } - - pub fn height(&self) -> u64 { - self.actual.read().unwrap().height() as u64 - } - + pub fn equals(&self, other: &Bitmap) -> bool { let a = self.actual.read().unwrap(); let b = other.actual.read().unwrap(); diff --git a/src/brightness_grid.rs b/src/brightness_grid.rs index 956f139..04f6321 100644 --- a/src/brightness_grid.rs +++ b/src/brightness_grid.rs @@ -1,8 +1,9 @@ use servicepoint::{Brightness, DataRef, Grid}; use std::sync::{Arc}; -use crate::macros::wrap_uniffi_object; +use crate::macros::{wrap_width_height, wrap_uniffi_object}; wrap_uniffi_object!(BrightnessGrid); +wrap_width_height!(BrightnessGrid); #[uniffi::export] impl BrightnessGrid { @@ -45,13 +46,6 @@ impl BrightnessGrid { .unwrap() .fill(Brightness::saturating_from(value)) } - pub fn width(&self) -> u64 { - self.actual.read().unwrap().width() as u64 - } - - pub fn height(&self) -> u64 { - self.actual.read().unwrap().height() as u64 - } pub fn equals(&self, other: &BrightnessGrid) -> bool { let a = self.actual.read().unwrap(); diff --git a/src/char_grid.rs b/src/char_grid.rs index 1f39e1c..ff27cd1 100644 --- a/src/char_grid.rs +++ b/src/char_grid.rs @@ -2,9 +2,10 @@ use crate::cp437_grid::Cp437Grid; use servicepoint::{Grid, SetValueSeriesError}; use std::convert::Into; use std::sync::{Arc}; -use crate::macros::wrap_uniffi_object; +use crate::macros::{wrap_width_height, wrap_uniffi_object}; wrap_uniffi_object!(CharGrid); +wrap_width_height!(CharGrid); #[derive(uniffi::Error, thiserror::Error, Debug)] pub enum CharGridError { @@ -59,14 +60,6 @@ impl CharGrid { Ok(()) } - pub fn width(&self) -> u64 { - self.actual.read().unwrap().width() as u64 - } - - pub fn height(&self) -> u64 { - self.actual.read().unwrap().height() as u64 - } - pub fn equals(&self, other: &CharGrid) -> 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 9db5d8e..b7ba623 100644 --- a/src/cp437_grid.rs +++ b/src/cp437_grid.rs @@ -1,9 +1,10 @@ use crate::char_grid::CharGrid; use servicepoint::{DataRef, Grid}; use std::sync::{Arc}; -use crate::macros::wrap_uniffi_object; +use crate::macros::{wrap_width_height, wrap_uniffi_object}; wrap_uniffi_object!(Cp437Grid); +wrap_width_height!(Cp437Grid); #[uniffi::export] impl Cp437Grid { @@ -38,13 +39,6 @@ impl Cp437Grid { pub fn fill(&self, value: u8) { self.actual.write().unwrap().fill(value) } - pub fn width(&self) -> u64 { - self.actual.read().unwrap().width() as u64 - } - - pub fn height(&self) -> u64 { - self.actual.read().unwrap().height() as u64 - } pub fn equals(&self, other: &Cp437Grid) -> bool { let a = self.actual.read().unwrap(); diff --git a/src/macros.rs b/src/macros.rs index b22b12a..9a4de90 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -13,7 +13,7 @@ macro_rules! wrap_uniffi_object { }) } } - + #[uniffi::export] impl $new_t { #[uniffi::constructor] @@ -27,4 +27,21 @@ macro_rules! wrap_uniffi_object { }; } -pub(crate) use wrap_uniffi_object; \ No newline at end of file +pub(crate) use wrap_uniffi_object; + +macro_rules! wrap_width_height { + ($t:ident) => { + #[uniffi::export] + impl $t { + pub fn width(&self) -> u64 { + self.actual.read().unwrap().width() as u64 + } + + pub fn height(&self) -> u64 { + self.actual.read().unwrap().height() as u64 + } + } + }; +} + +pub(crate) use wrap_width_height; \ No newline at end of file