diff --git a/src/variables/macros.rs b/src/variables/macros.rs index 4e3db1b..61b6997 100644 --- a/src/variables/macros.rs +++ b/src/variables/macros.rs @@ -160,23 +160,8 @@ macro_rules! test_lie { } #[macro_export] -macro_rules! impl_safe_variable { -($($var:ident $(< $num:literal >)? ),* $(,)?) => { - $( - #[cfg_attr(feature = "serde", typetag::serde)] - impl $crate::variables::VariableSafe for $var$(< $num >)? { - fn clone_box(&self) -> Box { - Box::new((*self).clone()) - } - - fn dim(&self) -> usize { - $crate::variables::Variable::dim(self) - } - - fn oplus_mut(&mut self, delta: VectorViewX) { - *self = self.oplus(delta); - } - } - )* - }; +macro_rules! tag_variable { + ($($ty:ty),* $(,)?) => {$( + $crate::register_typetag!($crate::variables::VariableSafe, $ty); + )*}; } diff --git a/src/variables/se2.rs b/src/variables/se2.rs index 391cbce..8351807 100644 --- a/src/variables/se2.rs +++ b/src/variables/se2.rs @@ -3,7 +3,6 @@ use std::{fmt, ops}; use super::VectorVar2; use crate::{ dtype, - impl_safe_variable, linalg::{ dvector, AllocatorBuffer, @@ -24,10 +23,11 @@ use crate::{ VectorViewX, VectorX, }, + tag_variable, variables::{MatrixLieGroup, Variable, SO2}, }; -impl_safe_variable!(SE2); +tag_variable!(SE2); #[derive(Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] diff --git a/src/variables/se3.rs b/src/variables/se3.rs index b21679a..a4b0ce9 100644 --- a/src/variables/se3.rs +++ b/src/variables/se3.rs @@ -3,7 +3,6 @@ use std::{fmt, ops}; use super::VectorVar3; use crate::{ dtype, - impl_safe_variable, linalg::{ AllocatorBuffer, Const, @@ -24,10 +23,11 @@ use crate::{ VectorViewX, VectorX, }, + tag_variable, variables::{MatrixLieGroup, Variable, SO3}, }; -impl_safe_variable!(SE3); +tag_variable!(SE3); #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] diff --git a/src/variables/so2.rs b/src/variables/so2.rs index 4a4ea92..cfd20fc 100644 --- a/src/variables/so2.rs +++ b/src/variables/so2.rs @@ -2,7 +2,6 @@ use std::{fmt, ops}; use crate::{ dtype, - impl_safe_variable, linalg::{ dvector, AllocatorBuffer, @@ -24,10 +23,11 @@ use crate::{ VectorViewX, VectorX, }, + tag_variable, variables::{MatrixLieGroup, Variable}, }; -impl_safe_variable!(SO2); +tag_variable!(SO2); #[derive(Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] diff --git a/src/variables/so3.rs b/src/variables/so3.rs index 08c1e62..14f0e49 100644 --- a/src/variables/so3.rs +++ b/src/variables/so3.rs @@ -3,7 +3,6 @@ use std::{fmt, ops}; use super::VectorVar4; use crate::{ dtype, - impl_safe_variable, linalg::{ dvector, AllocatorBuffer, @@ -23,10 +22,11 @@ use crate::{ VectorViewX, VectorX, }, + tag_variable, variables::{MatrixLieGroup, Variable}, }; -impl_safe_variable!(SO3); +tag_variable!(SO3); #[derive(Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] diff --git a/src/variables/traits.rs b/src/variables/traits.rs index 87f7ef3..5b57416 100644 --- a/src/variables/traits.rs +++ b/src/variables/traits.rs @@ -100,6 +100,34 @@ pub trait VariableSafe: Debug + Display + Downcast { fn oplus_mut(&mut self, delta: VectorViewX); } +impl< + #[cfg(not(feature = "serde"))] T: Variable + 'static, + #[cfg(feature = "serde")] T: Variable + 'static + crate::serde::Tagged, + > VariableSafe for T +{ + fn clone_box(&self) -> Box { + Box::new((*self).clone()) + } + + fn dim(&self) -> usize { + self.dim() + } + + fn oplus_mut(&mut self, delta: VectorViewX) { + *self = self.oplus(delta); + } + + #[doc(hidden)] + #[cfg(feature = "serde")] + fn typetag_name(&self) -> &'static str { + Self::TAG + } + + #[doc(hidden)] + #[cfg(feature = "serde")] + fn typetag_deserialize(&self) {} +} + pub trait VariableUmbrella: VariableSafe + Variable = Self> { diff --git a/src/variables/vector.rs b/src/variables/vector.rs index 9e8ee73..9bcae2a 100644 --- a/src/variables/vector.rs +++ b/src/variables/vector.rs @@ -5,7 +5,6 @@ use std::{ use crate::{ dtype, - impl_safe_variable, linalg::{ AllocatorBuffer, Const, @@ -19,10 +18,11 @@ use crate::{ VectorViewX, VectorX, }, + tag_variable, variables::Variable, }; -impl_safe_variable!( +tag_variable!( VectorVar<1>, VectorVar<2>, VectorVar<3>,