-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: derive
BorshSerialize
for recursive structures
- Loading branch information
dj8yf0μl
committed
Jul 5, 2023
1 parent
387c665
commit 57d8f4f
Showing
12 changed files
with
274 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use quote::quote; | ||
use syn::{Generics, Ident, WherePredicate}; | ||
|
||
pub fn compute_predicates(generics: &Generics, cratename: &Ident) -> Vec<WherePredicate> { | ||
let mut where_predicates = vec![]; | ||
for type_param in generics.type_params() { | ||
let type_param_name = &type_param.ident; | ||
where_predicates.push( | ||
syn::parse2(quote! { | ||
#type_param_name: #cratename::ser::BorshSerialize | ||
}) | ||
.unwrap(), | ||
); | ||
} | ||
where_predicates | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ve-internal/src/snapshots/borsh_derive_internal__struct_ser__tests__recursive_struct.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
source: borsh-derive-internal/src/struct_ser.rs | ||
expression: pretty_print_syn_str(&actual).unwrap() | ||
--- | ||
impl borsh::ser::BorshSerialize for CRecC { | ||
fn serialize<W: borsh::__private::maybestd::io::Write>( | ||
&self, | ||
writer: &mut W, | ||
) -> ::core::result::Result<(), borsh::__private::maybestd::io::Error> { | ||
borsh::BorshSerialize::serialize(&self.a, writer)?; | ||
borsh::BorshSerialize::serialize(&self.b, writer)?; | ||
Ok(()) | ||
} | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
.../src/snapshots/borsh_derive_internal__struct_ser__tests__simple_generic_tuple_struct.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
source: borsh-derive-internal/src/struct_ser.rs | ||
expression: pretty_print_syn_str(&actual).unwrap() | ||
--- | ||
impl<T> borsh::ser::BorshSerialize for TupleA<T> | ||
where | ||
T: borsh::ser::BorshSerialize, | ||
{ | ||
fn serialize<W: borsh::__private::maybestd::io::Write>( | ||
&self, | ||
writer: &mut W, | ||
) -> ::core::result::Result<(), borsh::__private::maybestd::io::Error> { | ||
borsh::BorshSerialize::serialize(&self.0, writer)?; | ||
borsh::BorshSerialize::serialize(&self.1, writer)?; | ||
Ok(()) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
borsh/tests/snapshots/test_generic_struct__generic_struct_hashmap.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
source: borsh/tests/test_generic_struct.rs | ||
expression: data | ||
--- | ||
[ | ||
5, | ||
0, | ||
0, | ||
0, | ||
102, | ||
105, | ||
101, | ||
108, | ||
100, | ||
2, | ||
0, | ||
0, | ||
0, | ||
14, | ||
0, | ||
0, | ||
0, | ||
5, | ||
0, | ||
0, | ||
0, | ||
118, | ||
97, | ||
108, | ||
117, | ||
101, | ||
34, | ||
0, | ||
0, | ||
0, | ||
7, | ||
0, | ||
0, | ||
0, | ||
97, | ||
110, | ||
111, | ||
116, | ||
104, | ||
101, | ||
114, | ||
] |
41 changes: 41 additions & 0 deletions
41
borsh/tests/snapshots/test_recursive_structs__recursive_struct.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
source: borsh/tests/test_recursive_structs.rs | ||
expression: data | ||
--- | ||
[ | ||
5, | ||
0, | ||
0, | ||
0, | ||
116, | ||
104, | ||
114, | ||
101, | ||
101, | ||
2, | ||
0, | ||
0, | ||
0, | ||
3, | ||
0, | ||
0, | ||
0, | ||
111, | ||
110, | ||
101, | ||
0, | ||
0, | ||
0, | ||
0, | ||
3, | ||
0, | ||
0, | ||
0, | ||
116, | ||
119, | ||
111, | ||
0, | ||
0, | ||
0, | ||
0, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#![cfg(feature = "derive")] | ||
use borsh::BorshSerialize; | ||
|
||
#[cfg(feature = "hashbrown")] | ||
use hashbrown::HashMap; | ||
|
||
#[cfg(feature = "std")] | ||
use std::collections::HashMap; | ||
|
||
#[cfg(not(feature = "std"))] | ||
extern crate alloc; | ||
#[cfg(not(feature = "std"))] | ||
use alloc::{boxed::Box, string::String, vec::Vec}; | ||
|
||
/// strangely enough, this worked before current commit | ||
#[cfg(hash_collections)] | ||
#[derive(BorshSerialize)] | ||
struct CRec<U: PartialOrd> { | ||
a: String, | ||
b: HashMap<U, CRec<U>>, | ||
} | ||
|
||
#[derive(BorshSerialize)] | ||
struct CRecA { | ||
a: String, | ||
b: Box<CRecA>, | ||
} | ||
|
||
#[derive(BorshSerialize, PartialEq, Eq)] | ||
struct CRecB { | ||
a: String, | ||
b: Vec<CRecB>, | ||
} | ||
|
||
#[cfg(hash_collections)] | ||
#[derive(BorshSerialize)] | ||
struct CRecC { | ||
a: String, | ||
b: HashMap<String, CRecC>, | ||
} | ||
|
||
#[test] | ||
fn test_recursive_struct() { | ||
let one = CRecB { | ||
a: "one".to_string(), | ||
b: vec![], | ||
}; | ||
let two = CRecB { | ||
a: "two".to_string(), | ||
b: vec![], | ||
}; | ||
|
||
let three = CRecB { | ||
a: "three".to_string(), | ||
b: vec![one, two], | ||
}; | ||
let _data = three.try_to_vec().unwrap(); | ||
#[cfg(feature = "std")] | ||
insta::assert_debug_snapshot!(_data); | ||
// let actual_three = from_slice::<CRecB>(&data).unwrap(); | ||
} |