Skip to content

Commit e68e60a

Browse files
committed
refactor(macro): move docs into macro crate
Signed-off-by: Martin Kröning <[email protected]>
1 parent c64c9af commit e68e60a

File tree

3 files changed

+76
-70
lines changed

3 files changed

+76
-70
lines changed

src/lib.rs

-70
Original file line numberDiff line numberDiff line change
@@ -47,76 +47,6 @@
4747
#![doc(test(attr(allow(dead_code))))]
4848
#![doc(test(attr(allow(unused_variables))))]
4949

50-
/// A derive macro for method-based accesses to volatile structures.
51-
///
52-
/// This macro allows you to access the fields of a volatile structure via methods that enforce access limitations.
53-
/// It is also more easily chainable than [`map_field`].
54-
///
55-
/// <div class="warning">
56-
///
57-
/// This macro generates and implements a new `{T}VolatileFieldAccess` trait, that you have to import if used from other modules.
58-
/// Currently, the trait is only implemented for `VolatilePtr<'_, _, ReadWrite>`.
59-
///
60-
/// </div>
61-
///
62-
/// # Examples
63-
///
64-
/// ```
65-
/// use volatile::access::ReadOnly;
66-
/// use volatile::{VolatileFieldAccess, VolatileRef};
67-
///
68-
/// #[repr(C)]
69-
/// #[derive(VolatileFieldAccess, Default)]
70-
/// pub struct DeviceConfig {
71-
/// feature_select: u32,
72-
/// #[access(ReadOnly)]
73-
/// feature: u32,
74-
/// }
75-
///
76-
/// let mut device_config = DeviceConfig::default();
77-
/// let mut volatile_ref = VolatileRef::from_mut_ref(&mut device_config);
78-
/// let volatile_ptr = volatile_ref.as_mut_ptr();
79-
///
80-
/// volatile_ptr.feature_select().write(42);
81-
/// assert_eq!(volatile_ptr.feature_select().read(), 42);
82-
///
83-
/// // This does not compile, because we specified `#[access(ReadOnly)]` for this field.
84-
/// // volatile_ptr.feature().write(42);
85-
///
86-
/// // A real device might have changed the value, though.
87-
/// assert_eq!(volatile_ptr.feature().read(), 0);
88-
/// ```
89-
///
90-
/// # Details
91-
///
92-
/// This macro generates a new trait (`{T}VolatileFieldAccess`) and implements it for `VolatilePtr<'a, T, ReadWrite>`.
93-
/// The example above results in (roughly) the following code:
94-
///
95-
/// ```
96-
/// # #[repr(C)]
97-
/// # pub struct DeviceConfig {
98-
/// # feature_select: u32,
99-
/// # feature: u32,
100-
/// # }
101-
/// use volatile::access::{ReadOnly, ReadWrite};
102-
/// use volatile::{map_field, VolatilePtr};
103-
///
104-
/// pub trait DeviceConfigVolatileFieldAccess<'a> {
105-
/// fn feature_select(self) -> VolatilePtr<'a, u32, ReadWrite>;
106-
///
107-
/// fn feature(self) -> VolatilePtr<'a, u32, ReadOnly>;
108-
/// }
109-
///
110-
/// impl<'a> DeviceConfigVolatileFieldAccess<'a> for VolatilePtr<'a, DeviceConfig, ReadWrite> {
111-
/// fn feature_select(self) -> VolatilePtr<'a, u32, ReadWrite> {
112-
/// map_field!(self.feature_select).restrict()
113-
/// }
114-
///
115-
/// fn feature(self) -> VolatilePtr<'a, u32, ReadOnly> {
116-
/// map_field!(self.feature).restrict()
117-
/// }
118-
/// }
119-
/// ```
12050
#[cfg(feature = "derive")]
12151
pub use volatile_macro::VolatileFieldAccess;
12252

volatile-macro/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ proc-macro = true
1616
proc-macro2 = "1"
1717
quote = "1"
1818
syn = { version = "2", features = ["full"] }
19+
20+
[dev-dependencies]
21+
volatile = { version = "=0.5.3", path = "..", features = ["derive"] }

volatile-macro/src/lib.rs

+73
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![doc(test(attr(deny(warnings))))]
2+
#![doc(test(attr(allow(dead_code))))]
3+
14
use proc_macro::TokenStream;
25
use proc_macro2::TokenStream as TokenStream2;
36
use quote::ToTokens;
@@ -11,6 +14,76 @@ macro_rules! bail {
1114

1215
mod volatile;
1316

17+
/// A derive macro for method-based accesses to volatile structures.
18+
///
19+
/// This macro allows you to access the fields of a volatile structure via methods that enforce access limitations.
20+
/// It is also more easily chainable than `map_field`.
21+
///
22+
/// <div class="warning">
23+
///
24+
/// This macro generates and implements a new `{T}VolatileFieldAccess` trait, that you have to import if used from other modules.
25+
/// Currently, the trait is only implemented for `VolatilePtr<'_, _, ReadWrite>`.
26+
///
27+
/// </div>
28+
///
29+
/// # Examples
30+
///
31+
/// ```
32+
/// use volatile::access::ReadOnly;
33+
/// use volatile::{VolatileFieldAccess, VolatileRef};
34+
///
35+
/// #[repr(C)]
36+
/// #[derive(VolatileFieldAccess, Default)]
37+
/// pub struct DeviceConfig {
38+
/// feature_select: u32,
39+
/// #[access(ReadOnly)]
40+
/// feature: u32,
41+
/// }
42+
///
43+
/// let mut device_config = DeviceConfig::default();
44+
/// let mut volatile_ref = VolatileRef::from_mut_ref(&mut device_config);
45+
/// let volatile_ptr = volatile_ref.as_mut_ptr();
46+
///
47+
/// volatile_ptr.feature_select().write(42);
48+
/// assert_eq!(volatile_ptr.feature_select().read(), 42);
49+
///
50+
/// // This does not compile, because we specified `#[access(ReadOnly)]` for this field.
51+
/// // volatile_ptr.feature().write(42);
52+
///
53+
/// // A real device might have changed the value, though.
54+
/// assert_eq!(volatile_ptr.feature().read(), 0);
55+
/// ```
56+
///
57+
/// # Details
58+
///
59+
/// This macro generates a new trait (`{T}VolatileFieldAccess`) and implements it for `VolatilePtr<'a, T, ReadWrite>`.
60+
/// The example above results in (roughly) the following code:
61+
///
62+
/// ```
63+
/// # #[repr(C)]
64+
/// # pub struct DeviceConfig {
65+
/// # feature_select: u32,
66+
/// # feature: u32,
67+
/// # }
68+
/// use volatile::access::{ReadOnly, ReadWrite};
69+
/// use volatile::{map_field, VolatilePtr};
70+
///
71+
/// pub trait DeviceConfigVolatileFieldAccess<'a> {
72+
/// fn feature_select(self) -> VolatilePtr<'a, u32, ReadWrite>;
73+
///
74+
/// fn feature(self) -> VolatilePtr<'a, u32, ReadOnly>;
75+
/// }
76+
///
77+
/// impl<'a> DeviceConfigVolatileFieldAccess<'a> for VolatilePtr<'a, DeviceConfig, ReadWrite> {
78+
/// fn feature_select(self) -> VolatilePtr<'a, u32, ReadWrite> {
79+
/// map_field!(self.feature_select).restrict()
80+
/// }
81+
///
82+
/// fn feature(self) -> VolatilePtr<'a, u32, ReadOnly> {
83+
/// map_field!(self.feature).restrict()
84+
/// }
85+
/// }
86+
/// ```
1487
#[proc_macro_derive(VolatileFieldAccess, attributes(access))]
1588
pub fn derive_volatile(item: TokenStream) -> TokenStream {
1689
match volatile::derive_volatile(parse_macro_input!(item)) {

0 commit comments

Comments
 (0)