Skip to content

default Field generics #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 29, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- `bool` and `u8` as default generics for `BitReader/Writer` and `FieldReader/Writer`
- Bump MSRV to 1.65
- Optimize case change/sanitize
- Fix dangling implicit derives
Expand Down
58 changes: 29 additions & 29 deletions src/generate/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ impl<REG: RegisterSpec> W<REG> {
}

#[doc(hidden)]
pub struct FieldReaderRaw<U, T> {
pub struct FieldReaderRaw<U, FI> {
pub(crate) bits: U,
_reg: marker::PhantomData<T>,
_reg: marker::PhantomData<FI>,
}

impl<U, FI> FieldReaderRaw<U, FI>
Expand All @@ -322,9 +322,9 @@ where
}

#[doc(hidden)]
pub struct BitReaderRaw<T> {
pub struct BitReaderRaw<FI> {
pub(crate) bits: bool,
_reg: marker::PhantomData<T>,
_reg: marker::PhantomData<FI>,
}

impl<FI> BitReaderRaw<FI> {
Expand All @@ -342,10 +342,10 @@ impl<FI> BitReaderRaw<FI> {
/// Field reader.
///
/// Result of the `read` methods of fields.
pub type FieldReader<U, FI> = FieldReaderRaw<U, FI>;
pub type FieldReader<U = u8, FI = u8> = FieldReaderRaw<U, FI>;

/// Bit-wise field reader
pub type BitReader<FI> = BitReaderRaw<FI>;
pub type BitReader<FI = bool> = BitReaderRaw<FI>;

impl<U, FI> FieldReader<U, FI>
where
Expand Down Expand Up @@ -405,7 +405,7 @@ pub struct Safe;
pub struct Unsafe;

#[doc(hidden)]
pub struct FieldWriterRaw<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
pub struct FieldWriterRaw<'a, U, REG, const WI: u8, const O: u8, N, FI, Safety>
where
REG: Writable + RegisterSpec<Ux = U>,
N: From<FI>,
Expand All @@ -414,8 +414,8 @@ where
_field: marker::PhantomData<(N, FI, Safety)>,
}

impl<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
FieldWriterRaw<'a, U, REG, N, FI, Safety, WI, O>
impl<'a, U, REG, const WI: u8, const O: u8, N, FI, Safety>
FieldWriterRaw<'a, U, REG, WI, O, N, FI, Safety>
where
REG: Writable + RegisterSpec<Ux = U>,
N: From<FI>,
Expand All @@ -432,7 +432,7 @@ where
}

#[doc(hidden)]
pub struct BitWriterRaw<'a, U, REG, FI, M, const O: u8>
pub struct BitWriterRaw<'a, U, REG, const O: u8, FI, M>
where
REG: Writable + RegisterSpec<Ux = U>,
bool: From<FI>,
Expand All @@ -441,7 +441,7 @@ where
_field: marker::PhantomData<(FI, M)>,
}

impl<'a, U, REG, FI, M, const O: u8> BitWriterRaw<'a, U, REG, FI, M, O>
impl<'a, U, REG, const O: u8, FI, M> BitWriterRaw<'a, U, REG, O, FI, M>
where
REG: Writable + RegisterSpec<Ux = U>,
bool: From<FI>,
Expand All @@ -458,13 +458,13 @@ where
}

/// Write field Proxy with unsafe `bits`
pub type FieldWriter<'a, U, REG, N, FI, const WI: u8, const O: u8> =
FieldWriterRaw<'a, U, REG, N, FI, Unsafe, WI, O>;
pub type FieldWriter<'a, U, REG, const WI: u8, const O: u8, N = u8, FI = u8> =
FieldWriterRaw<'a, U, REG, WI, O, N, FI, Unsafe>;
/// Write field Proxy with safe `bits`
pub type FieldWriterSafe<'a, U, REG, N, FI, const WI: u8, const O: u8> =
FieldWriterRaw<'a, U, REG, N, FI, Safe, WI, O>;
pub type FieldWriterSafe<'a, U, REG, const WI: u8, const O: u8, N = u8, FI = u8> =
FieldWriterRaw<'a, U, REG, WI, O, N, FI, Safe>;

impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, U, REG, N, FI, WI, OF>
impl<'a, U, REG, const WI: u8, const OF: u8, N, FI> FieldWriter<'a, U, REG, WI, OF, N, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
N: From<FI>,
Expand All @@ -473,7 +473,7 @@ where
pub const WIDTH: u8 = WI;
}

impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, U, REG, N, FI, WI, OF>
impl<'a, U, REG, const WI: u8, const OF: u8, N, FI> FieldWriterSafe<'a, U, REG, WI, OF, N, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
N: From<FI>,
Expand All @@ -488,9 +488,9 @@ macro_rules! bit_proxy {
pub struct $mwv;

/// Bit-wise write field proxy
pub type $writer<'a, U, REG, FI, const O: u8> = BitWriterRaw<'a, U, REG, FI, $mwv, O>;
pub type $writer<'a, U, REG, const O: u8, FI = bool> = BitWriterRaw<'a, U, REG, O, FI, $mwv>;

impl<'a, U, REG, FI, const OF: u8> $writer<'a, U, REG, FI, OF>
impl<'a, U, REG, const OF: u8, FI> $writer<'a, U, REG, OF, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
bool: From<FI>,
Expand All @@ -503,7 +503,7 @@ macro_rules! bit_proxy {

macro_rules! impl_bit_proxy {
($writer:ident) => {
impl<'a, U, REG, FI, const OF: u8> $writer<'a, U, REG, FI, OF>
impl<'a, U, REG, const OF: u8, FI> $writer<'a, U, REG, OF, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
U: RawReg,
Expand Down Expand Up @@ -533,7 +533,7 @@ bit_proxy!(BitWriter0S, Bit0S);
bit_proxy!(BitWriter1T, Bit1T);
bit_proxy!(BitWriter0T, Bit0T);

impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, U, REG, N, FI, WI, OF>
impl<'a, U, REG, const WI: u8, const OF: u8, N, FI> FieldWriter<'a, U, REG, WI, OF, N, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
U: RawReg + From<N>,
Expand All @@ -556,7 +556,7 @@ where
unsafe { self.bits(N::from(variant)) }
}
}
impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, U, REG, N, FI, WI, OF>
impl<'a, U, REG, const WI: u8, const OF: u8, N, FI> FieldWriterSafe<'a, U, REG, WI, OF, N, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
U: RawReg + From<N>,
Expand Down Expand Up @@ -584,7 +584,7 @@ impl_bit_proxy!(BitWriter0S);
impl_bit_proxy!(BitWriter1T);
impl_bit_proxy!(BitWriter0T);

impl<'a, U, REG, FI, const OF: u8> BitWriter<'a, U, REG, FI, OF>
impl<'a, U, REG, const OF: u8, FI> BitWriter<'a, U, REG, OF, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
U: RawReg,
Expand All @@ -604,7 +604,7 @@ where
}
}

impl<'a, U, REG, FI, const OF: u8> BitWriter1S<'a, U, REG, FI, OF>
impl<'a, U, REG, const OF: u8, FI> BitWriter1S<'a, U, REG, OF, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
U: RawReg,
Expand All @@ -618,7 +618,7 @@ where
}
}

impl<'a, U, REG, FI, const OF: u8> BitWriter0C<'a, U, REG, FI, OF>
impl<'a, U, REG, const OF: u8, FI> BitWriter0C<'a, U, REG, OF, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
U: RawReg,
Expand All @@ -632,7 +632,7 @@ where
}
}

impl<'a, U, REG, FI, const OF: u8> BitWriter1C<'a, U, REG, FI, OF>
impl<'a, U, REG, const OF: u8, FI> BitWriter1C<'a, U, REG, OF, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
U: RawReg,
Expand All @@ -646,7 +646,7 @@ where
}
}

impl<'a, U, REG, FI, const OF: u8> BitWriter0S<'a, U, REG, FI, OF>
impl<'a, U, REG, const OF: u8, FI> BitWriter0S<'a, U, REG, OF, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
U: RawReg,
Expand All @@ -660,7 +660,7 @@ where
}
}

impl<'a, U, REG, FI, const OF: u8> BitWriter1T<'a, U, REG, FI, OF>
impl<'a, U, REG, const OF: u8, FI> BitWriter1T<'a, U, REG, OF, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
U: RawReg,
Expand All @@ -674,7 +674,7 @@ where
}
}

impl<'a, U, REG, FI, const OF: u8> BitWriter0T<'a, U, REG, FI, OF>
impl<'a, U, REG, const OF: u8, FI> BitWriter0T<'a, U, REG, OF, FI>
where
REG: Writable + RegisterSpec<Ux = U>,
U: RawReg,
Expand Down
24 changes: 21 additions & 3 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,15 @@ pub fn fields(
// derive the read proxy structure if necessary.
if should_derive_reader {
let reader = if width == 1 {
quote! { crate::BitReader<#value_read_ty> }
if value_read_ty == "bool" {
quote! { crate::BitReader }
} else {
quote! { crate::BitReader<#value_read_ty> }
}
} else if fty == "u8" && value_read_ty == "u8" {
quote! { crate::FieldReader }
} else if value_read_ty == "u8" {
quote! { crate::FieldReader<#fty> }
} else {
quote! { crate::FieldReader<#fty, #value_read_ty> }
};
Expand Down Expand Up @@ -868,7 +876,11 @@ pub fn fields(
},
span,
);
quote! { crate::#wproxy<'a, #rty, #regspec_ident, #value_write_ty, O> }
if value_write_ty == "bool" {
quote! { crate::#wproxy<'a, #rty, #regspec_ident, O> }
} else {
quote! { crate::#wproxy<'a, #rty, #regspec_ident, O, #value_write_ty> }
}
} else {
let wproxy = Ident::new(
if unsafety {
Expand All @@ -879,7 +891,13 @@ pub fn fields(
span,
);
let width = &util::unsuffixed(width as _);
quote! { crate::#wproxy<'a, #rty, #regspec_ident, #fty, #value_write_ty, #width, O> }
if fty == "u8" && value_write_ty == "u8" {
quote! { crate::#wproxy<'a, #rty, #regspec_ident, #width, O> }
} else if value_write_ty == "u8" {
quote! { crate::#wproxy<'a, #rty, #regspec_ident, #width, O, #fty> }
} else {
quote! { crate::#wproxy<'a, #rty, #regspec_ident, #width, O, #fty, #value_write_ty> }
}
};
mod_items.extend(quote! {
#[doc = #field_writer_brief]
Expand Down