scsys_traits/wrapper.rs
1/*
2 Appellation: wrapper <module>
3 Contrib: @FL03
4*/
5/// [IntoInner] is typically used for basic structures that wrap a single value.
6pub trait IntoInner {
7 type Inner;
8
9 fn into_inner(self) -> Self::Inner;
10}
11/// A [`RawWrapper`] is the base trait defining all so-called "wrapper" types.
12pub trait RawWrapper {
13 type Item;
14}
15/// The [`Wrapper`] trait is used to establish a common interface for all simplemented
16/// structures that "wrap" a single value. Essentially, any type capable of implementing
17/// `#[repr(transparent)]` can be considered a wrapper.
18pub trait Wrapper: RawWrapper {
19 /// returns a new instance of the wrapper initialized with the given value
20 fn new(value: Self::Item) -> Self;
21 /// consumes the wrapper to return the stored value
22 fn value(self) -> Self::Item;
23 /// returns an immutable reference to the stored value
24 fn get(&self) -> &Self::Item;
25 /// returns a mutable reference to the stored value
26 fn get_mut(&mut self) -> &mut Self::Item;
27 /// [`replace`](core::mem::replace) replaces the value inside the wrapper with a new one,
28 /// returning the old value.
29 fn replace(&mut self, value: Self::Item) -> Self::Item {
30 core::mem::replace(self.get_mut(), value)
31 }
32 /// set the value and return a mutable reference to the wrapper
33 fn set(&mut self, value: Self::Item) -> &mut Self {
34 *self.get_mut() = value;
35 self
36 }
37 /// [`swap`](core::mem::swap) swaps the inner values of two instances.
38 fn swap(&mut self, other: &mut Self) {
39 core::mem::swap(self.get_mut(), other.get_mut())
40 }
41 /// [`take`](core::mem::take) takes the value out of the wrapper, leaving it in the logical
42 /// default in its place
43 fn take(&mut self) -> Self::Item
44 where
45 Self::Item: Default,
46 {
47 core::mem::take(self.get_mut())
48 }
49}
50
51/*
52 ************* Implementations *************
53*/