diff --git a/Cargo.lock b/Cargo.lock index 99b647b..476d1e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "approx" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" +dependencies = [ + "num-traits", +] + [[package]] name = "arrayvec" version = "0.7.4" @@ -74,6 +83,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "bytemuck" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94bbb0ad554ad961ddc5da507a12a29b14e4ae5bda06b19f575a3e6079d2e2ae" + [[package]] name = "byteorder" version = "1.5.0" @@ -234,6 +249,16 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[package]] +name = "matrixmultiply" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9380b911e3e96d10c1f415da0876389aaf1b56759054eeb0de7df940c456ba1a" +dependencies = [ + "autocfg", + "rawpointer", +] + [[package]] name = "memchr" version = "2.7.2" @@ -249,6 +274,81 @@ dependencies = [ "autocfg", ] +[[package]] +name = "nalgebra" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c4b5f057b303842cf3262c27e465f4c303572e7f6b0648f60e16248ac3397f4" +dependencies = [ + "approx", + "matrixmultiply", + "nalgebra-macros", + "num-complex", + "num-rational", + "num-traits", + "simba", + "typenum", +] + +[[package]] +name = "nalgebra-macros" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "254a5372af8fc138e36684761d3c0cdb758a4410e938babcff1c860ce14ddbfc" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.60", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.19.0" @@ -278,6 +378,12 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pkg-config" version = "0.3.30" @@ -384,6 +490,12 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" + [[package]] name = "redox_syscall" version = "0.5.1" @@ -461,9 +573,18 @@ dependencies = [ "semver 1.0.22", ] +[[package]] +name = "safe_arch" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3460605018fdc9612bce72735cba0d27efbcd9904780d44c7e3a9948f96148a" +dependencies = [ + "bytemuck", +] + [[package]] name = "savefile" -version = "0.17.7" +version = "0.17.8" dependencies = [ "arrayvec", "bit-set 0.5.3", @@ -474,6 +595,7 @@ dependencies = [ "bzip2", "indexmap", "memoffset", + "nalgebra", "parking_lot", "quickcheck", "rand", @@ -488,7 +610,7 @@ dependencies = [ [[package]] name = "savefile-abi" -version = "0.17.7" +version = "0.17.8" dependencies = [ "byteorder", "libloading", @@ -529,7 +651,7 @@ dependencies = [ [[package]] name = "savefile-derive" -version = "0.17.7" +version = "0.17.8" dependencies = [ "proc-macro-error", "proc-macro2", @@ -557,6 +679,7 @@ dependencies = [ "byteorder", "indexmap", "insta", + "nalgebra", "parking_lot", "quickcheck", "quickcheck_macros", @@ -618,6 +741,19 @@ dependencies = [ "syn 2.0.60", ] +[[package]] +name = "simba" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3a386a501cd104797982c15ae17aafe8b9261315b5d07e3ec803f2ea26be0fa" +dependencies = [ + "approx", + "num-complex", + "num-traits", + "paste", + "wide", +] + [[package]] name = "similar" version = "2.5.0" @@ -658,6 +794,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -746,6 +888,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "wide" +version = "0.7.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b828f995bf1e9622031f8009f8481a85406ce1f4d4588ff746d872043e855690" +dependencies = [ + "bytemuck", + "safe_arch", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/README.md b/README.md index 0238fde..7143d4b 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,10 @@ See the docs for more information, including schema-versioning: https://docs.rs/ # Changelog +## 0.17.8 + +Support for some nalgebra types. + ## 0.17.7 Support for BTreeSet. diff --git a/savefile-abi/Cargo.toml b/savefile-abi/Cargo.toml index 423f5cf..33b2196 100644 --- a/savefile-abi/Cargo.toml +++ b/savefile-abi/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "savefile-abi" -version = "0.17.7" +version = "0.17.8" edition = "2021" authors = ["Anders Musikka "] documentation = "https://docs.rs/savefile-abi/" @@ -17,8 +17,8 @@ keywords = ["dylib", "dlopen", "ffi"] license = "MIT/Apache-2.0" [dependencies] -savefile = { path="../savefile", version = "=0.17.7" } -savefile-derive = { path="../savefile-derive", version = "=0.17.7" } +savefile = { path="../savefile", version = "=0.17.8" } +savefile-derive = { path="../savefile-derive", version = "=0.17.8" } byteorder = "1.4" libloading = "0.8" diff --git a/savefile-derive/Cargo.toml b/savefile-derive/Cargo.toml index c984b34..8f117fc 100644 --- a/savefile-derive/Cargo.toml +++ b/savefile-derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "savefile-derive" -version = "0.17.7" +version = "0.17.8" authors = ["Anders Musikka "] repository = "https://github.com/avl/savefile" rust-version = "1.74" diff --git a/savefile-test/Cargo.toml b/savefile-test/Cargo.toml index b7e9df5..f6e9011 100644 --- a/savefile-test/Cargo.toml +++ b/savefile-test/Cargo.toml @@ -11,8 +11,8 @@ external_benchmarks = [] nightly=["savefile/nightly"] [dependencies] -savefile = { path = "../savefile", features = ["size_sanity_checks", "encryption", "compression","bit-set","bit-vec","rustc-hash","serde_derive", "quickcheck"]} -savefile-derive = { path = "../savefile-derive", version = "=0.17.7" } +savefile = { path = "../savefile", features = ["size_sanity_checks", "encryption", "compression","bit-set","bit-vec","rustc-hash","serde_derive", "quickcheck", "nalgebra"]} +savefile-derive = { path = "../savefile-derive", version = "=0.17.8" } savefile-abi = { path = "../savefile-abi" } bit-vec = "0.8" arrayvec="0.7" @@ -29,7 +29,7 @@ rustc-hash="1.1" quickcheck="1.0" quickcheck_macros ="1.0" insta = { version = "1.38.0", features = ["yaml"] } - +nalgebra="0.33" [build-dependencies] rustc_version="0.4" diff --git a/savefile-test/src/lib.rs b/savefile-test/src/lib.rs index d734e7f..a8a8085 100644 --- a/savefile-test/src/lib.rs +++ b/savefile-test/src/lib.rs @@ -9,6 +9,7 @@ extern crate quickcheck; #[macro_use(quickcheck)] extern crate quickcheck_macros; +extern crate nalgebra; extern crate serde; #[macro_use] extern crate serde_derive; @@ -1711,3 +1712,22 @@ fn dummy_test() { let r = simple_add_call(&conn, 1, 2); assert_eq!(r, 3); } + +#[test] +fn test_isometry() { + let iso = nalgebra::Isometry3::::from_parts( + nalgebra::Point3::new(0.0,1.0,2.0).into(), + nalgebra::UnitQuaternion::from_euler_angles(0.0,1.0,2.0), + ); + assert_roundtrip(iso); +} +#[test] +fn test_points() { + let a = [nalgebra::Point3::new(1.0,2.0,3.0),nalgebra::Point3::new(4.0,5.0,6.0)]; + assert_roundtrip(a); +} +#[test] +fn test_vector() { + let a = [nalgebra::Point3::new(1.5,2.5,3.0),nalgebra::Point3::new(4.0,5.5,6.0)]; + assert_roundtrip(a); +} \ No newline at end of file diff --git a/savefile/Cargo.toml b/savefile/Cargo.toml index d17a00d..3e98c0e 100644 --- a/savefile/Cargo.toml +++ b/savefile/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "savefile" -version = "0.17.7" +version = "0.17.8" authors = ["Anders Musikka "] documentation = "https://docs.rs/savefile/" homepage = "https://github.com/avl/savefile/" @@ -49,6 +49,7 @@ derive = ["dep:savefile-derive"] [dependencies] bit-vec = { version = "0.6", optional = true} +nalgebra = { version = "0.33", optional = true} bit-vec08 = { package="bit-vec", version = "0.8", optional = true} arrayvec = { version = "0.7", optional = true} smallvec = { version = "1.11", optional = true} @@ -62,13 +63,13 @@ bit-set08 = {package="bit-set", version = "0.8", optional = true} rustc-hash = {version = "1.1", optional = true} memoffset = "0.9" byteorder = "1.4" -savefile-derive = {path="../savefile-derive", version = "=0.17.7", optional = true } +savefile-derive = {path="../savefile-derive", version = "=0.17.8", optional = true } serde_derive = {version= "1.0", optional = true} serde = {version= "1.0", optional = true} quickcheck = {version= "1.0", optional = true} [dev-dependencies] -savefile-derive = { path="../savefile-derive", version = "=0.17.7" } +savefile-derive = { path="../savefile-derive", version = "=0.17.8" } [build-dependencies] rustc_version="0.2" diff --git a/savefile/src/lib.rs b/savefile/src/lib.rs index 49f9284..ea369a2 100644 --- a/savefile/src/lib.rs +++ b/savefile/src/lib.rs @@ -2217,6 +2217,21 @@ pub fn save_compressed( Serializer::save::(writer, version, data, true) } +/// Write the given `data` to the file. Compresses data using 'bzip2' compression format. +/// +/// The current version of data must be `version`. +/// The resultant data can be loaded using the regular load_file-function (it autodetects if compressions was +/// active or not). +/// Note, this function will fail if the bzip2-feature is not enabled. +pub fn save_file_compressed>( + path: P, + version: u32, + data: &T, +) -> Result<(), SavefileError> { + let mut f = BufWriter::new(File::create(path)?); + Serializer::save::(&mut f, version, data, true) +} + /// Serialize the given data and return as a `Vec` /// The current version of data must be `version`. pub fn save_to_mem(version: u32, data: &T) -> Result, SavefileError> { @@ -6570,6 +6585,172 @@ impl Deserialize for (T1,) { } } + +impl Introspect for nalgebra::Point3 { + fn introspect_value(&self) -> String { + format!("{:?}", self) + } + + fn introspect_child<'a>(&'a self, _index: usize) -> Option + 'a>> { + None + } +} +impl Introspect for nalgebra::Vector3 { + fn introspect_value(&self) -> String { + format!("{:?}", self) + } + + fn introspect_child<'a>(&'a self, _index: usize) -> Option + 'a>> { + None + } +} +impl Introspect for nalgebra::Isometry3 { + fn introspect_value(&self) -> String { + format!("{:?}", self) + } + + fn introspect_child<'a>(&'a self, _index: usize) -> Option + 'a>> { + None + } +} +#[cfg(feature = "nalgebra")] +impl Packed for nalgebra::Point3 { + unsafe fn repr_c_optimization_safe(_version: u32) -> IsPacked { + let d = nalgebra::Point3::::new(T::default(),T::default(),T::default()); + let p1 = &d.x as *const T; + let p2 = &d.y as *const T; + let p3 = &d.z as *const T; + + if std::mem::size_of::>() == 3 * std::mem::size_of::() && + p1.offset(1) ==p2 && p1.offset(2) == p3 + { + IsPacked::yes() + } else { + IsPacked::no() + } + } +} +#[cfg(feature = "nalgebra")] +impl WithSchema for nalgebra::Point3 { + fn schema(version: u32, context: &mut WithSchemaContext) -> Schema { + Schema::Array(SchemaArray { + item_type: Box::new(T::schema(version, context)), + count: 3, + }) + } +} +#[cfg(feature = "nalgebra")] +impl Serialize for nalgebra::Point3 { + fn serialize(&self, serializer: &mut Serializer) -> Result<(), SavefileError> { + self.coords.x.serialize(serializer)?; + self.coords.y.serialize(serializer)?; + self.coords.z.serialize(serializer)?; + + Ok(()) + } +} +#[cfg(feature = "nalgebra")] +impl Deserialize for nalgebra::Point3 { + fn deserialize(deserializer: &mut Deserializer) -> Result { + Ok( + nalgebra::Point3::new(::deserialize(deserializer)?,::deserialize(deserializer)?,::deserialize(deserializer)?).into(), + ) + } +} + + + +#[cfg(feature = "nalgebra")] +impl Packed for nalgebra::Vector3 { + unsafe fn repr_c_optimization_safe(_version: u32) -> IsPacked { + let d = nalgebra::Vector3::::new(T::default(),T::default(),T::default()); + let p1 = &d.x as *const T; + let p2 = &d.y as *const T; + let p3 = &d.z as *const T; + + if std::mem::size_of::>() == 3 * std::mem::size_of::() && + p1.offset(1) ==p2 && p1.offset(2) == p3 + { + IsPacked::yes() + } else { + IsPacked::no() + } + } +} +#[cfg(feature = "nalgebra")] +impl WithSchema for nalgebra::Vector3 { + fn schema(version: u32, context: &mut WithSchemaContext) -> Schema { + Schema::Array(SchemaArray { + item_type: Box::new(T::schema(version, context)), + count: 3, + }) + } +} +#[cfg(feature = "nalgebra")] +impl Serialize for nalgebra::Vector3 { + fn serialize(&self, serializer: &mut Serializer) -> Result<(), SavefileError> { + self.x.serialize(serializer)?; + self.y.serialize(serializer)?; + self.z.serialize(serializer)?; + + Ok(()) + } +} +#[cfg(feature = "nalgebra")] +impl Deserialize for nalgebra::Vector3 { + fn deserialize(deserializer: &mut Deserializer) -> Result { + Ok( + nalgebra::Vector3::new(::deserialize(deserializer)?,::deserialize(deserializer)?,::deserialize(deserializer)?).into(), + ) + } +} + + +#[cfg(feature = "nalgebra")] +impl Packed for nalgebra::Isometry3 { +} +#[cfg(feature = "nalgebra")] +impl WithSchema for nalgebra::Isometry3 { + fn schema(version: u32, context: &mut WithSchemaContext) -> Schema { + Schema::Array(SchemaArray { + item_type: Box::new(T::schema(version, context)), + count: 7, + }) + } +} +#[cfg(feature = "nalgebra")] +impl Serialize for nalgebra::Isometry3 { + fn serialize(&self, serializer: &mut Serializer) -> Result<(), SavefileError> { + self.translation.vector.x.serialize(serializer)?; + self.translation.vector.y.serialize(serializer)?; + self.translation.vector.z.serialize(serializer)?; + + self.rotation.coords.w.serialize(serializer)?; + self.rotation.coords.x.serialize(serializer)?; + self.rotation.coords.y.serialize(serializer)?; + self.rotation.coords.z.serialize(serializer)?; + + Ok(()) + } +} +#[cfg(feature = "nalgebra")] +impl Deserialize for nalgebra::Isometry3 { + fn deserialize(deserializer: &mut Deserializer) -> Result { + Ok(nalgebra::Isometry3::from_parts( + nalgebra::Point3::new(::deserialize(deserializer)?,::deserialize(deserializer)?,::deserialize(deserializer)?).into(), + nalgebra::UnitQuaternion::new_unchecked( + nalgebra::Quaternion::new( + ::deserialize(deserializer)?, + ::deserialize(deserializer)?, + ::deserialize(deserializer)?, + ::deserialize(deserializer)?, + ) + )) + ) + } +} + + #[cfg(feature = "arrayvec")] impl Packed for arrayvec::ArrayString {}