Skip to content

Commit

Permalink
so we are backward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Nov 3, 2024
1 parent 910bb08 commit de5f32f
Show file tree
Hide file tree
Showing 24 changed files with 547 additions and 652 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ members = ["borsh", "borsh-derive", "fuzz/fuzz-run", "benchmarks"]
[workspace.package]
# shared version of all public crates in the workspace
version = "1.5.1"
# rust-version = "1.67.0"
rust-version = "1.81.0"
15 changes: 10 additions & 5 deletions borsh-derive/src/internals/attributes/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ pub fn check_attributes(derive_input: &DeriveInput) -> Result<(), Error> {
}

pub(crate) fn contains_use_discriminant(input: &ItemEnum) -> Result<bool, syn::Error> {
if input.variants.len() > 256 {
return Err(syn::Error::new(
input.span(),
"up to 256 enum variants are supported",
));
}
let attrs: &Vec<Attribute> = &input.attrs;
let mut use_discriminant = None;
let attr = attrs.iter().find(|attr| attr.path() == BORSH);
Expand Down Expand Up @@ -75,14 +81,13 @@ pub(crate) fn contains_use_discriminant(input: &ItemEnum) -> Result<bool, syn::E
Ok(use_discriminant.unwrap_or(false))
}

pub(crate) fn get_may_be_repr(inpute: &ItemEnum) -> Result<Option<(TypePath, Span)>, syn::Error> {
inpute
pub(crate) fn get_maybe_rust_repr(input: &ItemEnum) -> Option<(TypePath, Span)> {
input
.attrs
.iter()
.find(|attr| attr.path() == RUST_REPR)
.map(|attr| attr.parse_args::<TypePath>().map(|value| (attr, value)))
.transpose()
.map(|(attr, value)| value.map(|value| (value, attr.unwrap().span())))
.map(|attr| attr.parse_args::<TypePath>().map(|value| (attr, value)).unwrap())
.map(|(attr, value)| (value, attr.span()))
}

pub(crate) fn get_maybe_borsh_tag_width(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: borsh-derive/src/internals/attributes/item/mod.rs
assertion_line: 247
assertion_line: 262
expression: actual.unwrap_err()
---
Error(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: borsh-derive/src/internals/attributes/item/mod.rs
assertion_line: 233
assertion_line: 248
expression: actual.unwrap_err()
---
Error(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: borsh-derive/src/internals/attributes/item/mod.rs
assertion_line: 332
assertion_line: 347
expression: actual.unwrap_err()
---
Error(
Expand Down
52 changes: 23 additions & 29 deletions borsh-derive/src/internals/deserialize/enums/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,17 @@ pub fn process(input: &ItemEnum, cratename: Path) -> syn::Result<TokenStream2> {
let mut variant_arms = TokenStream2::new();
let use_discriminant = item::contains_use_discriminant(input)?;
let maybe_borsh_tag_width = item::get_maybe_borsh_tag_width(input)?;
let discriminants = Discriminants::new(&input.variants, maybe_borsh_tag_width)?;
let maybe_rust_repr = item::get_maybe_rust_repr(input);
let discriminants = Discriminants::new(&input.variants, maybe_borsh_tag_width, maybe_rust_repr, use_discriminant)?;
let mut generics_output = deserialize::GenericsOutput::new(&generics);
let discriminant_type = discriminants.discriminant_type();
for (variant_idx, variant) in input.variants.iter().enumerate() {
let variant_body = process_variant(variant, &cratename, &mut generics_output)?;
let variant_ident = &variant.ident;

let discriminant_value = discriminants.get(variant_ident, use_discriminant, variant_idx)?;
let discriminant_value = discriminants.get(variant_ident, variant_idx)?;
variant_arms.extend(quote! {
if TryInto::<(#discriminant_type)>::try_into(variant_tag).map_err(|_|
return #cratename::io::Error::new(
#cratename::io::ErrorKind::InvalidData,
#cratename::__private::maybestd::format!("Unexpected variant tag: {:?}", variant_tag),
)
)?
== #discriminant_value { #name::#variant_ident #variant_body } else
if variant_tag == #discriminant_value { #name::#variant_ident #variant_body } else
});
}
let init = if let Some(method_ident) = item::contains_initialize_with(&input.attrs)? {
Expand All @@ -39,36 +34,35 @@ pub fn process(input: &ItemEnum, cratename: Path) -> syn::Result<TokenStream2> {
};
generics_output.extend(&mut where_clause, &cratename);

let x = quote! {
let deserialize_variant = quote! {
let mut return_value =
#variant_arms {
return Err(#cratename::io::Error::new(
#cratename::io::ErrorKind::InvalidData,
#cratename::__private::maybestd::format!("Unexpected variant tag: {:?}", variant_tag),
))
};
#init
Ok(return_value)
};

Ok(quote! {
impl #impl_generics #cratename::de::BorshDeserialize for #name #ty_generics #where_clause {
fn deserialize_reader<__R: #cratename::io::Read>(reader: &mut __R) -> ::core::result::Result<Self, #cratename::io::Error> {
let tag = <#discriminant_type as #cratename::de::BorshDeserialize>::deserialize_reader(reader)?;
<Self as #cratename::de::EnumExt>::deserialize_variant::<_, #discriminant_type>(reader, tag)
let variant_tag = <#discriminant_type as #cratename::de::BorshDeserialize>::deserialize_reader(reader)?;
#deserialize_variant
}
}

impl #impl_generics #cratename::de::EnumExt for #name #ty_generics #where_clause {
fn deserialize_variant<__R: #cratename::io::Read,
Tag: borsh::BorshDeserialize + ::core::fmt::Debug + Eq
+ ::core::convert::TryInto<u16> + ::core::convert::TryInto<u8> + Copy
>(
fn deserialize_variant<__R: #cratename::io::Read>(
reader: &mut __R,
variant_tag: Tag,
variant_tag: u8,
) -> ::core::result::Result<Self, #cratename::io::Error> {
use ::core::convert::TryInto;
let mut return_value =
#variant_arms {
return Err(#cratename::io::Error::new(
#cratename::io::ErrorKind::InvalidData,
#cratename::__private::maybestd::format!("Unexpected variant tag: {:?}", variant_tag),
))
};
#init
Ok(return_value)
#deserialize_variant
}
}
};
Ok(x)
})
}

fn process_variant(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,91 +1,56 @@
---
source: borsh-derive/src/internals/deserialize/enums/mod.rs
assertion_line: 306
assertion_line: 300
expression: pretty_print_syn_str(&actual).unwrap()
---
impl borsh::de::BorshDeserialize for X {
fn deserialize_reader<__R: borsh::io::Read>(
reader: &mut __R,
) -> ::core::result::Result<Self, borsh::io::Error> {
let tag = <u8 as borsh::de::BorshDeserialize>::deserialize_reader(reader)?;
<Self as borsh::de::EnumExt>::deserialize_variant::<_, u8>(reader, tag)
let variant_tag = <u8 as borsh::de::BorshDeserialize>::deserialize_reader(
reader,
)?;
let mut return_value = if variant_tag == 0u8 {
X::A
} else if variant_tag == 1u8 {
X::B
} else if variant_tag == 2u8 {
X::C
} else if variant_tag == 3u8 {
X::D
} else if variant_tag == 4u8 {
X::E
} else if variant_tag == 5u8 {
X::F
} else {
return Err(
borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
),
)
};
Ok(return_value)
}
}
impl borsh::de::EnumExt for X {
fn deserialize_variant<
__R: borsh::io::Read,
Tag: borsh::BorshDeserialize + ::core::fmt::Debug + Eq
+ ::core::convert::TryInto<u16> + ::core::convert::TryInto<u8> + Copy,
>(
fn deserialize_variant<__R: borsh::io::Read>(
reader: &mut __R,
variant_tag: Tag,
variant_tag: u8,
) -> ::core::result::Result<Self, borsh::io::Error> {
use ::core::convert::TryInto;
let mut return_value = if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 0u8
{
let mut return_value = if variant_tag == 0u8 {
X::A
} else if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 1u8
{
} else if variant_tag == 1u8 {
X::B
} else if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 2u8
{
} else if variant_tag == 2u8 {
X::C
} else if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 3u8
{
} else if variant_tag == 3u8 {
X::D
} else if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 4u8
{
} else if variant_tag == 4u8 {
X::E
} else if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 5u8
{
} else if variant_tag == 5u8 {
X::F
} else {
return Err(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,91 +1,56 @@
---
source: borsh-derive/src/internals/deserialize/enums/mod.rs
assertion_line: 324
assertion_line: 318
expression: pretty_print_syn_str(&actual).unwrap()
---
impl borsh::de::BorshDeserialize for X {
fn deserialize_reader<__R: borsh::io::Read>(
reader: &mut __R,
) -> ::core::result::Result<Self, borsh::io::Error> {
let tag = <u8 as borsh::de::BorshDeserialize>::deserialize_reader(reader)?;
<Self as borsh::de::EnumExt>::deserialize_variant::<_, u8>(reader, tag)
let variant_tag = <u8 as borsh::de::BorshDeserialize>::deserialize_reader(
reader,
)?;
let mut return_value = if variant_tag == 0 {
X::A
} else if variant_tag == 20 {
X::B
} else if variant_tag == 20 + 1 {
X::C
} else if variant_tag == 20 + 1 + 1 {
X::D
} else if variant_tag == 10 {
X::E
} else if variant_tag == 10 + 1 {
X::F
} else {
return Err(
borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
),
)
};
Ok(return_value)
}
}
impl borsh::de::EnumExt for X {
fn deserialize_variant<
__R: borsh::io::Read,
Tag: borsh::BorshDeserialize + ::core::fmt::Debug + Eq
+ ::core::convert::TryInto<u16> + ::core::convert::TryInto<u8> + Copy,
>(
fn deserialize_variant<__R: borsh::io::Read>(
reader: &mut __R,
variant_tag: Tag,
variant_tag: u8,
) -> ::core::result::Result<Self, borsh::io::Error> {
use ::core::convert::TryInto;
let mut return_value = if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 0
{
let mut return_value = if variant_tag == 0 {
X::A
} else if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 20
{
} else if variant_tag == 20 {
X::B
} else if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 20 + 1
{
} else if variant_tag == 20 + 1 {
X::C
} else if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 20 + 1 + 1
{
} else if variant_tag == 20 + 1 + 1 {
X::D
} else if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 10
{
} else if variant_tag == 10 {
X::E
} else if TryInto::<(u8)>::try_into(variant_tag)
.map_err(|_| {
return borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
borsh::__private::maybestd::format!(
"Unexpected variant tag: {:?}", variant_tag
),
);
})? == 10 + 1
{
} else if variant_tag == 10 + 1 {
X::F
} else {
return Err(
Expand Down
Loading

0 comments on commit de5f32f

Please sign in to comment.