Skip to content

Commit a1ef64c

Browse files
committed
Add a const_fn feature to make new usable in statics
1 parent 767354c commit a1ef64c

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ documentation = "https://docs.rs/volatile"
99
repository = "https://github.com/phil-opp/volatile"
1010

1111
[dependencies]
12+
13+
[features]
14+
const_fn = []

src/lib.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(feature="const_fn", feature(const_fn))]
2+
13
//! Provides wrapper types `Volatile`, `ReadOnly`, `WriteOnly`, `ReadWrite`, which wrap any copy-able type and allows for
24
//! volatile memory access to wrapped value. Volatile memory accesses are never optimized away by
35
//! the compiler, and are useful in many low-level systems programming and concurrent contexts.
@@ -47,6 +49,23 @@ impl<T: Copy> Volatile<T> {
4749
/// # Panics
4850
///
4951
/// This method never panics.
52+
#[cfg(feature="const_fn")]
53+
pub const fn new(value: T) -> Volatile<T> {
54+
Volatile(value)
55+
}
56+
57+
/// Construct a new volatile instance wrapping the given value.
58+
///
59+
/// ```rust
60+
/// use volatile::Volatile;
61+
///
62+
/// let value = Volatile::new(0u32);
63+
/// ```
64+
///
65+
/// # Panics
66+
///
67+
/// This method never panics.
68+
#[cfg(not(feature="const_fn"))]
5069
pub fn new(value: T) -> Volatile<T> {
5170
Volatile(value)
5271
}
@@ -139,8 +158,25 @@ impl<T: Copy> ReadOnly<T> {
139158
/// # Panics
140159
///
141160
/// This function never panics.
161+
#[cfg(feature = "const_fn")]
162+
pub const fn new(value: T) -> ReadOnly<T> {
163+
ReadOnly(Volatile::new(value))
164+
}
165+
166+
/// Construct a new read-only volatile wrapper wrapping the given value.
167+
///
168+
/// ```rust
169+
/// use volatile::ReadOnly;
170+
///
171+
/// let value = ReadOnly::new(42u32);
172+
/// ```
173+
///
174+
/// # Panics
175+
///
176+
/// This function never panics.
177+
#[cfg(not(feature = "const_fn"))]
142178
pub fn new(value: T) -> ReadOnly<T> {
143-
ReadOnly(Volatile(value))
179+
ReadOnly(Volatile::new(value))
144180
}
145181

146182
/// Perform a volatile read of the contained value, returning a copy of the read value.
@@ -179,8 +215,25 @@ impl<T: Copy> WriteOnly<T> {
179215
/// # Panics
180216
///
181217
/// This function never panics.
218+
#[cfg(feature = "const_fn")]
219+
pub const fn new(value: T) -> WriteOnly<T> {
220+
WriteOnly(Volatile::new(value))
221+
}
222+
223+
/// Constructs a new write only volatile wrapper around the given value.
224+
///
225+
/// ```rust
226+
/// use volatile::WriteOnly;
227+
///
228+
/// let value = WriteOnly::new(0u32);
229+
/// ```
230+
///
231+
/// # Panics
232+
///
233+
/// This function never panics.
234+
#[cfg(not(feature = "const_fn"))]
182235
pub fn new(value: T) -> WriteOnly<T> {
183-
WriteOnly(Volatile(value))
236+
WriteOnly(Volatile::new(value))
184237
}
185238

186239
/// Performs a volatile write of value `value` into the contained value. Functionally identical

0 commit comments

Comments
 (0)