Skip to content

Commit db731c3

Browse files
authored
Merge pull request #631 from zeenix/nightly-fmt
Enable a few rustfmt options:
2 parents dfe1441 + 947687b commit db731c3

File tree

18 files changed

+229
-153
lines changed

18 files changed

+229
-153
lines changed

.rustfmt.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
imports_granularity = "Crate"
2+
comment_width = 100
3+
wrap_comments = true

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ $ # run only for example histbuf tests
2121
$ cargo test histbuf --features serde
2222
```
2323

24+
# Formatting
25+
26+
Like most Rust projects, we use `rustfmt` to keep the formatting of code consistent. However, we
27+
make use of cecertain options that are currently only available in the nightly version:
28+
29+
```console
30+
$ cargo +nightly fmt --all
31+
```
32+
2433
## License
2534

2635
Licensed under either of

src/c_string.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ impl<const N: usize, LenT: LenType> CString<N, LenT> {
106106
///
107107
/// # Safety
108108
///
109-
/// - The memory pointed to by `ptr` must contain a valid nul terminator at the
110-
/// end of the string.
111-
/// - `ptr` must be valid for reads of bytes up to and including the nul terminator.
112-
/// This means in particular:
113-
/// - The entire memory range of this `CStr` must be contained within a single allocated object!
109+
/// - The memory pointed to by `ptr` must contain a valid nul terminator at the end of the
110+
/// string.
111+
/// - `ptr` must be valid for reads of bytes up to and including the nul terminator. This means
112+
/// in particular:
113+
/// - The entire memory range of this `CStr` must be contained within a single allocated
114+
/// object!
114115
/// - `ptr` must be non-nul even for a zero-length `CStr`.
115116
///
116117
/// # Example
@@ -190,7 +191,8 @@ impl<const N: usize, LenT: LenType> CString<N, LenT> {
190191

191192
match CStr::from_bytes_with_nul(bytes) {
192193
Ok(_) => {
193-
// SAFETY: A string is left in a valid state because appended bytes are nul-terminated.
194+
// SAFETY: A string is left in a valid state because appended bytes are
195+
// nul-terminated.
194196
unsafe { self.extend_from_bytes_unchecked(bytes) }?;
195197

196198
Ok(())

src/deque.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@
3333
//! }
3434
//! ```
3535
36-
use crate::vec::{OwnedVecStorage, VecStorage, VecStorageInner, ViewVecStorage};
37-
use crate::CapacityError;
38-
use core::cmp::Ordering;
39-
use core::fmt;
40-
use core::iter::FusedIterator;
41-
use core::marker::PhantomData;
42-
use core::mem::{ManuallyDrop, MaybeUninit};
43-
use core::{ptr, slice};
36+
use crate::{
37+
vec::{OwnedVecStorage, VecStorage, VecStorageInner, ViewVecStorage},
38+
CapacityError,
39+
};
40+
use core::{
41+
cmp::Ordering,
42+
fmt,
43+
iter::FusedIterator,
44+
marker::PhantomData,
45+
mem::{ManuallyDrop, MaybeUninit},
46+
ptr, slice,
47+
};
4448

4549
#[cfg(feature = "zeroize")]
4650
use zeroize::Zeroize;
@@ -173,14 +177,16 @@ impl<T, const N: usize> Deque<T, N> {
173177

174178
/// Returns the maximum number of elements the deque can hold.
175179
///
176-
/// This method is not available on a `DequeView`, use [`storage_capacity`](DequeInner::storage_capacity) instead.
180+
/// This method is not available on a `DequeView`, use
181+
/// [`storage_capacity`](DequeInner::storage_capacity) instead.
177182
pub const fn capacity(&self) -> usize {
178183
N
179184
}
180185

181186
/// Returns the number of elements currently in the deque.
182187
///
183-
/// This method is not available on a `DequeView`, use [`storage_len`](DequeInner::storage_len) instead.
188+
/// This method is not available on a `DequeView`, use [`storage_len`](DequeInner::storage_len)
189+
/// instead.
184190
pub const fn len(&self) -> usize {
185191
if self.full {
186192
N
@@ -452,8 +458,8 @@ impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
452458
self.storage_capacity() - free,
453459
);
454460

455-
// because the deque wasn't contiguous, we know that `tail_len < self.len == slice.len()`,
456-
// so this will never panic.
461+
// because the deque wasn't contiguous, we know that `tail_len < self.len ==
462+
// slice.len()`, so this will never panic.
457463
slice.rotate_left(back_len);
458464

459465
// the used part of the buffer now is `free..self.capacity()`, so set
@@ -465,7 +471,8 @@ impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
465471
// head is shorter so:
466472
// 1. copy head backwards
467473
// 2. rotate used part of the buffer
468-
// 3. update head to point to the new beginning (which is the beginning of the buffer)
474+
// 3. update head to point to the new beginning (which is the beginning of the
475+
// buffer)
469476

470477
unsafe {
471478
// if there is no free space in the buffer, then the slices are already
@@ -483,8 +490,8 @@ impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
483490
// next to each other, all the elements in the range are initialized.
484491
let slice: &mut [T] = slice::from_raw_parts_mut(buffer_ptr, len);
485492

486-
// because the deque wasn't contiguous, we know that `head_len < self.len == slice.len()`
487-
// so this will never panic.
493+
// because the deque wasn't contiguous, we know that `head_len < self.len ==
494+
// slice.len()` so this will never panic.
488495
slice.rotate_right(front_len);
489496

490497
// the used part of the buffer now is `0..self.len`, so set
@@ -844,11 +851,11 @@ impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
844851
}
845852

846853
// Safety:
847-
// * Any slice passed to `drop_in_place` is valid; the second case has
848-
// `len <= front.len()` and returning on `len > self.storage_len()` ensures
849-
// `begin <= back.len()` in the first case
850-
// * Deque front/back cursors are moved before calling `drop_in_place`,
851-
// so no value is dropped twice if `drop_in_place` panics
854+
// * Any slice passed to `drop_in_place` is valid; the second case has `len <= front.len()`
855+
// and returning on `len > self.storage_len()` ensures `begin <= back.len()` in the first
856+
// case
857+
// * Deque front/back cursors are moved before calling `drop_in_place`, so no value is
858+
// dropped twice if `drop_in_place` panics
852859
unsafe {
853860
// If new desired length is greater or equal, we don't need to act.
854861
if len >= self.storage_len() {
@@ -865,8 +872,9 @@ impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
865872
let drop_back = back.get_unchecked_mut(begin..) as *mut _;
866873

867874
// Self::to_physical_index returns the index `len` units _after_ the front cursor,
868-
// meaning we can use it to find the decremented index for `back` for non-contiguous deques,
869-
// as well as determine where the new "cap" for front needs to be placed for contiguous deques.
875+
// meaning we can use it to find the decremented index for `back` for non-contiguous
876+
// deques, as well as determine where the new "cap" for front needs
877+
// to be placed for contiguous deques.
870878
self.back = self.to_physical_index(len);
871879
self.full = false;
872880

@@ -880,8 +888,9 @@ impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
880888
self.back = self.to_physical_index(len);
881889
self.full = false;
882890

883-
// If `drop_front` causes a panic, the Dropper will still be called to drop it's slice during unwinding.
884-
// In either case, front will always be dropped before back.
891+
// If `drop_front` causes a panic, the Dropper will still be called to drop it's
892+
// slice during unwinding. In either case, front will always be
893+
// dropped before back.
885894
let _back_dropper = Dropper(&mut *drop_back);
886895
ptr::drop_in_place(drop_front);
887896
}
@@ -970,7 +979,8 @@ impl<T, S: VecStorage<T> + ?Sized> DequeInner<T, S> {
970979
cur += 1;
971980
idx += 1;
972981
}
973-
// Stage 2: Swap retained values into current idx, building a contiguous chunk from 0 to idx.
982+
// Stage 2: Swap retained values into current idx, building a contiguous chunk from 0 to
983+
// idx.
974984
while cur < len {
975985
let val = self
976986
.get_mut(cur)

src/history_buf.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@
3131
//! assert_eq!(avg, 4);
3232
//! ```
3333
34-
use core::fmt;
35-
use core::marker::PhantomData;
36-
use core::mem::MaybeUninit;
37-
use core::ops::Deref;
38-
use core::ptr;
39-
use core::slice;
34+
use core::{fmt, marker::PhantomData, mem::MaybeUninit, ops::Deref, ptr, slice};
4035

4136
#[cfg(feature = "zeroize")]
4237
use zeroize::Zeroize;
@@ -51,18 +46,23 @@ mod storage {
5146
///
5247
/// There's two implementations available:
5348
///
54-
/// - [`OwnedHistoryBufStorage`]: stores the data in an array `[T; N]` whose size is known at compile time.
49+
/// - [`OwnedHistoryBufStorage`]: stores the data in an array `[T; N]` whose size is known at
50+
/// compile time.
5551
/// - [`ViewHistoryBufStorage`]: stores the data in an unsized `[T]`.
5652
///
57-
/// This allows [`HistoryBuf`] to be generic over either sized or unsized storage. The [`histbuf`]
58-
/// module contains a [`HistoryBufInner`] struct that's generic on [`HistoryBufStorage`],
59-
/// and two type aliases for convenience:
53+
/// This allows [`HistoryBuf`] to be generic over either sized or unsized storage. The
54+
/// [`histbuf`] module contains a [`HistoryBufInner`] struct that's generic on
55+
/// [`HistoryBufStorage`], and two type aliases for convenience:
6056
///
61-
/// - [`HistoryBuf<T, N>`](super::HistoryBuf) = `HistoryBufInner<T, OwnedHistoryBufStorage<T, N>>`
62-
/// - [`HistoryBufView<T>`](super::HistoryBufView) = `HistoryBufInner<T, ViewHistoryBufStorage<T>>`
57+
/// - [`HistoryBuf<T, N>`](super::HistoryBuf) = `HistoryBufInner<T, OwnedHistoryBufStorage<T,
58+
/// N>>`
59+
/// - [`HistoryBufView<T>`](super::HistoryBufView) = `HistoryBufInner<T,
60+
/// ViewHistoryBufStorage<T>>`
6361
///
64-
/// `HistoryBuf` can be unsized into `HistoryBufView`, either by unsizing coercions such as `&mut HistoryBuf -> &mut HistoryBufView` or
65-
/// `Box<HistoryBuf> -> Box<HistoryBufView>`, or explicitly with [`.as_view()`](super::HistoryBuf::as_view) or [`.as_mut_view()`](super::HistoryBuf::as_mut_view).
62+
/// `HistoryBuf` can be unsized into `HistoryBufView`, either by unsizing coercions such as
63+
/// `&mut HistoryBuf -> &mut HistoryBufView` or `Box<HistoryBuf> -> Box<HistoryBufView>`, or
64+
/// explicitly with [`.as_view()`](super::HistoryBuf::as_view) or
65+
/// [`.as_mut_view()`](super::HistoryBuf::as_mut_view).
6666
///
6767
/// This trait is sealed, so you cannot implement it for your own types. You can only use
6868
/// the implementations provided by this crate.
@@ -75,7 +75,8 @@ mod storage {
7575
pub trait HistoryBufStorage<T>: HistoryBufSealedStorage<T> {}
7676

7777
pub trait HistoryBufSealedStorage<T> {
78-
// part of the sealed trait so that no trait is publicly implemented by `OwnedHistoryBufStorage` besides `Storage`
78+
// part of the sealed trait so that no trait is publicly implemented by
79+
// `OwnedHistoryBufStorage` besides `Storage`
7980
fn borrow(&self) -> &[MaybeUninit<T>];
8081
fn borrow_mut(&mut self) -> &mut [MaybeUninit<T>];
8182
fn as_hist_buf_view(this: &HistoryBufInner<T, Self>) -> &HistoryBufView<T>
@@ -92,7 +93,8 @@ mod storage {
9293
pub(crate) buffer: T,
9394
}
9495

95-
/// Implementation of [`HistoryBufStorage`] that stores the data in an array `[T; N]` whose size is known at compile time.
96+
/// Implementation of [`HistoryBufStorage`] that stores the data in an array `[T; N]` whose size
97+
/// is known at compile time.
9698
pub type OwnedHistoryBufStorage<T, const N: usize> =
9799
HistoryBufStorageInner<[MaybeUninit<T>; N]>;
98100
/// Implementation of [`HistoryBufStorage`] that stores the data in an unsized `[T]`.
@@ -651,8 +653,10 @@ impl<T> DoubleEndedIterator for OldestOrdered<'_, T> {
651653

652654
#[cfg(test)]
653655
mod tests {
654-
use core::fmt::Debug;
655-
use core::sync::atomic::{AtomicUsize, Ordering};
656+
use core::{
657+
fmt::Debug,
658+
sync::atomic::{AtomicUsize, Ordering},
659+
};
656660

657661
use static_assertions::assert_not_impl_any;
658662

src/index_map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,8 @@ where
665665
match self.core.insert(self.hash_val, self.key, value) {
666666
Insert::Success(inserted) => {
667667
unsafe {
668-
// SAFETY: Already checked existence at instantiation and the only mutable reference
669-
// to the map is internally held.
668+
// SAFETY: Already checked existence at instantiation and the only mutable
669+
// reference to the map is internally held.
670670
Ok(&mut (*self.core.entries.as_mut_ptr().add(inserted.index)).value)
671671
}
672672
}
@@ -1592,8 +1592,8 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
15921592

15931593
/// A mutable iterator over the values of a [`IndexMap`].
15941594
///
1595-
/// This `struct` is created by the [`values_mut`](IndexMap::values_mut) method on [`IndexMap`]. See its
1596-
/// documentation for more.
1595+
/// This `struct` is created by the [`values_mut`](IndexMap::values_mut) method on [`IndexMap`]. See
1596+
/// its documentation for more.
15971597
pub struct ValuesMut<'a, K, V> {
15981598
iter: slice::IterMut<'a, Bucket<K, V>>,
15991599
}

src/linear_map.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ mod storage {
2121
/// - [`OwnedStorage`]: stores the data in an array whose size is known at compile time.
2222
/// - [`ViewStorage`]: stores the data in an unsized slice
2323
///
24-
/// This allows [`LinearMap`] to be generic over either sized or unsized storage. The [`linear_map`](super)
25-
/// module contains a [`LinearMapInner`] struct that's generic on [`LinearMapStorage`],
26-
/// and two type aliases for convenience:
24+
/// This allows [`LinearMap`] to be generic over either sized or unsized storage. The
25+
/// [`linear_map`](super) module contains a [`LinearMapInner`] struct that's generic on
26+
/// [`LinearMapStorage`], and two type aliases for convenience:
2727
///
2828
/// - [`LinearMap<N>`](crate::linear_map::LinearMap) = `LinearMapInner<OwnedStorage<u8, N>>`
2929
/// - [`LinearMapView<T>`](crate::linear_map::LinearMapView) = `LinearMapInner<ViewStorage<u8>>`
3030
///
31-
/// `LinearMap` can be unsized into `StrinsgView`, either by unsizing coercions such as `&mut LinearMap -> &mut LinearMapView` or
32-
/// `Box<LinearMap> -> Box<LinearMapView>`, or explicitly with [`.as_view()`](crate::linear_map::LinearMap::as_view) or [`.as_mut_view()`](crate::linear_map::LinearMap::as_mut_view).
31+
/// `LinearMap` can be unsized into `StrinsgView`, either by unsizing coercions such as `&mut
32+
/// LinearMap -> &mut LinearMapView` or `Box<LinearMap> -> Box<LinearMapView>`, or
33+
/// explicitly with [`.as_view()`](crate::linear_map::LinearMap::as_view) or
34+
/// [`.as_mut_view()`](crate::linear_map::LinearMap::as_mut_view).
3335
///
3436
/// This trait is sealed, so you cannot implement it for your own types. You can only use
3537
/// the implementations provided by this crate.
@@ -85,7 +87,8 @@ mod storage {
8587
}
8688

8789
pub use storage::LinearMapStorage;
88-
/// Implementation of [`LinearMapStorage`] that stores the data in an array whose size is known at compile time.
90+
/// Implementation of [`LinearMapStorage`] that stores the data in an array whose size is known at
91+
/// compile time.
8992
pub type OwnedStorage<K, V, const N: usize> = OwnedVecStorage<(K, V), N>;
9093
/// Implementation of [`LinearMapStorage`] that stores the data in an unsized slice.
9194
pub type ViewStorage<K, V> = ViewVecStorage<(K, V)>;
@@ -590,7 +593,8 @@ where
590593

591594
/// An iterator that moves out of a [`LinearMap`].
592595
///
593-
/// This struct is created by calling the [`into_iter`](LinearMap::into_iter) method on [`LinearMap`].
596+
/// This struct is created by calling the [`into_iter`](LinearMap::into_iter) method on
597+
/// [`LinearMap`].
594598
pub struct IntoIter<K, V, const N: usize>
595599
where
596600
K: Eq,

src/mpmc.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
//!
7070
//! # Benchmark
7171
//!
72-
//! Measured on an ARM Cortex-M3 core running at 8 MHz and with zero flash wait cycles, compiled with `-C opt-level=z`:
72+
//! Measured on an ARM Cortex-M3 core running at 8 MHz and with zero flash wait cycles, compiled
73+
//! with `-C opt-level=z`:
7374
//!
7475
//! | Method | Time | N |
7576
//! |:----------------------------|-----:|---:|
@@ -80,15 +81,14 @@
8081
//! | `Queue::<u8, 8>::dequeue()` | 53 | 1 |
8182
//! | `Queue::<u8, 8>::dequeue()` | 71 | 2 |
8283
//!
83-
//! - N denotes the number of interruptions. On Cortex-M, an interruption consists of an
84-
//! interrupt handler preempting the would-be atomic section of the `enqueue`/`dequeue`
85-
//! operation. Note that it does *not* matter if the higher priority handler uses the queue or
86-
//! not.
84+
//! - N denotes the number of interruptions. On Cortex-M, an interruption consists of an interrupt
85+
//! handler preempting the would-be atomic section of the `enqueue`/`dequeue` operation. Note that
86+
//! it does *not* matter if the higher priority handler uses the queue or not.
8787
//! - All execution times are in clock cycles (1 clock cycle = 125 ns).
8888
//! - Execution time is *dependent* on `mem::size_of::<T>()`, as both operations include
8989
//! `ptr::read::<T>()` or `ptr::write::<T>()` in their successful path.
90-
//! - The numbers reported correspond to the successful path, i.e. `dequeue` returning `Some`
91-
//! and `enqueue` returning `Ok`.
90+
//! - The numbers reported correspond to the successful path, i.e. `dequeue` returning `Some` and
91+
//! `enqueue` returning `Ok`.
9292
//!
9393
//! # References
9494
//!
@@ -146,7 +146,8 @@ pub type Queue<T, const N: usize> = QueueInner<T, OwnedStorage<N>>;
146146

147147
/// A [`Queue`] with dynamic capacity.
148148
///
149-
/// [`Queue`] coerces to `QueueView`. `QueueView` is `!Sized`, meaning it can only ever be used by reference.
149+
/// [`Queue`] coerces to `QueueView`. `QueueView` is `!Sized`, meaning it can only ever be used by
150+
/// reference.
150151
pub type QueueView<T> = QueueInner<T, ViewStorage>;
151152

152153
impl<T, const N: usize> Queue<T, N> {
@@ -158,8 +159,8 @@ impl<T, const N: usize> Queue<T, N> {
158159
/// # Deprecation
159160
///
160161
/// <div class="warning">
161-
/// The current implementation of `mpmc` is marked as deprecated due to not being truly lock-free
162-
/// </div>
162+
/// The current implementation of `mpmc` is marked as deprecated due to not being truly
163+
/// lock-free </div>
163164
///
164165
/// If a thread is parked, or pre-empted for a long time by an higher-priority task
165166
/// during an `enqueue` or `dequeue` operation, it is possible that the queue ends-up
@@ -230,7 +231,8 @@ impl<T, S: Storage> QueueInner<T, S> {
230231
/// let view: &QueueView<u8> = queue.as_view();
231232
/// ```
232233
///
233-
/// It is often preferable to do the same through type coerction, since `Queue<T, N>` implements `Unsize<QueueView<T>>`:
234+
/// It is often preferable to do the same through type coerction, since `Queue<T, N>` implements
235+
/// `Unsize<QueueView<T>>`:
234236
///
235237
/// ```rust
236238
/// # use heapless::mpmc::{Queue, QueueView};
@@ -251,7 +253,8 @@ impl<T, S: Storage> QueueInner<T, S> {
251253
/// let view: &mut QueueView<u8> = queue.as_mut_view();
252254
/// ```
253255
///
254-
/// It is often preferable to do the same through type coerction, since `Queue<T, N>` implements `Unsize<QueueView<T>>`:
256+
/// It is often preferable to do the same through type coerction, since `Queue<T, N>` implements
257+
/// `Unsize<QueueView<T>>`:
255258
///
256259
/// ```rust
257260
/// # use heapless::mpmc::{Queue, QueueView};

0 commit comments

Comments
 (0)