Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bilge-impl/src/bitsize_internal/struct_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub(crate) fn generate_getter_inner(ty: &Type, is_getter: bool) -> TokenStream {
// generate the real value from the arbint `elem_value`
quote! {
#elem_value
match #ty::try_from(elem_value) {
match <#ty>::try_from(elem_value) {
Ok(v) => v,
Err(_) => panic!("unreachable"),
}
Expand All @@ -186,7 +186,7 @@ pub(crate) fn generate_getter_inner(ty: &Type, is_getter: bool) -> TokenStream {
#elem_value
// so, has try_from impl
// note this is available even if the type is `From`
#ty::try_from(elem_value).is_ok()
<#ty>::try_from(elem_value).is_ok()
} }
}
}
Expand Down
45 changes: 45 additions & 0 deletions tests/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,48 @@ fn default_bits() {
let default = ArrayTupleDefault::default();
assert_eq!(default, ArrayTupleDefault::from(u34::new(0b1000_1000_1000_10_10_1000_01000_1000_01000)));
}

// quick 'n dirty -- consider something more robust if more manual implementations are needed
#[cfg(not(feature = "nightly"))]
macro_rules! impl_from (
($($generic:ident),*; $from_ty:ty => $to_ty:ty; |$name:ident| $expr:expr) => {
impl<$($generic),*> From<$from_ty> for $to_ty {
fn from($name: $from_ty) -> $to_ty {
$expr
}
}
}
);
#[cfg(feature = "nightly")]
macro_rules! impl_from (
($($generic:ident),*; $from_ty:ty => $to_ty:ty; |$name:ident| $expr:expr) => {
impl<$($generic),*> const From<$from_ty> for $to_ty {
fn from($name: $from_ty) -> $to_ty {
$expr
}
}
}
);

// do we want to be able to derive automatically on structs where the user has specified phantomdata?
Copy link
Copy Markdown
Owner

@hecatia-elegua hecatia-elegua Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be done by removing the PhantomData field in bitsize_internal and adding it back at the end. We should do that, in the other PR though.

// #[bitsize(2)]
// #[derive(DefaultBits, PartialEq, DebugBits, FromBits)]
#[derive(Default, Debug)]
struct Generic<T>(u2, std::marker::PhantomData<T>);

impl<T> Bitsized for Generic<T> {
type ArbitraryInt = u2;
const BITS: usize = u2::BITS;
const MAX: Self::ArbitraryInt = <u2 as Bitsized>::MAX;
}

impl_from!(T; u2 => Generic<T>; |val| Self(val, std::marker::PhantomData));
impl_from!(T; Generic<T> => u2; |val| val.0);

#[bitsize(2)]
#[derive(DefaultBits, PartialEq, DebugBits, FromBits)]
struct UsingGeneric(Generic<()>);

#[bitsize(2)]
#[derive(DefaultBits, PartialEq, DebugBits, TryFromBits)]
struct UsingGenericUnfilled(Generic<()>);