Skip to content

Commit 04cab1a

Browse files
committed
Register reader/writer use reg spec, not type alias
1 parent 85ec683 commit 04cab1a

File tree

2 files changed

+83
-35
lines changed

2 files changed

+83
-35
lines changed

src/generate/generic.rs

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ where
8787
/// let flag = reader.field2().bit_is_set();
8888
/// ```
8989
#[inline(always)]
90-
pub fn read(&self) -> R<REG::Ux, Self> {
90+
pub fn read(&self) -> R<REG> {
9191
R {
9292
bits: self.register.get(),
9393
_reg: marker::PhantomData,
@@ -139,7 +139,7 @@ where
139139
#[inline(always)]
140140
pub fn write<F>(&self, f: F)
141141
where
142-
F: FnOnce(&mut W<REG::Ux, Self>) -> &mut W<REG::Ux, Self>,
142+
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
143143
{
144144
self.register.set(
145145
f(&mut W {
@@ -162,7 +162,7 @@ where
162162
#[inline(always)]
163163
pub fn write_with_zero<F>(&self, f: F)
164164
where
165-
F: FnOnce(&mut W<REG::Ux, Self>) -> &mut W<REG::Ux, Self>,
165+
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
166166
{
167167
self.register.set(
168168
f(&mut W {
@@ -199,7 +199,7 @@ where
199199
#[inline(always)]
200200
pub fn modify<F>(&self, f: F)
201201
where
202-
for<'w> F: FnOnce(&R<REG::Ux, Self>, &'w mut W<REG::Ux, Self>) -> &'w mut W<REG::Ux, Self>,
202+
for<'w> F: FnOnce(&R<REG>, &'w mut W<REG>) -> &'w mut W<REG>,
203203
{
204204
let bits = self.register.get();
205205
self.register.set(
@@ -218,47 +218,38 @@ where
218218
}
219219
}
220220

221-
/// Register/field reader.
221+
/// Register reader.
222222
///
223223
/// Result of the `read` methods of registers. Also used as a closure argument in the `modify`
224224
/// method.
225-
pub struct R<U, T> {
226-
pub(crate) bits: U,
227-
_reg: marker::PhantomData<T>,
225+
pub struct R<REG: Register> {
226+
pub(crate) bits: REG::Ux,
227+
_reg: marker::PhantomData<REG>,
228228
}
229229

230-
impl<U, T> R<U, T>
230+
impl<REG: Register> R<REG>
231231
where
232-
U: Copy,
232+
REG::Ux: Copy,
233233
{
234-
/// Creates a new instance of the reader.
234+
/// Reads raw bits from register.
235235
#[inline(always)]
236-
pub(crate) fn new(bits: U) -> Self {
237-
Self {
238-
bits,
239-
_reg: marker::PhantomData,
240-
}
241-
}
242-
243-
/// Reads raw bits from register/field.
244-
#[inline(always)]
245-
pub fn bits(&self) -> U {
236+
pub fn bits(&self) -> REG::Ux {
246237
self.bits
247238
}
248239
}
249240

250-
impl<U, T, FI> PartialEq<FI> for R<U, T>
241+
impl<REG: Register, FI> PartialEq<FI> for R<REG>
251242
where
252-
U: PartialEq,
253-
FI: Copy + Into<U>,
243+
REG::Ux: PartialEq,
244+
FI: Copy + Into<REG::Ux>,
254245
{
255246
#[inline(always)]
256247
fn eq(&self, other: &FI) -> bool {
257248
self.bits.eq(&(*other).into())
258249
}
259250
}
260251

261-
impl<FI> R<bool, FI> {
252+
impl<REG: Register<Ux=bool>> R<REG> {
262253
/// Value of the field as raw bits.
263254
#[inline(always)]
264255
pub fn bit(&self) -> bool {
@@ -279,16 +270,16 @@ impl<FI> R<bool, FI> {
279270
/// Register writer.
280271
///
281272
/// Used as an argument to the closures in the `write` and `modify` methods of the register.
282-
pub struct W<U, T> {
273+
pub struct W<REG: Register> {
283274
///Writable bits
284-
pub(crate) bits: U,
285-
_reg: marker::PhantomData<T>,
275+
pub(crate) bits: REG::Ux,
276+
_reg: marker::PhantomData<REG>,
286277
}
287278

288-
impl<U, T> W<U, T> {
279+
impl<REG: Register> W<REG> {
289280
/// Writes raw bits to the register.
290281
#[inline(always)]
291-
pub unsafe fn bits(&mut self, bits: U) -> &mut Self {
282+
pub unsafe fn bits(&mut self, bits: REG::Ux) -> &mut Self {
292283
self.bits = bits;
293284
self
294285
}
@@ -302,3 +293,60 @@ pub enum Variant<U, T> {
302293
/// Raw bits.
303294
Res(U),
304295
}
296+
297+
/// Field reader.
298+
///
299+
/// Result of the `read` methods of fields.
300+
pub struct FieldReader<U, T> {
301+
pub(crate) bits: U,
302+
_reg: marker::PhantomData<T>,
303+
}
304+
305+
impl<U, T> FieldReader<U, T>
306+
where
307+
U: Copy,
308+
{
309+
/// Creates a new instance of the reader.
310+
#[inline(always)]
311+
pub(crate) fn new(bits: U) -> Self {
312+
Self {
313+
bits,
314+
_reg: marker::PhantomData,
315+
}
316+
}
317+
318+
/// Reads raw bits from field.
319+
#[inline(always)]
320+
pub fn bits(&self) -> U {
321+
self.bits
322+
}
323+
}
324+
325+
impl<U, T, FI> PartialEq<FI> for FieldReader<U, T>
326+
where
327+
U: PartialEq,
328+
FI: Copy + Into<U>,
329+
{
330+
#[inline(always)]
331+
fn eq(&self, other: &FI) -> bool {
332+
self.bits.eq(&(*other).into())
333+
}
334+
}
335+
336+
impl<FI> FieldReader<bool, FI> {
337+
/// Value of the field as raw bits.
338+
#[inline(always)]
339+
pub fn bit(&self) -> bool {
340+
self.bits
341+
}
342+
/// Returns `true` if the bit is clear (0).
343+
#[inline(always)]
344+
pub fn bit_is_clear(&self) -> bool {
345+
!self.bit()
346+
}
347+
/// Returns `true` if the bit is set (1).
348+
#[inline(always)]
349+
pub fn bit_is_set(&self) -> bool {
350+
self.bit()
351+
}
352+
}

src/generate/register.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn render(
5555
let desc = format!("Reader of register {}", register.name);
5656
mod_items.extend(quote! {
5757
#[doc = #desc]
58-
pub type R = crate::R<#rty, super::#name_pc>;
58+
pub type R = crate::R<super::#u_name_pc>;
5959
});
6060
methods.push("read");
6161
}
@@ -65,7 +65,7 @@ pub fn render(
6565
let desc = format!("Writer for register {}", register.name);
6666
mod_items.extend(quote! {
6767
#[doc = #desc]
68-
pub type W = crate::W<#rty, super::#name_pc>;
68+
pub type W = crate::W<super::#u_name_pc>;
6969
});
7070
if let Some(rv) = res_val.map(util::hex) {
7171
let doc = format!("Register {} `reset()`'s with value {}", register.name, &rv);
@@ -381,7 +381,7 @@ pub fn fields(
381381

382382
mod_items.extend(quote! {
383383
#[doc = #readerdoc]
384-
pub type #name_pc_r = crate::R<#fty, #name_pc_a>;
384+
pub type #name_pc_r = crate::FieldReader<#fty, #name_pc_a>;
385385
});
386386
} else {
387387
let has_reserved_variant = evs.values.len() != (1 << width);
@@ -463,7 +463,7 @@ pub fn fields(
463463

464464
mod_items.extend(quote! {
465465
#[doc = #readerdoc]
466-
pub type #name_pc_r = crate::R<#fty, #name_pc_a>;
466+
pub type #name_pc_r = crate::FieldReader<#fty, #name_pc_a>;
467467
impl #name_pc_r {
468468
#enum_items
469469
}
@@ -472,7 +472,7 @@ pub fn fields(
472472
} else {
473473
mod_items.extend(quote! {
474474
#[doc = #readerdoc]
475-
pub type #name_pc_r = crate::R<#fty, #fty>;
475+
pub type #name_pc_r = crate::FieldReader<#fty, #fty>;
476476
})
477477
}
478478
}

0 commit comments

Comments
 (0)