Skip to content

Commit

Permalink
SendSync
Browse files Browse the repository at this point in the history
  • Loading branch information
avl committed Oct 11, 2024
1 parent 968cabc commit c141205
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 55 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ members = [
"savefile-abi-min-lib-impl"
]
exclude = ["compile_tests"]

resolver = "2"

[profile.dev.package]
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ See the docs for more information, including schema-versioning: https://docs.rs/

# Changelog

## 0.17.11

Support Sync + Send-bounds for dyn Fn parameters, in savefile-abi. Also, reduce
risk of variable name conflicts between generated code and user code.

## 0.17.10

Support for serializing the std::io::Error type.
Expand Down
6 changes: 3 additions & 3 deletions savefile-abi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "savefile-abi"
version = "0.17.10"
version = "0.17.11"
edition = "2021"
authors = ["Anders Musikka <[email protected]>"]
documentation = "https://docs.rs/savefile-abi/"
Expand All @@ -17,8 +17,8 @@ keywords = ["dylib", "dlopen", "ffi"]
license = "MIT/Apache-2.0"

[dependencies]
savefile = { path="../savefile", version = "=0.17.10" }
savefile-derive = { path="../savefile-derive", version = "=0.17.10" }
savefile = { path="../savefile", version = "=0.17.11" }
savefile-derive = { path="../savefile-derive", version = "=0.17.11" }
byteorder = "1.4"
libloading = "0.8"

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.17.10"
version = "0.17.11"
authors = ["Anders Musikka <[email protected]>"]
repository = "https://github.com/avl/savefile"
rust-version = "1.74"
Expand Down
29 changes: 15 additions & 14 deletions savefile-derive/src/savefile_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ fn parse_type(
);
}
})
.filter(|seg| seg.ident != "Send" && seg.ident != "Sync")
.collect();
if type_bounds.len() == 0 {
abort!(trait_obj.bounds.span(), "{}, unsupported trait object reference. Only &dyn Trait is supported. Encountered zero traits.", location);
Expand Down Expand Up @@ -1062,15 +1063,15 @@ pub(super) fn generate_method_definitions(

let compile_time_known_size = compile_time_known_size + 4; //Space for 'version'
arg_buffer = quote! {
let mut rawdata = [0u8;#compile_time_known_size];
let mut data = Cursor::new(&mut rawdata[..]);
let mut __savefile_internal_datarawdata = [0u8;#compile_time_known_size];
let mut __savefile_internal_data = Cursor::new(&mut __savefile_internal_datarawdata[..]);
};
data_as_ptr = quote!(rawdata[..].as_ptr());
data_as_ptr = quote!(__savefile_internal_datarawdata[..].as_ptr());
data_length = quote!( #compile_time_known_size );
} else {
arg_buffer = quote!( let mut data = FlexBuffer::new(); );
data_as_ptr = quote!(data.as_ptr() as *const u8);
data_length = quote!(data.len());
arg_buffer = quote!( let mut __savefile_internal_data = FlexBuffer::new(); );
data_as_ptr = quote!(__savefile_internal_data.as_ptr() as *const u8);
data_length = quote!(__savefile_internal_data.len());
}

let _ = caller_return_type;
Expand All @@ -1093,7 +1094,7 @@ pub(super) fn generate_method_definitions(
#(#caller_arg_serializers_temp)*

let mut serializer = Serializer {
writer: &mut data,
writer: &mut __savefile_internal_data,
file_version: self.template.effective_version,
};
serializer.write_u32(self.template.effective_version).unwrap();
Expand Down Expand Up @@ -1162,21 +1163,21 @@ pub(super) fn generate_method_definitions(

let compile_time_known_size = compile_time_known_size + 4; //Space for 'version'
ret_buffer = quote! {
let mut rawdata = [0u8;#compile_time_known_size];
let mut data = Cursor::new(&mut rawdata[..]);
let mut __savefile_internal_datarawdata = [0u8;#compile_time_known_size];
let mut __savefile_internal_data = Cursor::new(&mut __savefile_internal_datarawdata[..]);
};
data_as_ptr = quote!(rawdata[..].as_ptr());
data_as_ptr = quote!(__savefile_internal_datarawdata[..].as_ptr());
data_length = quote!( #compile_time_known_size );
} else {
ret_buffer = quote!( let mut data = FlexBuffer::new(); );
data_as_ptr = quote!(data.as_ptr() as *const u8);
data_length = quote!(data.len());
ret_buffer = quote!( let mut __savefile_internal_data = FlexBuffer::new(); );
data_as_ptr = quote!(__savefile_internal_data.as_ptr() as *const u8);
data_length = quote!(__savefile_internal_data.len());
}

handle_retval = quote! {
#ret_buffer
let mut serializer = Serializer {
writer: &mut data,
writer: &mut __savefile_internal_data,
file_version: #version,
};

Expand Down
4 changes: 3 additions & 1 deletion savefile-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "savefile-test"
version = "0.0.1"
authors = ["Anders Musikka <[email protected]>"]
resolver = "2"
edition = "2021"

[features]
default = ["external_benchmarks"]
# Enable this to reduce risk of crashing on corrupt input. Provides sanity checks for sizes of objects.
Expand All @@ -12,7 +14,7 @@ nightly=["savefile/nightly"]

[dependencies]
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.10" }
savefile-derive = { path = "../savefile-derive", version = "=0.17.11" }
savefile-abi = { path = "../savefile-abi" }
bit-vec = "0.8"
arrayvec="0.7"
Expand Down
4 changes: 2 additions & 2 deletions savefile-test/src/cycles.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use assert_roundtrip;
use crate::assert_roundtrip;
use savefile::{get_schema, Removed, WithSchema, WithSchemaContext};

#[derive(Savefile, Debug, PartialEq)]
Expand Down Expand Up @@ -65,6 +65,6 @@ struct Version2Base(Option<Box<Version2LevelA>>);
expected = "Saved schema differs from in-memory schema for version 0. Error: At location [./Version1Base/0/?/Version1LevelA/0/?/Version1LevelB/0/Version1LevelC/0]: Application protocol uses recursion up 3 levels, but foreign format uses 2"
)]
fn cycles_vertest1() {
use assert_roundtrip_to_new_version;
use crate::assert_roundtrip_to_new_version;
assert_roundtrip_to_new_version(Version1Base(None), 0, Version2Base(None), 1);
}
4 changes: 2 additions & 2 deletions savefile-test/src/enum_variant_versioning.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use assert_roundtrip_version;
use crate::assert_roundtrip_version;
use savefile::{Packed, Removed};
use {assert_roundtrip, assert_roundtrip_to_new_version};
use crate::{assert_roundtrip, assert_roundtrip_to_new_version};

#[repr(u8)]
#[derive(Savefile, Debug, PartialEq)]
Expand Down
42 changes: 41 additions & 1 deletion savefile-test/src/savefile_abi_test/advanced_datatypes_test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use savefile_abi::AbiConnection;
use savefile_abi::AbiExportable;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

#[savefile_abi_exportable(version = 0)]
pub trait SimpleInterface {
fn do_call(&self, x: u32) -> u32;
}
#[savefile_abi_exportable(version = 0)]
pub trait AdvancedTestInterface {
pub trait AdvancedTestInterface : Send{
fn roundtrip_hashmap(&self, x: HashMap<String, String>) -> HashMap<String, String>;
fn clone_hashmap(&self, x: &HashMap<String, String>) -> HashMap<String, String>;

Expand All @@ -17,6 +18,9 @@ pub trait AdvancedTestInterface {
fn return_boxed_closure(&self) -> Box<dyn Fn() -> u32>;
fn return_boxed_closure2(&self) -> Box<dyn Fn()>;
fn many_callbacks(&mut self, x: &mut dyn FnMut(&dyn Fn(&dyn Fn() -> u32) -> u32) -> u32) -> u32;

fn buf_callback(&mut self, cb: Box<dyn Fn(&[u8], String) + Send + Sync>);

}
struct SimpleImpl;

Expand Down Expand Up @@ -60,8 +64,44 @@ impl AdvancedTestInterface for AdvancedTestInterfaceImpl {
fn many_callbacks(&mut self, x: &mut dyn FnMut(&dyn Fn(&dyn Fn() -> u32) -> u32) -> u32) -> u32 {
x(&|y| y())
}

fn buf_callback(&mut self, cb: Box<dyn Fn(&[u8], String) + Send + Sync>) {
cb(&[1,2,3], "hello".to_string())
}
}

struct TestUser(Box<dyn AdvancedTestInterface + 'static>);

pub trait DummyTrait2 : Send {

}

impl DummyTrait2 for TestUser {

}
fn require_send<T:Send>(_t: T) {

}
#[test]
fn abi_test_buf_send() {
let boxed: Box<dyn AdvancedTestInterface + Send + Sync> = Box::new(AdvancedTestInterfaceImpl {});
require_send(boxed);
}

#[test]
fn abi_test_buf_callback() {
let boxed: Box<dyn AdvancedTestInterface> = Box::new(AdvancedTestInterfaceImpl {});
let mut conn = AbiConnection::from_boxed_trait(boxed).unwrap();
let buf = Arc::new(Mutex::new(None));
let bufclone = Arc::clone(&buf);
conn.buf_callback(Box::new(move|argbuf, _s|{
*bufclone.lock().unwrap() = Some(argbuf.to_vec());
}));
let mut guard = buf.lock().unwrap();
let vec = guard.take().unwrap();
assert_eq!(vec, [1,2,3]);

}
#[test]
fn abi_test_slice() {
let boxed: Box<dyn AdvancedTestInterface> = Box::new(AdvancedTestInterfaceImpl {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use savefile::prelude::AbiRemoved;
use savefile::{get_schema, SavefileError, WithSchemaContext};
use savefile_abi::RawAbiCallResult::AbiError;
use savefile_abi::{verify_compatiblity, AbiConnection, AbiExportable};
use savefile_abi_test::argument_backward_compatibility::v1::{ArgInterfaceV1, EnumArgument, Implementation1};
use savefile_abi_test::argument_backward_compatibility::v2::{ArgInterfaceV2, Implementation2};
use savefile_abi_test::basic_abi_tests::CowSmuggler;
use crate::savefile_abi_test::argument_backward_compatibility::v1::{ArgInterfaceV1, EnumArgument, Implementation1};
use crate::savefile_abi_test::argument_backward_compatibility::v2::{ArgInterfaceV2, Implementation2};
use crate::savefile_abi_test::basic_abi_tests::CowSmuggler;
use savefile_derive::Savefile;

mod v1 {
Expand Down
6 changes: 3 additions & 3 deletions savefile-test/src/savefile_abi_test/closure_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(non_camel_case_types)]
use savefile_abi::{AbiConnection, AbiExportable};
use savefile_abi_test::basic_abi_tests::{CallbackImpl, TestInterface, TestInterfaceImpl};
use savefile_abi_test::closure_tests::new_version::ExampleImplementationNewer;
use crate::savefile_abi_test::basic_abi_tests::{CallbackImpl, TestInterface, TestInterfaceImpl};
use crate::savefile_abi_test::closure_tests::new_version::ExampleImplementationNewer;

#[derive(Savefile)]
pub struct CustomArg {
Expand Down Expand Up @@ -128,7 +128,7 @@ fn test_closure_with_custom_return_call_older() {
//let old_def = <dyn ExampleNewer as AbiExportable>::get_definition(0);
//println!("Old def: {:#?}", old_def);
{
use savefile_abi_test::closure_tests::new_version::Example;
use crate::savefile_abi_test::closure_tests::new_version::Example;
let result = conn.call_closure_return_custom_arg(&|arg| new_version::CustomArg {
x: arg.x,
y: "hej".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion savefile-test/src/savefile_abi_test/enum_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use savefile_abi::{AbiConnection, AbiExportable};
use savefile_abi_test::basic_abi_tests::{TestInterface, TestInterfaceImpl};
use crate::savefile_abi_test::basic_abi_tests::{TestInterface, TestInterfaceImpl};

#[derive(Savefile, Clone)]
#[repr(C, u8)]
Expand Down
2 changes: 1 addition & 1 deletion savefile-test/src/test_arrayvec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use arrayvec::ArrayVec;
use assert_roundtrip;
use crate::assert_roundtrip;

#[test]
pub fn test_arrayvec0() {
Expand Down
2 changes: 1 addition & 1 deletion savefile-test/src/test_enum_many_variants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use assert_roundtrip;
use crate::assert_roundtrip;

#[repr(u16)]
#[derive(Savefile, Debug, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion savefile-test/src/test_generic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use assert_roundtrip;
use crate::assert_roundtrip;
use savefile::prelude::*;
use std::fmt::Debug;
use std::marker::{PhantomData, PhantomPinned};
Expand Down
2 changes: 1 addition & 1 deletion savefile-test/src/test_introspect.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use parking_lot::{Mutex, RwLock};
use roundtrip;
use crate::roundtrip;
use savefile::prelude::*;
use savefile::{IntrospectedElementKey, IntrospectionError, Introspector, IntrospectorNavCommand};
use std::cell::RefCell;
Expand Down
2 changes: 1 addition & 1 deletion savefile-test/src/test_nested_non_repr_c.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(test)]
use roundtrip;
use crate::roundtrip;
use savefile::prelude::*;

#[derive(Clone, Copy, Debug, PartialEq, Savefile)]
Expand Down
Loading

0 comments on commit c141205

Please sign in to comment.