Skip to content

Commit 5bc8c34

Browse files
bors[bot]burrbull
andauthored
Merge #722
722: FieldSpec r=Emilgardis a=burrbull Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
2 parents 8f63b56 + 6c115bd commit 5bc8c34

File tree

3 files changed

+67
-51
lines changed

3 files changed

+67
-51
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
- `FieldFpec` instead or `fty` generic
1011
- removed `rty` generic in `FieldWriter`
1112
- `bool` and `u8` as default generics for `BitReader/Writer` and `FieldReader/Writer`
1213
- Bump MSRV to 1.65

src/generate/generic.rs

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ macro_rules! raw_reg {
3333
const fn $mask<const WI: u8>() -> $U {
3434
<$U>::MAX >> ($size - WI)
3535
}
36+
impl FieldSpec for $U {
37+
type Ux = $U;
38+
}
3639
};
3740
}
3841

@@ -47,6 +50,12 @@ pub trait RegisterSpec {
4750
type Ux: RawReg;
4851
}
4952

53+
/// Raw field type
54+
pub trait FieldSpec: Sized {
55+
/// Raw field type (`u8`, `u16`, `u32`, ...).
56+
type Ux: Copy + PartialEq + From<Self>;
57+
}
58+
5059
/// Trait implemented by readable registers to enable the `read` method.
5160
///
5261
/// Registers marked with `Writable` can be also be `modify`'ed.
@@ -301,19 +310,19 @@ impl<REG: RegisterSpec> W<REG> {
301310
}
302311

303312
#[doc(hidden)]
304-
pub struct FieldReaderRaw<U, FI> {
305-
pub(crate) bits: U,
313+
pub struct FieldReaderRaw<FI = u8>
314+
where
315+
FI: FieldSpec
316+
{
317+
pub(crate) bits: FI::Ux,
306318
_reg: marker::PhantomData<FI>,
307319
}
308320

309-
impl<U, FI> FieldReaderRaw<U, FI>
310-
where
311-
U: Copy,
312-
{
321+
impl<FI: FieldSpec> FieldReaderRaw<FI> {
313322
/// Creates a new instance of the reader.
314323
#[allow(unused)]
315324
#[inline(always)]
316-
pub(crate) fn new(bits: U) -> Self {
325+
pub(crate) fn new(bits: FI::Ux) -> Self {
317326
Self {
318327
bits,
319328
_reg: marker::PhantomData,
@@ -322,7 +331,7 @@ where
322331
}
323332

324333
#[doc(hidden)]
325-
pub struct BitReaderRaw<FI> {
334+
pub struct BitReaderRaw<FI = bool> {
326335
pub(crate) bits: bool,
327336
_reg: marker::PhantomData<FI>,
328337
}
@@ -342,30 +351,26 @@ impl<FI> BitReaderRaw<FI> {
342351
/// Field reader.
343352
///
344353
/// Result of the `read` methods of fields.
345-
pub type FieldReader<N = u8, FI = u8> = FieldReaderRaw<N, FI>;
354+
pub type FieldReader<FI = u8> = FieldReaderRaw<FI>;
346355

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

350-
impl<N, FI> FieldReader<N, FI>
351-
where
352-
N: Copy,
353-
{
359+
impl<FI: FieldSpec> FieldReader<FI> {
354360
/// Reads raw bits from field.
355361
#[inline(always)]
356-
pub fn bits(&self) -> N {
362+
pub fn bits(&self) -> FI::Ux {
357363
self.bits
358364
}
359365
}
360366

361-
impl<N, FI> PartialEq<FI> for FieldReader<N, FI>
367+
impl<FI> PartialEq<FI> for FieldReader<FI>
362368
where
363-
FI: Copy,
364-
N: PartialEq + From<FI>,
369+
FI: FieldSpec + Copy,
365370
{
366371
#[inline(always)]
367372
fn eq(&self, other: &FI) -> bool {
368-
self.bits.eq(&N::from(*other))
373+
self.bits.eq(&FI::Ux::from(*other))
369374
}
370375
}
371376

@@ -404,20 +409,20 @@ pub struct Safe;
404409
pub struct Unsafe;
405410

406411
#[doc(hidden)]
407-
pub struct FieldWriterRaw<'a, REG, const WI: u8, const O: u8, N, FI, Safety>
412+
pub struct FieldWriterRaw<'a, REG, const WI: u8, const O: u8, FI = u8, Safety = Unsafe>
408413
where
409414
REG: Writable + RegisterSpec,
410-
N: From<FI>,
415+
FI: FieldSpec,
411416
{
412417
pub(crate) w: &'a mut REG::Writer,
413-
_field: marker::PhantomData<(N, FI, Safety)>,
418+
_field: marker::PhantomData<(FI, Safety)>,
414419
}
415420

416-
impl<'a, REG, const WI: u8, const O: u8, N, FI, Safety>
417-
FieldWriterRaw<'a, REG, WI, O, N, FI, Safety>
421+
impl<'a, REG, const WI: u8, const O: u8, FI, Safety>
422+
FieldWriterRaw<'a, REG, WI, O, FI, Safety>
418423
where
419424
REG: Writable + RegisterSpec,
420-
N: From<FI>,
425+
FI: FieldSpec,
421426
{
422427
/// Creates a new instance of the writer
423428
#[allow(unused)]
@@ -431,7 +436,7 @@ where
431436
}
432437

433438
#[doc(hidden)]
434-
pub struct BitWriterRaw<'a, REG, const O: u8, FI, M>
439+
pub struct BitWriterRaw<'a, REG, const O: u8, FI = bool, M = BitM>
435440
where
436441
REG: Writable + RegisterSpec,
437442
bool: From<FI>,
@@ -457,25 +462,25 @@ where
457462
}
458463

459464
/// Write field Proxy with unsafe `bits`
460-
pub type FieldWriter<'a, REG, const WI: u8, const O: u8, N = u8, FI = u8> =
461-
FieldWriterRaw<'a, REG, WI, O, N, FI, Unsafe>;
465+
pub type FieldWriter<'a, REG, const WI: u8, const O: u8, FI = u8> =
466+
FieldWriterRaw<'a, REG, WI, O, FI, Unsafe>;
462467
/// Write field Proxy with safe `bits`
463-
pub type FieldWriterSafe<'a, REG, const WI: u8, const O: u8, N = u8, FI = u8> =
464-
FieldWriterRaw<'a, REG, WI, O, N, FI, Safe>;
468+
pub type FieldWriterSafe<'a, REG, const WI: u8, const O: u8, FI = u8> =
469+
FieldWriterRaw<'a, REG, WI, O, FI, Safe>;
465470

466-
impl<'a, REG, const WI: u8, const OF: u8, N, FI> FieldWriter<'a, REG, WI, OF, N, FI>
471+
impl<'a, REG, const WI: u8, const OF: u8, FI> FieldWriter<'a, REG, WI, OF, FI>
467472
where
468473
REG: Writable + RegisterSpec,
469-
N: From<FI>,
474+
FI: FieldSpec,
470475
{
471476
/// Field width
472477
pub const WIDTH: u8 = WI;
473478
}
474479

475-
impl<'a, REG, const WI: u8, const OF: u8, N, FI> FieldWriterSafe<'a, REG, WI, OF, N, FI>
480+
impl<'a, REG, const WI: u8, const OF: u8, FI> FieldWriterSafe<'a, REG, WI, OF, FI>
476481
where
477482
REG: Writable + RegisterSpec,
478-
N: From<FI>,
483+
FI: FieldSpec,
479484
{
480485
/// Field width
481486
pub const WIDTH: u8 = WI;
@@ -531,46 +536,46 @@ bit_proxy!(BitWriter0S, Bit0S);
531536
bit_proxy!(BitWriter1T, Bit1T);
532537
bit_proxy!(BitWriter0T, Bit0T);
533538

534-
impl<'a, REG, const WI: u8, const OF: u8, N, FI> FieldWriter<'a, REG, WI, OF, N, FI>
539+
impl<'a, REG, const WI: u8, const OF: u8, FI> FieldWriter<'a, REG, WI, OF, FI>
535540
where
536541
REG: Writable + RegisterSpec,
537-
REG::Ux: From<N>,
538-
N: From<FI>,
542+
FI: FieldSpec,
543+
REG::Ux: From<FI::Ux>,
539544
{
540545
/// Writes raw bits to the field
541546
///
542547
/// # Safety
543548
///
544549
/// Passing incorrect value can cause undefined behaviour. See reference manual
545550
#[inline(always)]
546-
pub unsafe fn bits(self, value: N) -> &'a mut REG::Writer {
551+
pub unsafe fn bits(self, value: FI::Ux) -> &'a mut REG::Writer {
547552
self.w.bits &= !(REG::Ux::mask::<WI>() << OF);
548553
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << OF;
549554
self.w
550555
}
551556
/// Writes `variant` to the field
552557
#[inline(always)]
553558
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
554-
unsafe { self.bits(N::from(variant)) }
559+
unsafe { self.bits(FI::Ux::from(variant)) }
555560
}
556561
}
557-
impl<'a, REG, const WI: u8, const OF: u8, N, FI> FieldWriterSafe<'a, REG, WI, OF, N, FI>
562+
impl<'a, REG, const WI: u8, const OF: u8, FI> FieldWriterSafe<'a, REG, WI, OF, FI>
558563
where
559564
REG: Writable + RegisterSpec,
560-
REG::Ux: From<N>,
561-
N: From<FI>,
565+
FI: FieldSpec,
566+
REG::Ux: From<FI::Ux>,
562567
{
563568
/// Writes raw bits to the field
564569
#[inline(always)]
565-
pub fn bits(self, value: N) -> &'a mut REG::Writer {
570+
pub fn bits(self, value: FI::Ux) -> &'a mut REG::Writer {
566571
self.w.bits &= !(REG::Ux::mask::<WI>() << OF);
567572
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << OF;
568573
self.w
569574
}
570575
/// Writes `variant` to the field
571576
#[inline(always)]
572577
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
573-
self.bits(N::from(variant))
578+
self.bits(FI::Ux::from(variant))
574579
}
575580
}
576581

src/generate/register.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -691,12 +691,10 @@ pub fn fields(
691691
} else {
692692
quote! { crate::BitReader<#value_read_ty> }
693693
}
694-
} else if fty == "u8" && value_read_ty == "u8" {
695-
quote! { crate::FieldReader }
696694
} else if value_read_ty == "u8" {
697-
quote! { crate::FieldReader<#fty> }
695+
quote! { crate::FieldReader }
698696
} else {
699-
quote! { crate::FieldReader<#fty, #value_read_ty> }
697+
quote! { crate::FieldReader<#value_read_ty> }
700698
};
701699
let mut readerdoc = field_reader_brief.clone();
702700
if let Some(action) = f.read_action {
@@ -1032,12 +1030,10 @@ pub fn fields(
10321030
span,
10331031
);
10341032
let width = &util::unsuffixed(width as _);
1035-
if fty == "u8" && value_write_ty == "u8" {
1033+
if value_write_ty == "u8" {
10361034
quote! { crate::#wproxy<'a, #regspec_ident, #width, O> }
1037-
} else if value_write_ty == "u8" {
1038-
quote! { crate::#wproxy<'a, #regspec_ident, #width, O, #fty> }
10391035
} else {
1040-
quote! { crate::#wproxy<'a, #regspec_ident, #width, O, #fty, #value_write_ty> }
1036+
quote! { crate::#wproxy<'a, #regspec_ident, #width, O, #value_write_ty> }
10411037
}
10421038
};
10431039
mod_items.extend(quote! {
@@ -1246,6 +1242,13 @@ fn add_with_no_variants(
12461242
}
12471243
}
12481244
});
1245+
if fty != "bool" {
1246+
mod_items.extend(quote! {
1247+
impl crate::FieldSpec for #pc {
1248+
type Ux = #fty;
1249+
}
1250+
});
1251+
}
12491252
}
12501253

12511254
fn add_from_variants(
@@ -1295,6 +1298,13 @@ fn add_from_variants(
12951298
}
12961299
}
12971300
});
1301+
if fty != "bool" {
1302+
mod_items.extend(quote! {
1303+
impl crate::FieldSpec for #pc {
1304+
type Ux = #fty;
1305+
}
1306+
});
1307+
}
12981308
}
12991309

13001310
fn calculate_offset(

0 commit comments

Comments
 (0)