declare wrapper types with macro

This commit is contained in:
Vinzenz Schroeter 2025-06-13 01:09:31 +02:00
parent 022106e2db
commit 88f7696dbd
9 changed files with 44 additions and 77 deletions

View file

@ -1,18 +1,8 @@
use servicepoint::{DataRef, Grid}; use servicepoint::{DataRef, Grid};
use std::sync::{Arc, RwLock}; use std::sync::{Arc};
use crate::macros::wrap_uniffi_object;
#[derive(uniffi::Object)] wrap_uniffi_object!(Bitmap);
pub struct Bitmap {
pub(crate) actual: RwLock<servicepoint::Bitmap>,
}
impl Bitmap {
fn internal_new(actual: servicepoint::Bitmap) -> Arc<Self> {
Arc::new(Self {
actual: RwLock::new(actual),
})
}
}
#[uniffi::export] #[uniffi::export]
impl Bitmap { impl Bitmap {

View file

@ -1,17 +1,7 @@
use std::sync::{Arc, RwLock}; use std::sync::{Arc};
use crate::macros::wrap_uniffi_object;
#[derive(uniffi::Object)] wrap_uniffi_object!(DisplayBitVec, BitVec);
pub struct BitVec {
pub(crate) actual: RwLock<servicepoint::DisplayBitVec>,
}
impl BitVec {
fn internal_new(actual: servicepoint::DisplayBitVec) -> Arc<Self> {
Arc::new(Self {
actual: RwLock::new(actual),
})
}
}
#[uniffi::export] #[uniffi::export]
impl BitVec { impl BitVec {

View file

@ -1,18 +1,8 @@
use servicepoint::{Brightness, DataRef, Grid}; use servicepoint::{Brightness, DataRef, Grid};
use std::sync::{Arc, RwLock}; use std::sync::{Arc};
use crate::macros::wrap_uniffi_object;
#[derive(uniffi::Object)] wrap_uniffi_object!(BrightnessGrid);
pub struct BrightnessGrid {
pub(crate) actual: RwLock<servicepoint::BrightnessGrid>,
}
impl BrightnessGrid {
fn internal_new(actual: servicepoint::BrightnessGrid) -> Arc<Self> {
Arc::new(Self {
actual: RwLock::new(actual),
})
}
}
#[uniffi::export] #[uniffi::export]
impl BrightnessGrid { impl BrightnessGrid {

View file

@ -1,12 +1,10 @@
use crate::cp437_grid::Cp437Grid; use crate::cp437_grid::Cp437Grid;
use servicepoint::{Grid, SetValueSeriesError}; use servicepoint::{Grid, SetValueSeriesError};
use std::convert::Into; use std::convert::Into;
use std::sync::{Arc, RwLock}; use std::sync::{Arc};
use crate::macros::wrap_uniffi_object;
#[derive(uniffi::Object)] wrap_uniffi_object!(CharGrid);
pub struct CharGrid {
pub(crate) actual: RwLock<servicepoint::CharGrid>,
}
#[derive(uniffi::Error, thiserror::Error, Debug)] #[derive(uniffi::Error, thiserror::Error, Debug)]
pub enum CharGridError { pub enum CharGridError {
@ -133,12 +131,6 @@ impl CharGrid {
} }
impl CharGrid { impl CharGrid {
pub(crate) fn internal_new(actual: servicepoint::CharGrid) -> Arc<Self> {
Arc::new(Self {
actual: RwLock::new(actual),
})
}
fn str_to_char(value: String) -> Result<char, CharGridError> { fn str_to_char(value: String) -> Result<char, CharGridError> {
if value.len() != 1 { if value.len() != 1 {
return Err(CharGridError::StringNotOneChar { value }); return Err(CharGridError::StringNotOneChar { value });

View file

@ -1,4 +1,3 @@
use crate::bitmap::Bitmap;
use crate::bitvec::BitVec; use crate::bitvec::BitVec;
use crate::brightness_grid::BrightnessGrid; use crate::brightness_grid::BrightnessGrid;
use crate::char_grid::CharGrid; use crate::char_grid::CharGrid;
@ -7,17 +6,10 @@ use crate::cp437_grid::Cp437Grid;
use crate::errors::ServicePointError; use crate::errors::ServicePointError;
use servicepoint::{BitVecCommand, BrightnessGridCommand, CharGridCommand, ClearCommand, Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand, HardResetCommand, Origin}; use servicepoint::{BitVecCommand, BrightnessGridCommand, CharGridCommand, ClearCommand, Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand, HardResetCommand, Origin};
use std::sync::Arc; use std::sync::Arc;
use crate::bitmap::Bitmap;
use crate::macros::wrap_uniffi_object;
#[derive(uniffi::Object)] wrap_uniffi_object!(TypedCommand, Command);
pub struct Command {
pub(crate) actual: servicepoint::TypedCommand,
}
impl Command {
fn internal_new(actual: servicepoint::TypedCommand) -> Arc<Command> {
Arc::new(Command { actual })
}
}
#[uniffi::export] #[uniffi::export]
impl Command { impl Command {
@ -126,11 +118,11 @@ impl Command {
#[uniffi::constructor] #[uniffi::constructor]
pub fn clone(other: &Arc<Self>) -> Arc<Self> { pub fn clone(other: &Arc<Self>) -> Arc<Self> {
Self::internal_new(other.actual.clone()) Self::internal_new(other.actual.read().unwrap().clone())
} }
pub fn equals(&self, other: &Command) -> bool { pub fn equals(&self, other: &Command) -> bool {
self.actual == other.actual *self.actual.read().unwrap() == *other.actual.read().unwrap()
} }
} }

View file

@ -25,7 +25,7 @@ impl Connection {
} }
pub fn send(&self, command: Arc<Command>) -> Result<(), ServicePointError> { pub fn send(&self, command: Arc<Command>) -> Result<(), ServicePointError> {
self.actual.send_command(command.actual.clone()).ok_or_else(|| { self.actual.send_command(command.actual.read().unwrap().clone()).ok_or_else(|| {
ServicePointError::IoError { ServicePointError::IoError {
error: "send failed".to_string(), error: "send failed".to_string(),
} }

View file

@ -1,19 +1,9 @@
use crate::char_grid::CharGrid; use crate::char_grid::CharGrid;
use servicepoint::{DataRef, Grid}; use servicepoint::{DataRef, Grid};
use std::sync::{Arc, RwLock}; use std::sync::{Arc};
use crate::macros::wrap_uniffi_object;
#[derive(uniffi::Object)] wrap_uniffi_object!(Cp437Grid);
pub struct Cp437Grid {
pub(crate) actual: RwLock<servicepoint::Cp437Grid>,
}
impl Cp437Grid {
pub(crate) fn internal_new(actual: servicepoint::Cp437Grid) -> Arc<Self> {
Arc::new(Self {
actual: RwLock::new(actual),
})
}
}
#[uniffi::export] #[uniffi::export]
impl Cp437Grid { impl Cp437Grid {

View file

@ -10,3 +10,4 @@ mod connection;
mod constants; mod constants;
mod cp437_grid; mod cp437_grid;
mod errors; mod errors;
mod macros;

22
src/macros.rs Normal file
View file

@ -0,0 +1,22 @@
macro_rules! wrap_uniffi_object {
($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> {
Arc::new(Self {
actual: std::sync::RwLock::new(actual),
})
}
}
};
($t:ident) => {
wrap_uniffi_object!($t, $t);
};
}
pub(crate) use wrap_uniffi_object;