Skip to content

Commit

Permalink
Updated variable serde impl
Browse files Browse the repository at this point in the history
  • Loading branch information
contagon committed Jul 10, 2024
1 parent c2a6fe5 commit 1454896
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 29 deletions.
23 changes: 4 additions & 19 deletions src/variables/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn $crate::variables::VariableSafe> {
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);
)*};
}
4 changes: 2 additions & 2 deletions src/variables/se2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{fmt, ops};
use super::VectorVar2;
use crate::{
dtype,
impl_safe_variable,
linalg::{
dvector,
AllocatorBuffer,
Expand All @@ -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))]
Expand Down
4 changes: 2 additions & 2 deletions src/variables/se3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{fmt, ops};
use super::VectorVar3;
use crate::{
dtype,
impl_safe_variable,
linalg::{
AllocatorBuffer,
Const,
Expand All @@ -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))]
Expand Down
4 changes: 2 additions & 2 deletions src/variables/so2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{fmt, ops};

use crate::{
dtype,
impl_safe_variable,
linalg::{
dvector,
AllocatorBuffer,
Expand All @@ -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))]
Expand Down
4 changes: 2 additions & 2 deletions src/variables/so3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{fmt, ops};
use super::VectorVar4;
use crate::{
dtype,
impl_safe_variable,
linalg::{
dvector,
AllocatorBuffer,
Expand All @@ -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))]
Expand Down
28 changes: 28 additions & 0 deletions src/variables/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn VariableSafe> {
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<D: Numeric = dtype>:
VariableSafe + Variable<D, Alias<D> = Self>
{
Expand Down
4 changes: 2 additions & 2 deletions src/variables/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{

use crate::{
dtype,
impl_safe_variable,
linalg::{
AllocatorBuffer,
Const,
Expand All @@ -19,10 +18,11 @@ use crate::{
VectorViewX,
VectorX,
},
tag_variable,
variables::Variable,
};

impl_safe_variable!(
tag_variable!(
VectorVar<1>,
VectorVar<2>,
VectorVar<3>,
Expand Down

0 comments on commit 1454896

Please sign in to comment.