module_lattice/
maybe_box.rs1use core::ops::{Deref, DerefMut};
2
3#[derive(Clone, Debug, PartialEq)]
6pub struct MaybeBox<T> {
7 #[cfg(not(feature = "alloc"))]
8 inner: T,
9 #[cfg(feature = "alloc")]
10 inner: alloc::boxed::Box<T>,
11}
12
13impl<T> MaybeBox<T> {
14 #[inline]
16 pub fn new(inner: T) -> Self {
17 #[cfg(not(feature = "alloc"))]
18 {
19 Self { inner }
20 }
21 #[cfg(feature = "alloc")]
22 Self {
23 inner: alloc::boxed::Box::new(inner),
24 }
25 }
26
27 #[inline]
31 #[must_use]
32 pub fn into_inner(self) -> T {
33 #[cfg(not(feature = "alloc"))]
34 {
35 self.inner
36 }
37 #[cfg(feature = "alloc")]
38 {
39 *self.inner
40 }
41 }
42}
43
44impl<T> Deref for MaybeBox<T> {
45 type Target = T;
46
47 fn deref(&self) -> &Self::Target {
48 &self.inner
49 }
50}
51
52impl<T> DerefMut for MaybeBox<T> {
53 fn deref_mut(&mut self) -> &mut Self::Target {
54 &mut self.inner
55 }
56}