| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#, IoSliceMutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSliceMut#, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 2 | |
| 3 | pub(superrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/) struct RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer# { |
| 4 | storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[u8; Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::LEN]>, |
| 5 | // The start index of the non-empty section of the buffer. |
| 6 | startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start.: usize, |
| 7 | // The length of the non-empty section of the buffer. |
| 8 | lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len.: usize, |
| 9 | } |
| 10 | |
| 11 | impl RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer# { |
| 12 | /// The size of the internal storage of the ring buffer. |
| 13 | pub(superrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/) const LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN.: usize = 8 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().1024; |
| 14 | |
| 15 | /// Create a new, empty buffer. |
| 16 | pub(superrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/) fn newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]new().() -> Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer] { |
| 17 | Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer] { |
| 18 | storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().([0; Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN.]), |
| 19 | startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start.: 0, |
| 20 | lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len.: 0, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | pub(superrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/) fn is_fullrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]is_full().(&self) -> bool { |
| 25 | self.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len. 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().self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() |
| 26 | } |
| 27 | |
| 28 | pub(superrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/) fn insertrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]insert().<R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#>(&mut self, read: &mut R) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 29 | let inserted_len = if self.is_emptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]is_empty().() { |
| 30 | // Case 1.1. The buffer is empty, meaning that there are two unfilled slices in |
| 31 | // `storage`:`start..` and `..start`. |
| 32 | let (second_slice, first_slice) = self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage..split_at_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at_mut().(self.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start.); |
| 33 | read.read_vectoredrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_vectored().(&mut [first_slice, second_slice].maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]map().(IoSliceMutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSliceMut#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSliceMut<'a>`]new().))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 34 | } else { |
| 35 | let &mut Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer] { startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., .. } = self; |
| 36 | let end = start 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().len; |
| 37 | if end rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 38 | // Case 1.2. The buffer is not empty and the filled section wraps around `storage`. |
| 39 | // Meaning that there is only one unfilled slice in `storage`: `end..start`. |
| 40 | let end = end 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().self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 41 | read.readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read().(&mut self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().end..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 42 | } else { |
| 43 | // Case 1.3. The buffer is not empty and the filled section is a contiguous slice |
| 44 | // of `storage`. Meaning that there are two unfilled slices in `storage`: `..start` |
| 45 | // and `end..`. |
| 46 | let (mid, first_slice) = self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage..split_at_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at_mut().(end); |
| 47 | let second_slice = &mut mid[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#start]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().; |
| 48 | read.read_vectoredrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_vectored().(&mut [first_slice, second_slice].maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]map().(IoSliceMutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSliceMut#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSliceMut<'a>`]new().))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | self.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().inserted_len; |
| 53 | |
| 54 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(self.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start. <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN.); |
| 55 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(self.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len. <= Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN.); |
| 56 | |
| 57 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(inserted_len) |
| 58 | } |
| 59 | |
| 60 | pub(superrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/) fn is_emptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]is_empty().(&self) -> bool { |
| 61 | self.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len. 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 |
| 62 | } |
| 63 | |
| 64 | pub(superrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/) fn removerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]remove().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(&mut self, write: &mut W) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 65 | let removed_len = if self.is_fullrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]is_full().() { |
| 66 | // Case 2.1. The buffer is full, meaning that there are two filled slices in `storage`: |
| 67 | // `start..` and `..start`. |
| 68 | let (second_slice, first_slice) = self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage..split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at().(self.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start.); |
| 69 | write.write_vectoredrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_vectored().(&[first_slice, second_slice].maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]map().(IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`]new().))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 70 | } else { |
| 71 | let end = self.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start. 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().self.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len.; |
| 72 | if end rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 73 | // Case 2.2. The buffer is not full and the filled section wraps around `storage`. |
| 74 | // Meaning that there are two non-empty slices in `storage`: `start..` and `..end`. |
| 75 | let end = end 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().self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 76 | let first_slice = &self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().self.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 77 | let second_slice = &self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#end]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 78 | write.write_vectoredrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_vectored().(&[first_slice, second_slice].maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]map().(IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`]new().))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 79 | } else { |
| 80 | // Case 2.3. The buffer is not full and the filled section is a contiguous slice |
| 81 | // of `storage.` Meaning that there is only one filled slice in `storage`: |
| 82 | // `start..end`. |
| 83 | write.writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write().(&self.storagerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#storage.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().self.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | self.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().removed_len; |
| 88 | self.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`RemAssign<Self>`]rem_assign().%=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`RemAssign<Self>`]rem_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`RemAssign<Self>`]rem_assign().Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN.; |
| 89 | |
| 90 | self.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().removed_len; |
| 91 | |
| 92 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(self.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start. <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN.); |
| 93 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(self.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len. <= Selfrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN.); |
| 94 | |
| 95 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(removed_len) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #[cfg(test)] |
| 100 | mod testsrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/ { |
| 101 | use superrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/::RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#; |
| 102 | |
| 103 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 104 | fn empty_buffer_is_emptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/empty_buffer_is_empty().() { |
| 105 | let buf = RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]new().(); |
| 106 | |
| 107 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(buf.is_emptyrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]is_empty().()); |
| 108 | } |
| 109 | |
| 110 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 111 | fn full_buffer_is_fullrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/full_buffer_is_full().() { |
| 112 | let mut buf = RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]new().(); |
| 113 | |
| 114 | let inserted_len = buf.insertrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]insert().(&mut [0x45; RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN.].as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 115 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(inserted_len, RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN.); |
| 116 | |
| 117 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(buf.is_fullrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]is_full().()); |
| 118 | } |
| 119 | |
| 120 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 121 | fn buffer_is_fiforust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/buffer_is_fifo().() { |
| 122 | let mut buf = RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]new().(); |
| 123 | |
| 124 | let expected = (0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#u8::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]MAX.).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>(); |
| 125 | let inserted_len = buf.insertrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]insert().(&mut expected.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 126 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(inserted_len, expected.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 127 | |
| 128 | let mut found = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 129 | let removed_len = buf.removerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]remove().(&mut found).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 130 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(removed_len, expected.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 131 | |
| 132 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, found); |
| 133 | } |
| 134 | |
| 135 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 136 | fn insert_into_empty_buffer_with_offsetrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/insert_into_empty_buffer_with_offset().() { |
| 137 | const HALF_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/HALF_LEN.: usize = RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().2; |
| 138 | let mut buf = RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]new().(); |
| 139 | |
| 140 | // This should leave the buffer empty but with the start field pointing to the middle of |
| 141 | // the buffer. |
| 142 | // ┌───────────────────┐ |
| 143 | // │ │ |
| 144 | // └───────────────────┘ |
| 145 | // ▲ |
| 146 | // │ |
| 147 | // start |
| 148 | buf.insertrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]insert().(&mut [0u8; HALF_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/HALF_LEN.].as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 149 | buf.removerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]remove().(&mut vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 150 | |
| 151 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., HALF_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/HALF_LEN.); |
| 152 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., 0); |
| 153 | |
| 154 | // Then we fill the first half of the buffer with ones and the second one with twos in a |
| 155 | // single insertion. This tests case 1.1. |
| 156 | // ┌─────────┬─────────┐ |
| 157 | // │ 2 │ 1 │ |
| 158 | // └─────────┴─────────┘ |
| 159 | // ▲ |
| 160 | // │ |
| 161 | // start |
| 162 | let mut expected = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1; HALF_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/HALF_LEN.]; |
| 163 | expected.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&[2; HALF_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/HALF_LEN.]); |
| 164 | buf.insertrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]insert().(&mut expected.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 165 | |
| 166 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., HALF_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/HALF_LEN.); |
| 167 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN.); |
| 168 | |
| 169 | // When we remove all the elements of the buffer we should find them in the same order we |
| 170 | // inserted them. This tests case 2.1. |
| 171 | let mut found = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 172 | let removed_len = buf.removerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]remove().(&mut found).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 173 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(removed_len, expected.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 174 | |
| 175 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, found); |
| 176 | |
| 177 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., HALF_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/HALF_LEN.); |
| 178 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., 0); |
| 179 | } |
| 180 | |
| 181 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 182 | fn insert_into_non_empty_wrapping_bufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/insert_into_non_empty_wrapping_buffer().() { |
| 183 | const QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.: usize = RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().4; |
| 184 | let mut buf = RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]new().(); |
| 185 | |
| 186 | // This should leave the buffer empty but with the start field pointing to the middle of |
| 187 | // the buffer. |
| 188 | // ┌───────────────────────┐ |
| 189 | // │ │ |
| 190 | // └───────────────────────┘ |
| 191 | // ▲ |
| 192 | // │ |
| 193 | // start |
| 194 | buf.insertrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]insert().(&mut [0; 2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.].as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 195 | buf.removerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]remove().(&mut vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 196 | |
| 197 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., 2 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.); |
| 198 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., 0); |
| 199 | |
| 200 | // Then we fill one quarter of the buffer with ones. This gives us a non-empty buffer whose |
| 201 | // empty section is not contiguous. |
| 202 | // ┌───────────┬─────┬─────┐ |
| 203 | // │ │ 1 │ │ |
| 204 | // └───────────┴─────┴─────┘ |
| 205 | // ▲ |
| 206 | // │ |
| 207 | // start |
| 208 | let mut expected = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1; QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.]; |
| 209 | buf.insertrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]insert().(&mut expected.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 210 | |
| 211 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., 2 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.); |
| 212 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.); |
| 213 | |
| 214 | // Then we fill one quarter of the buffer with twos and another quarter of the buffer with |
| 215 | // threes in a single insertion. This tests case 1.2. |
| 216 | // ┌─────┬─────┬─────┬─────┐ |
| 217 | // │ 3 │ │ 1 │ 2 │ |
| 218 | // └─────┴─────┴─────┴─────┘ |
| 219 | // ▲ |
| 220 | // │ |
| 221 | // start |
| 222 | let mut second_half = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2; QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.]; |
| 223 | second_half.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&[3; QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.]); |
| 224 | buf.insertrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]insert().(&mut second_half.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 225 | |
| 226 | expected.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(second_half); |
| 227 | |
| 228 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., 2 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.); |
| 229 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., 3 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.); |
| 230 | |
| 231 | // When we remove all the elements of the buffer we should find them in the same order we |
| 232 | // inserted them. This tests case 2.2. |
| 233 | let mut found = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 234 | let removed_len = buf.removerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]remove().(&mut found).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 235 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(removed_len, expected.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 236 | |
| 237 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, found); |
| 238 | |
| 239 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.); |
| 240 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., 0); |
| 241 | } |
| 242 | |
| 243 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 244 | fn insert_into_non_empty_non_wrapping_bufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/insert_into_non_empty_non_wrapping_buffer().() { |
| 245 | const QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.: usize = RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]LEN. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().4; |
| 246 | let mut buf = RingBufferrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#::newrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]new().(); |
| 247 | |
| 248 | // We fill one quarter of the buffer with ones. This gives us a non-empty buffer whose |
| 249 | // empty section is contiguous. |
| 250 | // ┌─────┬────────────────┐ |
| 251 | // │ 1 │ │ |
| 252 | // └─────┴────────────────┘ |
| 253 | // ▲ |
| 254 | // │ |
| 255 | // └ start |
| 256 | let mut expected = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1; QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.]; |
| 257 | buf.insertrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]insert().(&mut expected.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 258 | |
| 259 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., 0); |
| 260 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.); |
| 261 | |
| 262 | // Then we fill one quarter of the buffer with twos. This tests case 1.3. |
| 263 | // ┌─────┬─────┬──────────┐ |
| 264 | // │ 1 │ 2 │ │ |
| 265 | // └─────┴─────┴──────────┘ |
| 266 | // ▲ |
| 267 | // │ |
| 268 | // └ start |
| 269 | let second_half = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2; QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.]; |
| 270 | buf.insertrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]insert().(&mut second_half.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 271 | |
| 272 | expected.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(second_half); |
| 273 | |
| 274 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., 0); |
| 275 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., 2 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.); |
| 276 | |
| 277 | // When we remove all the elements of the buffer we should find them in the same order we |
| 278 | // inserted them. This tests case 2.3. |
| 279 | let mut found = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 280 | let removed_len = buf.removerust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/impl#[RingBuffer]remove().(&mut found).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 281 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(removed_len, expected.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 282 | |
| 283 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, found); |
| 284 | |
| 285 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.startrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#start., 2 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). QUARTER_LENrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/tests/QUARTER_LEN.); |
| 286 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.lenrust-analyzer cargo sudo-rs 0.2.14 exec/use_pty/pipe/ring_buffer/RingBuffer#len., 0); |
| 287 | } |
| 288 | } |