Skip to content

Commit

Permalink
Exclude self bounds (#87)
Browse files Browse the repository at this point in the history
* add test for ser with self bound

* ignore self bound in derive macros
  • Loading branch information
knickish authored Nov 4, 2023
1 parent 49b6123 commit 2a9e153
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions derive/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub(crate) fn struct_bounds_strings(struct_: &Struct, bound_name: &str) -> (Stri
return ("".to_string(), "".to_string());
}
let mut generic_w_bounds = "<".to_string();
for generic in generics.iter() {
for generic in generics.iter().filter(|g| g.ident_only() != "Self") {
generic_w_bounds += generic
.full_with_const(&[format!("nanoserde::{}", bound_name).as_str()], true)
.as_str();
Expand All @@ -82,7 +82,7 @@ pub(crate) fn struct_bounds_strings(struct_: &Struct, bound_name: &str) -> (Stri
generic_w_bounds += ">";

let mut generic_no_bounds = "<".to_string();
for generic in generics.iter() {
for generic in generics.iter().filter(|g| g.ident_only() != "Self") {
generic_no_bounds += generic.ident_only().as_str();
generic_no_bounds += ", ";
}
Expand All @@ -97,7 +97,7 @@ pub(crate) fn enum_bounds_strings(enum_: &Enum, bound_name: &str) -> (String, St
return ("".to_string(), "".to_string());
}
let mut generic_w_bounds = "<".to_string();
for generic in generics.iter() {
for generic in generics.iter().filter(|g| g.ident_only() != "Self") {
generic_w_bounds += generic
.full_with_const(&[format!("nanoserde::{}", bound_name).as_str()], true)
.as_str();
Expand All @@ -106,7 +106,7 @@ pub(crate) fn enum_bounds_strings(enum_: &Enum, bound_name: &str) -> (String, St
generic_w_bounds += ">";

let mut generic_no_bounds = "<".to_string();
for generic in generics.iter() {
for generic in generics.iter().filter(|g| g.ident_only() != "Self") {
generic_no_bounds += generic.ident_only().as_str();
generic_no_bounds += ", ";
}
Expand Down
24 changes: 24 additions & 0 deletions tests/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ fn field_proxy() {
assert!(test == test_deserialized);
}

#[test]
fn field_ignore_self_bound() {
#[derive(SerBin)]
pub struct Serializable<'a>
where
Self: 'a,
{
foo: &'a i32,
}

#[derive(DeBin)]
pub struct DeSerializable {
foo: i32,
}

let foo_base = 42;

let test = Serializable { foo: &42 };

let bytes = SerBin::serialize_bin(&test);
let deser: DeSerializable = DeBin::deserialize_bin(&bytes).unwrap();
assert_eq!(foo_base, deser.foo);
}

#[test]
fn struct_proxy() {
#[derive(PartialEq, Debug)]
Expand Down

0 comments on commit 2a9e153

Please sign in to comment.