Skip to content

Commit e48b154

Browse files
committed
Update docs and tests
1 parent 479c74d commit e48b154

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

src/const_fn.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
1+
// Make functions `const` if the `const-fn` feature is active.
2+
// The meta attributes are in place to keep doc comments with the functions.
3+
// The function definition incl. annotations and doc comments must be enclodes
4+
// by the marco invocation.
25
macro_rules! const_fn {
36
($(#[$attr:meta])* pub const unsafe fn $($f:tt)*) => (
47

src/lib.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,6 @@
88
//! via their type parameter `N`. This means that you can instantiate a `heapless` data structure on
99
//! the stack, in a `static` variable, or even in the heap.
1010
//!
11-
//! There is a feature gate `const-fn` which enables the nightly `const_fn` feature.
12-
//! You can enable it in `Cargo.toml`.
13-
//!
14-
//! ```text
15-
//! # Cargo.toml
16-
//! ...
17-
//! [dependencies]
18-
//! heapless = { version = "0.4.0", features = ["const-fn"] }
19-
//! ...
20-
//!
21-
//! ```
22-
//! When enabled, you can use most `new` methods to initialize `static`
23-
//! variables at compile time.
24-
//!
25-
//!
2611
//! ```
2712
//! use heapless::Vec; // fixed capacity `std::Vec`
2813
//! use heapless::consts::U8; // type level integer used to specify capacity
@@ -38,9 +23,10 @@
3823
//! // work around
3924
//! static mut XS: Option<Vec<u8, U8>> = None;
4025
//! unsafe { XS = Some(Vec::new()) };
26+
//! let xs = unsafe { XS.as_mut().unwrap() };
4127
//!
42-
//! unsafe { XS.as_mut().unwrap().push(42) };
43-
//! unsafe { assert_eq!(XS.as_mut().unwrap().pop(), Some(42)) };
28+
//! xs.push(42);
29+
//! assert_eq!(xs.pop(), Some(42));
4430
//!
4531
//! // in the heap (though kind of pointless because no reallocation)
4632
//! let mut ys: Box<Vec<u8, U8>> = Box::new(Vec::new());
@@ -69,6 +55,29 @@
6955
//! queue
7056
//! - [`String`](struct.String.html)
7157
//! - [`Vec`](struct.Vec.html)
58+
//!
59+
//!
60+
//! In order to target the Rust stable toolchain, there are some feature gates.
61+
//! The features need to be enabled in `Cargo.toml` in order to use them.
62+
//! Once the underlaying features in Rust are stable,
63+
//! these feature gates might be activated by default.
64+
//!
65+
//! Example of `Cargo.toml`:
66+
//!
67+
//! ```text
68+
//! ...
69+
//! [dependencies]
70+
//! heapless = { version = "0.4.0", features = ["const-fn"] }
71+
//! ...
72+
//!
73+
//! ```
74+
//!
75+
//! Currently the following features are availbale and not active by default:
76+
//!
77+
//! - `"const-fn"` -- Enable the nightly `const_fn` feature and make most `new` methods `const`.
78+
//! This way they can be used to initialize static memory at compile time.
79+
//!
80+
7281

7382
#![allow(warnings)]
7483
#![deny(missing_docs)]

tests/tsan.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@ extern crate generic_array;
44
extern crate heapless;
55
extern crate scoped_threadpool;
66

7-
#[cfg(feature = "const-fn")]
87
use std::thread;
98

109
use generic_array::typenum::Unsigned;
1110
use heapless::consts::*;
1211
use heapless::RingBuffer;
1312
use scoped_threadpool::Pool;
1413

15-
#[cfg(feature = "const-fn")]
1614
#[test]
1715
fn once() {
18-
static mut RB: RingBuffer<i32, U4> = RingBuffer::new();
16+
static mut RB: Option<RingBuffer<i32, U4>> = None;
17+
unsafe{ RB = Some(RingBuffer::new()) };
1918

20-
let rb = unsafe { &mut RB };
19+
let rb = unsafe { RB.as_mut().unwrap() };
2120

2221
rb.enqueue(0).unwrap();
2322

@@ -34,12 +33,12 @@ fn once() {
3433
});
3534
}
3635

37-
#[cfg(feature = "const-fn")]
3836
#[test]
3937
fn twice() {
40-
static mut RB: RingBuffer<i32, U8> = RingBuffer::new();
38+
static mut RB: Option<RingBuffer<i32, U4>> = None;
39+
unsafe{ RB = Some(RingBuffer::new()) };
4140

42-
let rb = unsafe { &mut RB };
41+
let rb = unsafe { RB.as_mut().unwrap() };
4342

4443
rb.enqueue(0).unwrap();
4544
rb.enqueue(1).unwrap();

0 commit comments

Comments
 (0)