use matio_rs::{Mat, MatFile, MayBeFrom, MayBeInto}; use tempfile::NamedTempFile; #[derive(Debug, Default)] struct SMat { a: f64, b: Vec, s: Nested, } #[derive(Debug, Default, Clone)] struct Nested { a: f64, b: Vec, } impl<'a> MayBeFrom<&Nested> for Mat<'a> { fn maybe_from>(name: S, data: &Nested) -> matio_rs::Result where Self: Sized, { let mats: Vec = vec![ Mat::maybe_from("a", data.a)?, Mat::maybe_from("b", &data.b)?, ]; MayBeFrom::maybe_from(name, mats) } } impl<'a> MayBeInto for &Mat<'a> { fn maybe_into(self) -> matio_rs::Result { Ok(Nested { a: self.field("a")?.get(0).unwrap().maybe_into()?, b: self.field("b")?.get(0).unwrap().maybe_into()?, }) } } impl<'a> MayBeFrom<&SMat> for Mat<'a> { fn maybe_from>(name: S, data: &SMat) -> matio_rs::Result where Self: Sized, { let mats: Vec = vec![ Mat::maybe_from("a", data.a)?, Mat::maybe_from("b", &data.b)?, Mat::maybe_from("s", &data.s)?, ]; MayBeFrom::maybe_from(name, mats) } } impl<'a> MayBeInto for Mat<'a> { fn maybe_into(self) -> matio_rs::Result { Ok(SMat { a: self.field("a")?.get(0).unwrap().maybe_into()?, b: self.field("b")?.get(0).unwrap().maybe_into()?, s: self.field("s")?.get(0).unwrap().maybe_into()?, }) } } fn main() -> anyhow::Result<()> { let n = Nested { a: 1f64, b: vec![2, 3, 4, 5], }; let a = SMat { a: 1f64, b: vec![2, 3, 4, 5], s: n.clone(), }; let file = NamedTempFile::new().unwrap(); MatFile::save(&file)?.var("a", &a)?; let aa: SMat = MatFile::load(file)?.var("a")?; dbg!(aa); Ok(()) }