Skip to content

Commit 81e5653

Browse files
committed
Move macro definition of "make_bitfield_serde" to struct_accessors.
Fixes <#124>.
1 parent 577f219 commit 81e5653

File tree

2 files changed

+90
-89
lines changed

2 files changed

+90
-89
lines changed

src/ondisk.rs

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::flash::Location;
44
use crate::struct_accessors::Getter;
55
use crate::struct_accessors::Setter;
66
use crate::struct_accessors::make_accessors;
7+
use crate::struct_accessors::make_bitfield_serde;
78
use crate::types::Error;
89
use crate::types::Result;
910
use core::convert::TryFrom;
@@ -751,95 +752,6 @@ impl ValueOrLocation {
751752
}
752753
}
753754

754-
// XXX: If I move this to struct_accessors, it doesn't work anymore.
755-
756-
/// A variant of the make_accessors macro for modular_bitfields.
757-
macro_rules! make_bitfield_serde {(
758-
$(#[$struct_meta:meta])*
759-
$struct_vis:vis
760-
struct $StructName:ident {
761-
$(
762-
$(#[$field_meta:meta])*
763-
$field_vis:vis
764-
$field_name:ident
765-
$(|| $(#[$serde_field_orig_meta:meta])* $serde_ty:ty : $field_orig_ty:ty)?
766-
$(: $field_ty:ty)?
767-
$(| $getter_vis:vis get $field_user_ty:ty $(: $setter_vis:vis set $field_setter_user_ty:ty)?)?
768-
),* $(,)?
769-
}
770-
) => {
771-
$(#[$struct_meta])*
772-
$struct_vis
773-
struct $StructName {
774-
$(
775-
$(#[$field_meta])*
776-
$field_vis
777-
$($field_name : $field_ty,)?
778-
$($field_name : $field_orig_ty,)?
779-
)*
780-
}
781-
782-
impl $StructName {
783-
pub fn builder() -> Self {
784-
Self::new() // NOT default
785-
}
786-
pub fn build(&self) -> Self {
787-
self.clone()
788-
}
789-
}
790-
791-
#[cfg(feature = "serde")]
792-
#[allow(dead_code)]
793-
impl $StructName {
794-
$(
795-
paste::paste!{
796-
$(
797-
#[allow(non_snake_case)]
798-
pub(crate) fn [<serde_ $field_name>] (self : &'_ Self)
799-
-> Result<$field_ty> {
800-
Ok(self.$field_name())
801-
}
802-
)?
803-
$(
804-
#[allow(non_snake_case)]
805-
pub(crate) fn [<serde_ $field_name>] (self : &'_ Self)
806-
-> Result<$serde_ty> {
807-
Ok(self.$field_name().into())
808-
}
809-
)?
810-
$(
811-
#[allow(non_snake_case)]
812-
pub(crate) fn [<serde_with_ $field_name>](self : &mut Self, value: $field_ty) -> &mut Self {
813-
self.[<set_ $field_name>](value.into());
814-
self
815-
}
816-
)?
817-
$(
818-
#[allow(non_snake_case)]
819-
pub(crate) fn [<serde_with_ $field_name>](self : &mut Self, value: $serde_ty) -> &mut Self {
820-
self.[<set_ $field_name>](value.into());
821-
self
822-
}
823-
)?
824-
}
825-
)*
826-
}
827-
828-
#[cfg(feature = "serde")]
829-
paste::paste! {
830-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
831-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
832-
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
833-
#[cfg_attr(feature = "serde", serde(rename = "" $StructName))]
834-
pub(crate) struct [<Serde $StructName>] {
835-
$(
836-
$(pub $field_name : <$field_ty as Specifier>::InOut,)?
837-
$($(#[$serde_field_orig_meta])* pub $field_name : $serde_ty,)?
838-
)*
839-
}
840-
}
841-
}}
842-
843755
#[derive(
844756
Debug, PartialEq, Eq, FromPrimitive, Clone, Copy, BitfieldSpecifier,
845757
)]

src/struct_accessors.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,92 @@ macro_rules! make_accessors {(
315315
)}
316316

317317
pub(crate) use make_accessors;
318+
319+
/// A variant of the make_accessors macro for modular_bitfields.
320+
macro_rules! make_bitfield_serde {(
321+
$(#[$struct_meta:meta])*
322+
$struct_vis:vis
323+
struct $StructName:ident {
324+
$(
325+
$(#[$field_meta:meta])*
326+
$field_vis:vis
327+
$field_name:ident
328+
$(|| $(#[$serde_field_orig_meta:meta])* $serde_ty:ty : $field_orig_ty:ty)?
329+
$(: $field_ty:ty)?
330+
$(| $getter_vis:vis get $field_user_ty:ty $(: $setter_vis:vis set $field_setter_user_ty:ty)?)?
331+
),* $(,)?
332+
}
333+
) => {
334+
$(#[$struct_meta])*
335+
$struct_vis
336+
struct $StructName {
337+
$(
338+
$(#[$field_meta])*
339+
$field_vis
340+
$($field_name : $field_ty,)?
341+
$($field_name : $field_orig_ty,)?
342+
)*
343+
}
344+
345+
impl $StructName {
346+
pub fn builder() -> Self {
347+
Self::new() // NOT default
348+
}
349+
pub fn build(&self) -> Self {
350+
self.clone()
351+
}
352+
}
353+
354+
#[cfg(feature = "serde")]
355+
#[allow(dead_code)]
356+
impl $StructName {
357+
$(
358+
paste::paste!{
359+
$(
360+
#[allow(non_snake_case)]
361+
pub(crate) fn [<serde_ $field_name>] (self : &'_ Self)
362+
-> Result<$field_ty> {
363+
Ok(self.$field_name())
364+
}
365+
)?
366+
$(
367+
#[allow(non_snake_case)]
368+
pub(crate) fn [<serde_ $field_name>] (self : &'_ Self)
369+
-> Result<$serde_ty> {
370+
Ok(self.$field_name().into())
371+
}
372+
)?
373+
$(
374+
#[allow(non_snake_case)]
375+
pub(crate) fn [<serde_with_ $field_name>](self : &mut Self, value: $field_ty) -> &mut Self {
376+
self.[<set_ $field_name>](value.into());
377+
self
378+
}
379+
)?
380+
$(
381+
#[allow(non_snake_case)]
382+
pub(crate) fn [<serde_with_ $field_name>](self : &mut Self, value: $serde_ty) -> &mut Self {
383+
self.[<set_ $field_name>](value.into());
384+
self
385+
}
386+
)?
387+
}
388+
)*
389+
}
390+
391+
#[cfg(feature = "serde")]
392+
paste::paste! {
393+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
394+
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
395+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
396+
#[cfg_attr(feature = "serde", serde(rename = "" $StructName))]
397+
pub(crate) struct [<Serde $StructName>] {
398+
$(
399+
$(pub $field_name : <$field_ty as Specifier>::InOut,)?
400+
$($(#[$serde_field_orig_meta])* pub $field_name : $serde_ty,)?
401+
)*
402+
}
403+
}
404+
}}
405+
406+
pub(crate) use make_bitfield_serde;

0 commit comments

Comments
 (0)