Debian Codegraph @

src/utils.rs

raw symbols SCIP
1 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{
2 memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::{selfrust-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#},
3 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#,
4 opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#,
5 };
6
7 /// Returns a pair of ranges between `head` and `tail` positions in a ring buffer with specific `capacity`.
8 ///
9 /// `head` and `tail` may be arbitrary large, but must satisfy the following condition: `0 <= (head - tail) % (2 * capacity) <= capacity`.
10 /// Actual positions are taken modulo `capacity`.
11 ///
12 /// The first range starts from `head`. If the first slice is empty then second slice is empty too.
13 pub fn ring_buffer_rangesrust-analyzer cargo ringbuf 0.3.3 utils/ring_buffer_ranges().(
14 capacity: NonZeroUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroUsize#,
15 head: usize,
16 tail: usize,
17 ) -> (Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>, Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>) {
18 let (head_quo, head_rem) = (head rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Div<NonZero<usize>>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Div<NonZero<usize>>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Div<NonZero<usize>>`]div().capacity, head rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Rem<NonZero<usize>>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Rem<NonZero<usize>>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Rem<NonZero<usize>>`]rem().capacity);
19 let (tail_quo, tail_rem) = (tail rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Div<NonZero<usize>>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Div<NonZero<usize>>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Div<NonZero<usize>>`]div().capacity, tail rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Rem<NonZero<usize>>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Rem<NonZero<usize>>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[usize][`Rem<NonZero<usize>>`]rem().capacity);
20
21 if (head_quo rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().tail_quo) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 {
22 (head_rem..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#tail_rem, 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#0)
23 } else {
24 (head_rem..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#capacity.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[`NonZero<T>`]get().(), 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#tail_rem)
25 }
26 }
27
28 // TODO: Remove on `maybe_uninit_uninit_array` stabilization.
29 pub fn uninit_arrayrust-analyzer cargo ringbuf 0.3.3 utils/uninit_array().<T, const N: usize>() -> [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>; N] {
30 unsafe { MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#::<[MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>; N]>::uninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]uninit().().assume_initrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]assume_init().() }
31 }
32
33 // TODO: Remove on `maybe_uninit_slice` stabilization.
34 pub unsafe fn slice_assume_init_refrust-analyzer cargo ringbuf 0.3.3 utils/slice_assume_init_ref().<T>(slice: &[MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>]) -> &[T] {
35 &*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().(slice as *const [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>] as *const [T])
36 }
37
38 // TODO: Remove on `maybe_uninit_slice` stabilization.
39 pub unsafe fn slice_assume_init_mutrust-analyzer cargo ringbuf 0.3.3 utils/slice_assume_init_mut().<T>(slice: &mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>]) -> &mut [T] {
40 &mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().(slice as *mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>] as *mut [T])
41 }
42
43 // TODO: Remove on `maybe_uninit_write_slice` stabilization.
44 pub fn write_slicerust-analyzer cargo ringbuf 0.3.3 utils/write_slice().<'a, T: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy#>(dst: &'a mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>], src: &[T]) -> &'a mut [T] {
45 let uninit_src: &[MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>] = unsafe { memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().(src) };
46 dst.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(uninit_src);
47 unsafe { slice_assume_init_mutrust-analyzer cargo ringbuf 0.3.3 utils/slice_assume_init_mut().(dst) }
48 }
49
50 pub unsafe fn write_uninit_slicerust-analyzer cargo ringbuf 0.3.3 utils/write_uninit_slice().<'a, T: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy#>(
51 dst: &'a mut [T],
52 src: &[MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>],
53 ) -> &'a mut [T] {
54 dst.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(slice_assume_init_refrust-analyzer cargo ringbuf 0.3.3 utils/slice_assume_init_ref().(src));
55 dst
56 }