1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
//! Generic useful functions pub use sunrise_libutils::*; pub use crate::checks::*; use crate::error::KernelError; /// A trait for things that can be splitted in two parts pub trait Splittable where Self: Sized { /// Split the given object in two at a given offset. /// /// The left side is modified in place, and the new right side is returned. /// /// If offset >= self.length, the object is untouched, and the right-hand side is None. /// If offset == 0, the object is untouched, and the right-hand side is None. fn split_at(&mut self, offset: usize) -> Result<Option<Self>, KernelError>; /// Splits the given object in two at the given offset. /// /// The right side is modified in place, and the new left side is returned. /// /// Note that offset is still the distance from the **start**. /// /// If offset >= self.length, the object is untouched, and the right-hand side is None. /// If offset == 0, the object is untouched, and the right-hand side is None. fn right_split(&mut self, offset: usize) -> Result<Option<Self>, KernelError> { let right_opt = self.split_at(offset)?; match right_opt { None => Ok(None), // no split was done Some(mut other) => { // swap the left and the right parts ::core::mem::swap(self, &mut other); Ok(Some(other)) } } } }