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 std::sync::{Arc, RwLock};
use std::sync::{Arc};
use crate::macros::wrap_uniffi_object;
#[derive(uniffi::Object)]
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),
})
}
}
wrap_uniffi_object!(Bitmap);
#[uniffi::export]
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)]
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),
})
}
}
wrap_uniffi_object!(DisplayBitVec, BitVec);
#[uniffi::export]
impl BitVec {

View file

@ -1,18 +1,8 @@
use servicepoint::{Brightness, DataRef, Grid};
use std::sync::{Arc, RwLock};
use std::sync::{Arc};
use crate::macros::wrap_uniffi_object;
#[derive(uniffi::Object)]
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),
})
}
}
wrap_uniffi_object!(BrightnessGrid);
#[uniffi::export]
impl BrightnessGrid {

View file

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

View file

@ -1,4 +1,3 @@
use crate::bitmap::Bitmap;
use crate::bitvec::BitVec;
use crate::brightness_grid::BrightnessGrid;
use crate::char_grid::CharGrid;
@ -7,17 +6,10 @@ use crate::cp437_grid::Cp437Grid;
use crate::errors::ServicePointError;
use servicepoint::{BitVecCommand, BrightnessGridCommand, CharGridCommand, ClearCommand, Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand, HardResetCommand, Origin};
use std::sync::Arc;
use crate::bitmap::Bitmap;
use crate::macros::wrap_uniffi_object;
#[derive(uniffi::Object)]
pub struct Command {
pub(crate) actual: servicepoint::TypedCommand,
}
impl Command {
fn internal_new(actual: servicepoint::TypedCommand) -> Arc<Command> {
Arc::new(Command { actual })
}
}
wrap_uniffi_object!(TypedCommand, Command);
#[uniffi::export]
impl Command {
@ -126,11 +118,11 @@ impl Command {
#[uniffi::constructor]
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 {
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> {
self.actual.send_command(command.actual.clone()).ok_or_else(|| {
self.actual.send_command(command.actual.read().unwrap().clone()).ok_or_else(|| {
ServicePointError::IoError {
error: "send failed".to_string(),
}

View file

@ -1,19 +1,9 @@
use crate::char_grid::CharGrid;
use servicepoint::{DataRef, Grid};
use std::sync::{Arc, RwLock};
use std::sync::{Arc};
use crate::macros::wrap_uniffi_object;
#[derive(uniffi::Object)]
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),
})
}
}
wrap_uniffi_object!(Cp437Grid);
#[uniffi::export]
impl Cp437Grid {

View file

@ -10,3 +10,4 @@ mod connection;
mod constants;
mod cp437_grid;
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;