shufl/
lib.rs

1#![no_std]
2#[cfg(feature = "alloc")]
3extern crate alloc;
4use core::mem::take;
5pub trait Shufl {
6    type Elem;
7    fn push(&mut self, e: Self::Elem) -> Result<(), impl Iterator<Item = Self::Elem>>;
8    fn drain(&mut self) -> impl Iterator<Item = Self::Elem>;
9}
10impl<T, const N: usize> Shufl for heapless::Vec<T, N> {
11    type Elem = T;
12
13    fn push(&mut self, e: Self::Elem) -> Result<(), impl Iterator<Item = Self::Elem>> {
14        match self.push(e) {
15            Ok(a) => Ok(a),
16            Err(val) => Err(take(self).into_iter().chain([val])),
17        }
18    }
19
20    fn drain(&mut self) -> impl Iterator<Item = Self::Elem> {
21        take(self).into_iter()
22    }
23}
24#[cfg(feature = "alloc")]
25const _: () = {
26    use core::iter::Empty;
27
28    use alloc::vec::Vec;
29
30    impl<T> Shufl for Vec<T> {
31        type Elem = T;
32
33        fn push(&mut self, e: Self::Elem) -> Result<(), impl Iterator<Item = Self::Elem>> {
34            self.push(e);
35            Ok::<_, Empty<T>>(())
36        }
37
38        fn drain(&mut self) -> impl Iterator<Item = Self::Elem> {
39            self.drain(..)
40        }
41    }
42};