Skip to content

Commit

Permalink
v0.16.2
Browse files Browse the repository at this point in the history
  • Loading branch information
avl committed Aug 4, 2023
1 parent 8ba270b commit 8c69430
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 65 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ fn main() {

# Changelog

## 0.16.2

Fixes multiple problems. The most impactful being that 0.16.1 could only be built
using a nightly rust compiler.

It also provides an optional feature to integrate 'savefile-derive' into 'savefile'.

Just activate the feature 'derive', and you can then use savefile without an explicit
dependency on 'savefile-derive'. Just do

```rust
use savefile::prelude::*;

#[derive(Savefile)]
struct MyStruct {
//...
}
```
And you're good to go!


## 0.16.1

Fix a minor issue where the ```#[savefile_introspect_ignore]``` was not accepted
Expand Down
2 changes: 1 addition & 1 deletion savefile-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "savefile-derive"
version = "0.16.1"
version = "0.16.2"
authors = ["Anders Musikka <[email protected]>"]

description = "Custom derive macros for savefile crate - simple, convenient, fast, versioned, binary serialization/deserialization library."
Expand Down
2 changes: 1 addition & 1 deletion savefile-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ fn get_enum_size(attrs: &Vec<syn::Attribute>) -> Option<u32> {
)]
pub fn reprc(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {

panic!("The #[derive(ReprC)] style of unsafe performance opt-in has been changed. Add a #[savefile_unsafe_and_fast] attribute on a new line, after the #[derive(Savefile)] attribute instead.")
panic!("The #[derive(ReprC)] style of unsafe performance opt-in has been removed. The performance gains are now available automatically for any packed struct.")
}
fn derive_reprc_new(input: DeriveInput) -> TokenStream {

Expand Down
3 changes: 1 addition & 2 deletions savefile-min-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
savefile = { path = "../savefile", features = [], default-features = false }
savefile-derive = { path = "../savefile-derive" }
savefile = { path = "../savefile", features = ["derive"], default-features = false }


8 changes: 8 additions & 0 deletions savefile-min-build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use savefile::prelude::*;
use std::fmt::Debug;
use std::io::{BufWriter, Cursor, Write};
use savefile::{Deserialize, Deserializer, Serialize, Serializer};


pub fn assert_roundtrip<E: Serialize + Deserialize + Debug + PartialEq>(sample: E) {
assert_roundtrip_version(sample, 0)
}
Expand All @@ -24,8 +26,14 @@ pub fn assert_roundtrip_version<E: Serialize + Deserialize + Debug + PartialEq>(
assert_eq!(f.position() as usize,f_internal_size);
}

#[derive(Savefile,PartialEq,Eq,Debug)]
struct SimpleStruct {
x: u32
}

#[test]
fn it_works() {
assert_roundtrip("Test-string".to_string());
assert_roundtrip(42i32);
assert_roundtrip(SimpleStruct{x:42});
}
30 changes: 28 additions & 2 deletions savefile-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,32 @@ pub fn test_array_string() {
assert_roundtrip(arraystr);
}

#[test]
pub fn test_array_vec() {
use arrayvec::ArrayVec;
let mut data:ArrayVec<u32, 30> = ArrayVec::new();
assert_roundtrip(data.clone());
data.push(47);
assert_roundtrip(data.clone());
data.push(1);
data.push(32);
data.push(49);
assert_roundtrip(data.clone());
}

#[test]
pub fn test_array_vec_with_string() {
use arrayvec::ArrayVec;
let mut data:ArrayVec<String, 30> = ArrayVec::new();
assert_roundtrip(data.clone());
data.push("hello".to_string());
assert_roundtrip(data.clone());
data.push("wonderful".to_string());
data.push("world".to_string());
data.push("how ya doing?".to_string());
assert_roundtrip(data.clone());
}

#[test]
pub fn test_smallvec0() {
let mut v = smallvec::SmallVec::<[u8;2]>::new();
Expand Down Expand Up @@ -1142,13 +1168,13 @@ struct MySimpleFuzz1 {
#[test]
pub fn fuzz_regression1() {
let mut data:&[u8] = &[0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 8, 3, 0, 3, 0, 64, 0, 0, 0];
let _ = load_noschema::<MySimpleFuzz1>(&mut data,0);
let _t:Result<MySimpleFuzz1,_> = load_noschema(&mut data,0);
}

#[test]
pub fn fuzz_regression2() {
let mut data:&[u8] = &[0, 0, 0, 0, 3, 11, 0, 254, 2, 1, 252, 255, 254];
let _ = load_noschema::<MySimpleFuzz1>(&mut data,0);
let _t:Result<MySimpleFuzz1,_> = load_noschema(&mut data,0);
}


Expand Down
7 changes: 4 additions & 3 deletions savefile/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "savefile"
version = "0.16.1"
version = "0.16.2"
authors = ["Anders Musikka <[email protected]>"]
documentation = "https://docs.rs/savefile/"
homepage = "https://github.com/avl/savefile/"
Expand Down Expand Up @@ -37,6 +37,8 @@ compression = ["bzip2"]

encryption = ["ring", "rand"]

derive = ["dep:savefile-derive"]

[dependencies]
bit-vec = { version = "0.6", optional = true}
arrayvec = { version = "0.7", optional = true}
Expand All @@ -49,9 +51,8 @@ bzip2 = {version = "0.4.4", optional = true}
bit-set = {version = "0.5", optional = true}
rustc-hash = {version = "1.1", optional = true}
memoffset = "0.9"

byteorder = "1.4"

savefile-derive = {path="../savefile-derive",version = "=0.16.2", optional = true }

[dev-dependencies]
savefile-derive = { path="../savefile-derive" }
Expand Down
80 changes: 24 additions & 56 deletions savefile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,8 @@ extern crate core;

extern crate memoffset;

#[cfg(feature="derive")]
extern crate savefile_derive;

/// This object represents an error in deserializing or serializing
/// an item.
Expand Down Expand Up @@ -1526,17 +1528,17 @@ impl<'a, W:Write+'a> Serializer<'a, W> {
pub unsafe fn raw_write_region<T, T1:ReprC,T2:ReprC>(&mut self, full: &T, t1: &T1, t2: &T2, version: u32) -> Result<(), SavefileError> {
assert!(T1::repr_c_optimization_safe(version).is_yes());
assert!(T2::repr_c_optimization_safe(version).is_yes());
unsafe {

let base = full as *const T as *const u8;
let totlen = std::mem::size_of::<T>();
let p1 = (t1 as *const T1 as *const u8) as usize;
let p2 = (t2 as *const T2 as *const u8) as usize;
let start = p1 - (base as usize);
let end = (p2 - (base as usize)) + std::mem::size_of::<T2>();
let full_slice = std::slice::from_raw_parts(base, totlen);
Ok(self.writer.write_all(&full_slice[start..end])?)
}

let base = full as *const T as *const u8;
let totlen = std::mem::size_of::<T>();
let p1 = (t1 as *const T1 as *const u8) as usize;
let p2 = (t2 as *const T2 as *const u8) as usize;
let start = p1 - (base as usize);
let end = (p2 - (base as usize)) + std::mem::size_of::<T2>();
let full_slice = std::slice::from_raw_parts(base, totlen);
Ok(self.writer.write_all(&full_slice[start..end])?)

}
/// Creata a new serializer.
/// Don't use this function directly, use the [crate::save] function instead.
Expand Down Expand Up @@ -3774,7 +3776,7 @@ impl Serialize for bit_vec::BitVec<u32> {
serializer.write_usize(l)?;
let storage = self.storage();
let rawbytes_ptr = storage.as_ptr() as *const u8;
let rawbytes :&[u8] = unsafe{slice::from_raw_parts(rawbytes_ptr,4*storage.len())};
let rawbytes :&[u8] = unsafe{std::slice::from_raw_parts(rawbytes_ptr,4*storage.len())};
serializer.write_usize(rawbytes.len()|(1<<63))?;
serializer.write_bytes(&rawbytes)?;
Ok(())
Expand All @@ -3799,7 +3801,7 @@ impl Deserialize for bit_vec::BitVec<u32> {
let storage = ret.storage_mut();
storage.resize(num_words, 0);
let storage_ptr = storage.as_ptr() as *mut u8;
let storage_bytes:&mut [u8] = slice::from_raw_parts_mut(storage_ptr,4*num_words);
let storage_bytes:&mut [u8] = std::slice::from_raw_parts_mut(storage_ptr,4*num_words);
deserializer.read_bytes_to_buf(storage_bytes)?;
ret.set_len(numbits);
}
Expand Down Expand Up @@ -4160,7 +4162,7 @@ fn regular_deserialize_vec<T: Deserialize>(deserializer: &mut Deserializer<impl
impl<T: Deserialize + ReprC> Deserialize for Vec<T> {
fn deserialize(deserializer: &mut Deserializer<impl Read>) -> Result<Self, SavefileError> {
if unsafe{T::repr_c_optimization_safe(deserializer.file_version)}.is_false() {
Ok(regular_deserialize_vec::<T>(deserializer)?)
Ok(regular_deserialize_vec(deserializer)?)
} else {
use std::mem;

Expand Down Expand Up @@ -4228,13 +4230,13 @@ impl<T: WithSchema> WithSchema for VecDeque<T> {
impl<T> ReprC for VecDeque<T> {}
impl<T: Serialize> Serialize for VecDeque<T> {
fn serialize(&self, serializer: &mut Serializer<impl Write>) -> Result<(), SavefileError> {
regular_serialize_vecdeque::<T>(self, serializer)
regular_serialize_vecdeque(self, serializer)
}
}

impl<T: Deserialize> Deserialize for VecDeque<T> {
fn deserialize(deserializer: &mut Deserializer<impl Read>) -> Result<Self, SavefileError> {
Ok(regular_deserialize_vecdeque::<T>(deserializer)?)
Ok(regular_deserialize_vecdeque(deserializer)?)
}
}

Expand Down Expand Up @@ -4628,26 +4630,16 @@ impl<V: Introspect + 'static, const C: usize> Introspect for arrayvec::ArrayVec<
}
}

#[cfg(all(feature = "nightly", feature="arrayvec"))]
impl<V, const C: usize> ReprC for arrayvec::ArrayVec<V,C> {}


#[cfg(all(feature = "nightly", feature="arrayvec"))]
impl<V: Serialize, const C: usize> Serialize for arrayvec::ArrayVec<V,C> {
default fn serialize(&self, serializer: &mut Serializer<impl Write>) -> Result<(), SavefileError> {
regular_serialize_vec(self, serializer)
#[cfg(feature="arrayvec")]
impl<V:ReprC, const C: usize> ReprC for arrayvec::ArrayVec<V,C> {
unsafe fn repr_c_optimization_safe(version: u32) -> IsReprC {
V::repr_c_optimization_safe(version)
}
}

#[cfg(all(not(feature = "nightly"), feature="arrayvec"))]
impl<V: Serialize, const C: usize> Serialize for arrayvec::ArrayVec<V,C> {
fn serialize(&self, serializer: &mut Serializer<impl Write>) -> Result<(), SavefileError> {
regular_serialize_vec(self, serializer)
}
}

#[cfg(all(feature = "nightly", feature="arrayvec"))]
impl<V: Serialize + ReprC + Copy, const C:usize> Serialize for arrayvec::ArrayVec<V,C> {
#[cfg(feature="arrayvec")]
impl<V: Serialize + ReprC, const C:usize> Serialize for arrayvec::ArrayVec<V,C> {
fn serialize(&self, serializer: &mut Serializer<impl Write>) -> Result<(), SavefileError> {
unsafe {
if V::repr_c_optimization_safe(serializer.version).is_false() {
Expand All @@ -4663,31 +4655,8 @@ impl<V: Serialize + ReprC + Copy, const C:usize> Serialize for arrayvec::ArrayVe
}
}
}
#[cfg(all(feature = "nightly", feature="arrayvec"))]
impl<V: Deserialize, const C:usize> Deserialize for arrayvec::ArrayVec<V,C> {
default fn deserialize(deserializer: &mut Deserializer<impl Read>) -> Result<arrayvec::ArrayVec<V,C>, SavefileError> {
let mut ret = arrayvec::ArrayVec::new();
let l = deserializer.read_usize()?;
for _ in 0..l {
ret.push(V::deserialize(deserializer)?);
}
Ok(ret)
}
}

#[cfg(all(not(feature = "nightly"), feature="arrayvec"))]
impl<V: Deserialize, const C: usize> Deserialize for arrayvec::ArrayVec<V,C> {
fn deserialize(deserializer: &mut Deserializer<impl Read>) -> Result<arrayvec::ArrayVec<V,C>, SavefileError> {
let mut ret = arrayvec::ArrayVec::new();
let l = deserializer.read_usize()?;
for _ in 0..l {
ret.push(V::deserialize(deserializer)?);
}
Ok(ret)
}
}

#[cfg(all(feature = "nightly", feature="arrayvec"))]
#[cfg(feature="arrayvec")]
impl<V: Deserialize + ReprC, const C: usize > Deserialize for arrayvec::ArrayVec<V,C> {
fn deserialize(deserializer: &mut Deserializer<impl Read>) -> Result<arrayvec::ArrayVec<V,C>, SavefileError> {
let mut ret = arrayvec::ArrayVec::new();
Expand Down Expand Up @@ -4775,7 +4744,6 @@ use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData;
use std::path::{PathBuf, Path};
use std::ptr::NonNull;
use std::slice;
use std::sync::Arc;


Expand Down
9 changes: 9 additions & 0 deletions savefile/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ pub use memoffset::span_of;

#[cfg(feature="ring")]
pub use super::{CryptoReader, CryptoWriter, save_encrypted_file, load_encrypted_file};

#[cfg(feature="derive")]
pub use savefile_derive::Savefile;
#[cfg(feature="derive")]
pub use savefile_derive::SavefileNoIntrospect;
#[cfg(feature="derive")]
pub use savefile_derive::SavefileIntrospectOnly;
#[cfg(feature="derive")]
pub use savefile_derive::ReprC;

0 comments on commit 8c69430

Please sign in to comment.