Skip to content

Commit 204f655

Browse files
committed
📝 Rename inner to repr
1 parent 7086ee2 commit 204f655

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

src/lib.rs

+40-30
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ fn s_err(span: proc_macro2::Span, msg: impl fmt::Display) -> syn::Error {
1616

1717
/// Creates a bitfield for this struct.
1818
///
19-
/// The arguments first, have to begin with the underlying type of the bitfield:
19+
/// The arguments first, have to begin with the integer type of the bitfield:
2020
/// For example: `#[bitfield(u64)]`.
2121
///
22-
/// It can contain an extra `debug` argument for disabling the `Debug` trait
23-
/// generation (`#[bitfield(u64, debug = false)]`).
22+
/// It can contain the following additional parameters, like the `debug` argument
23+
/// for disabling the `Debug` trait generation (`#[bitfield(u64, debug = false)]`).
2424
///
2525
/// Parameters of the `bitfield` attribute:
26-
/// - the bitfield type
26+
/// - the bitfield integer type (required)
27+
/// - `repr` specifies the bitfield's representation in memory
28+
/// - `from` to specify a conversion function from repr to the bitfield's integer type
29+
/// - `into` to specify a conversion function from the bitfield's integer type to repr
2730
/// - `debug` to disable the `Debug` trait generation
2831
/// - `default` to disable the `Default` trait generation
2932
/// - `order` to specify the bit order (Lsb, Msb)
@@ -47,7 +50,7 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
4750
let input = syn::parse2::<syn::ItemStruct>(input)?;
4851
let Params {
4952
ty,
50-
inner,
53+
repr,
5154
into,
5255
from,
5356
bits,
@@ -133,11 +136,11 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
133136
let conversion = if conversion {
134137
quote! {
135138
/// Convert from bits.
136-
#vis const fn from_bits(bits: #inner) -> Self {
139+
#vis const fn from_bits(bits: #repr) -> Self {
137140
Self(bits)
138141
}
139142
/// Convert into bits.
140-
#vis const fn into_bits(self) -> #inner {
143+
#vis const fn into_bits(self) -> #repr {
141144
self.0
142145
}
143146
}
@@ -149,7 +152,7 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
149152
#attrs
150153
#[derive(Copy, Clone)]
151154
#[repr(transparent)]
152-
#vis struct #name(#inner);
155+
#vis struct #name(#repr);
153156

154157
impl #name {
155158
/// Creates a new default initialized bitfield.
@@ -165,13 +168,13 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
165168

166169
#default_impl
167170

168-
impl From<#inner> for #name {
169-
fn from(v: #inner) -> Self {
171+
impl From<#repr> for #name {
172+
fn from(v: #repr) -> Self {
170173
Self(v)
171174
}
172175
}
173-
impl From<#name> for #inner {
174-
fn from(v: #name) -> #inner {
176+
impl From<#name> for #repr {
177+
fn from(v: #name) -> #repr {
175178
v.0
176179
}
177180
}
@@ -185,8 +188,8 @@ struct Member {
185188
offset: usize,
186189
bits: usize,
187190
base_ty: syn::Type,
188-
inner_into: Option<syn::Path>,
189-
inner_from: Option<syn::Path>,
191+
repr_into: Option<syn::Path>,
192+
repr_from: Option<syn::Path>,
190193
default: TokenStream,
191194
inner: Option<MemberInner>,
192195
}
@@ -204,8 +207,8 @@ impl Member {
204207
fn new(
205208
base_ty: syn::Type,
206209
base_bits: usize,
207-
inner_into: Option<syn::Path>,
208-
inner_from: Option<syn::Path>,
210+
repr_into: Option<syn::Path>,
211+
repr_from: Option<syn::Path>,
209212
f: syn::Field,
210213
offset: usize,
211214
order: Order,
@@ -274,8 +277,8 @@ impl Member {
274277
offset,
275278
bits,
276279
base_ty,
277-
inner_into,
278-
inner_from,
280+
repr_into,
281+
repr_from,
279282
default,
280283
inner: Some(MemberInner {
281284
ident,
@@ -295,8 +298,8 @@ impl Member {
295298
offset,
296299
bits,
297300
base_ty,
298-
inner_into,
299-
inner_from,
301+
repr_into,
302+
repr_from,
300303
default,
301304
inner: None,
302305
})
@@ -335,8 +338,8 @@ impl ToTokens for Member {
335338
offset,
336339
bits,
337340
base_ty,
338-
inner_into,
339-
inner_from,
341+
repr_into,
342+
repr_from,
340343
default: _,
341344
inner:
342345
Some(MemberInner {
@@ -382,7 +385,7 @@ impl ToTokens for Member {
382385
#[doc = #location]
383386
#vis const fn #ident(&self) -> #ty {
384387
let mask = #base_ty::MAX >> (#base_ty::BITS - Self::#bits_ident as u32);
385-
let this = (#inner_into(self.0) >> Self::#offset_ident) & mask;
388+
let this = (#repr_into(self.0) >> Self::#offset_ident) & mask;
386389
#from
387390
}
388391
});
@@ -399,8 +402,8 @@ impl ToTokens for Member {
399402
let mask = #base_ty::MAX >> (#base_ty::BITS - Self::#bits_ident as u32);
400403
#[allow(unused_comparisons)]
401404
debug_assert!(value <= mask, "value out of bounds");
402-
let bits = #inner_into(self.0) & !(mask << Self::#offset_ident) | (value & mask) << Self::#offset_ident;
403-
Self(#inner_from(bits))
405+
let bits = #repr_into(self.0) & !(mask << Self::#offset_ident) | (value & mask) << Self::#offset_ident;
406+
Self(#repr_from(bits))
404407
}
405408

406409
#doc
@@ -678,7 +681,7 @@ enum Order {
678681
/// The bitfield macro parameters
679682
struct Params {
680683
ty: syn::Type,
681-
inner: syn::Type,
684+
repr: syn::Type,
682685
into: Option<syn::Path>,
683686
from: Option<syn::Path>,
684687
bits: usize,
@@ -698,7 +701,7 @@ impl Parse for Params {
698701
return Err(s_err(input.span(), "unsupported type"));
699702
}
700703

701-
let mut inner = ty.clone();
704+
let mut repr = None;
702705
let mut from = None;
703706
let mut into = None;
704707
let mut debug = true;
@@ -711,8 +714,8 @@ impl Parse for Params {
711714
let ident = Ident::parse(input)?;
712715
<Token![=]>::parse(input)?;
713716
match ident.to_string().as_str() {
714-
"inner" => {
715-
inner = input.parse()?;
717+
"repr" => {
718+
repr = Some(input.parse()?);
716719
}
717720
"from" => {
718721
from = Some(input.parse()?);
@@ -740,9 +743,16 @@ impl Parse for Params {
740743
};
741744
}
742745

746+
if repr.is_some() != from.is_some() || repr.is_some() != into.is_some() {
747+
return Err(s_err(
748+
input.span(),
749+
"`repr` requires both `from` and `into`",
750+
));
751+
}
752+
743753
Ok(Self {
754+
repr: repr.unwrap_or_else(|| ty.clone()),
744755
ty,
745-
inner,
746756
from,
747757
into,
748758
bits,

tests/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ fn raw() {
401401

402402
#[test]
403403
fn custom_inner() {
404-
#[bitfield(u32, inner = CustomInner, from = CustomInner::from_inner, into = CustomInner::to_inner)]
404+
#[bitfield(u32, repr = CustomInner, from = CustomInner::from_inner, into = CustomInner::to_inner)]
405405
#[derive(PartialEq, Eq)]
406406
struct MyBitfield {
407407
data: u32,

0 commit comments

Comments
 (0)