Debian Codegraph @

src/ring_buffer/storage.rs

raw symbols SCIP
1 use craterust-analyzer cargo ringbuf 0.3.3 crate/::utilsrust-analyzer cargo ringbuf 0.3.3 utils/::ring_buffer_rangesrust-analyzer cargo ringbuf 0.3.3 utils/ring_buffer_ranges().;
2 #[cfg(feature = "alloc")]
3 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#;
4 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::UnsafeCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/UnsafeCell#, markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#, numrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/::NonZeroUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroUsize#, slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/};
5
6 /// Abstract container for the ring buffer.
7 ///
8 /// Container items must be stored as a contiguous array.
9 ///
10 /// # Safety
11 ///
12 /// *[`Self::len`]/[`Self::is_empty`] must always return the same value.*
13 ///
14 /// *Container must not cause data race on concurrent [`Self::as_mut_slice`]/[`Self::as_mut_ptr`] calls.*
15 pub unsafe trait Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T> {
16 /// Internal representation of the container.
17 ///
18 /// *Must not be aliased with its content.*
19 type Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#;
20
21 /// Transform container to internal representation.
22 fn into_internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#into_internal().(self) -> Self::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#;
23 /// Restore container from internal representation.
24 ///
25 /// # Safety
26 ///
27 /// `this` must be valid.
28 unsafe fn from_internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#from_internal().(this: Self::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> Self;
29
30 /// Return pointer to the beginning of the container items.
31 fn as_mut_ptrrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#as_mut_ptr().(this: &Self::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> *mut MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>;
32 /// Length of the container.
33 fn lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#len().(this: &Self::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> usize;
34 }
35
36 unsafe impl<'a, T> Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T> for &'a mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>] {
37 type Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`&'a mut [MaybeUninit<T>]`][`Container<T>`]Internal# = (*mut MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>, usize);
38
39 fn into_internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`&'a mut [MaybeUninit<T>]`][`Container<T>`]into_internal().(self) -> Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`&'a mut [MaybeUninit<T>]`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal# {
40 (self.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().(), self.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())
41 }
42 unsafe fn from_internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`&'a mut [MaybeUninit<T>]`][`Container<T>`]from_internal().(this: Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`&'a mut [MaybeUninit<T>]`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`&'a mut [MaybeUninit<T>]`][`Container<T>`] {
43 slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(this.0, this.1)
44 }
45
46 #[inline]
47 fn as_mut_ptrrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`&'a mut [MaybeUninit<T>]`][`Container<T>`]as_mut_ptr().(this: &Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`&'a mut [MaybeUninit<T>]`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> *mut MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T> {
48 this.0
49 }
50 #[inline]
51 fn lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`&'a mut [MaybeUninit<T>]`][`Container<T>`]len().(this: &Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`&'a mut [MaybeUninit<T>]`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> usize {
52 this.1
53 }
54 }
55
56 unsafe impl<T, const N: usize> Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T> for [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>; N] {
57 type Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`[MaybeUninit<T>; N]`][`Container<T>`]Internal# = UnsafeCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/UnsafeCell#<[MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>; N]>;
58
59 fn into_internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`[MaybeUninit<T>; N]`][`Container<T>`]into_internal().(self) -> Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`[MaybeUninit<T>; N]`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal# {
60 UnsafeCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/UnsafeCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`UnsafeCell<T>`]new().(self)
61 }
62 unsafe fn from_internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`[MaybeUninit<T>; N]`][`Container<T>`]from_internal().(this: Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`[MaybeUninit<T>; N]`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`[MaybeUninit<T>; N]`][`Container<T>`] {
63 this.into_innerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`UnsafeCell<T>`]into_inner().()
64 }
65
66 #[inline]
67 fn as_mut_ptrrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`[MaybeUninit<T>; N]`][`Container<T>`]as_mut_ptr().(this: &Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`[MaybeUninit<T>; N]`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> *mut MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T> {
68 this.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`UnsafeCell<T>`]get().() as *mut _
69 }
70 #[inline]
71 fn lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`[MaybeUninit<T>; N]`][`Container<T>`]len().(_: &Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`[MaybeUninit<T>; N]`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> usize {
72 N
73 }
74 }
75
76 #[cfg(feature = "alloc")]
77 unsafe impl<T> Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T> for Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>> {
78 type Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`]Internal# = Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`];
79
80 fn into_internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`]into_internal().(self) -> Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal# {
81 self
82 }
83 unsafe fn from_internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`]from_internal().(this: Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`] {
84 this
85 }
86
87 #[inline]
88 fn as_mut_ptrrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`]as_mut_ptr().(this: &Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> *mut MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T> {
89 this.as_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_ptr().() as *mut _
90 }
91 #[inline]
92 fn lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`]len().(this: &Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`Vec<MaybeUninit<T>>`][`Container<T>`]::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#) -> usize {
93 this.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()
94 }
95 }
96
97 /// Wrapper for container that provides multiple write access to it.
98 pub(craterust-analyzer cargo ringbuf 0.3.3 crate/) struct SharedStoragerust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> {
99 containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#container.: C::Internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#Internal#,
100 _prust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#_p.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<T>,
101 }
102
103 unsafe impl<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# for SharedStoragerust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#<T, C> where T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# {}
104
105 impl<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> SharedStoragerust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#<T, C> {
106 /// Create new storage.
107 ///
108 /// *Panics if container is empty.*
109 pub fn newrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]new().(container: C) -> Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`] {
110 let internal = container.into_internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#into_internal().();
111 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(C::lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#len().(&internal) >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). 0);
112 Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`] {
113 containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#container.: internal,
114 _prust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#_p.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#,
115 }
116 }
117
118 /// Get the length of the container.
119 #[inline]
120 pub fn lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]len().(&self) -> NonZeroUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroUsize# {
121 unsafe { NonZeroUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroUsize#::new_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[`NonZero<T>`]new_unchecked().(C::lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#len().(&self.containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#container.)) }
122 }
123
124 /// Returns a pair of slices between `head` and `tail` positions in the storage.
125 ///
126 /// For more information see [`ring_buffer_ranges`].
127 ///
128 /// # Safety
129 ///
130 /// There only single reference to any item allowed to exist at the time.
131 pub unsafe fn as_mut_slicesrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]as_mut_slices().(
132 &self,
133 head: usize,
134 tail: usize,
135 ) -> (&mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>], &mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>]) {
136 let ranges = ring_buffer_rangesrust-analyzer cargo ringbuf 0.3.3 utils/ring_buffer_ranges().(self.lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]len().(), head, tail);
137 let ptr = C::as_mut_ptrrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#as_mut_ptr().(&self.containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#container.);
138 (
139 slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(ptr.addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]add().(ranges.0.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start.), ranges.0.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator#len().()),
140 slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(ptr.addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]add().(ranges.1.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start.), ranges.1.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator#len().()),
141 )
142 }
143
144 /// Returns underlying container.
145 pub fn into_innerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]into_inner().(self) -> C {
146 unsafe { C::from_internalrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#from_internal().(self.containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#container.) }
147 }
148 }