#[cfg(all(doc, feature = "alloc"))]
use crate::{Box, Boxed};
use crate::{Mem, Storage};
#[doc = crate::_tags!(no mem)]
#[doc = crate::_doc_location!("sys/mem/alloc")]
pub type Bare = ();
#[doc = crate::_tags!(no mem)]
#[doc = crate::_doc_location!("sys/mem/alloc")]
pub struct BareBox<T>(pub T);
#[rustfmt::skip]
impl Storage for Bare {
type Stored<T> = BareBox<T>;
fn name() -> &'static str { "BareBox" }
}
#[rustfmt::skip]
impl<T> BareBox<T> {
#[must_use]
pub const fn new(t: T) -> Self { Self(t) }
#[must_use]
pub fn into_inner(self) -> T { self.0 }
#[must_use]
pub const fn as_ref(&self) -> &T { &self.0 }
#[must_use]
pub const fn as_mut(&mut self) -> &mut T { &mut self.0 }
#[must_use]
pub fn replace(&mut self, mut new: T) -> T {
Mem::swap(&mut self.0, &mut new); new
}
#[must_use]
pub fn is_default(&self) -> bool where T: Default + PartialEq {
self.0 == T::default()
}
}
#[rustfmt::skip]
impl<T: Copy> BareBox<T> {
#[must_use]
pub const fn into_inner_copy(self) -> T {
self.0
}
pub fn map<U: Copy>(self, f: fn(T) -> U) -> BareBox<U> {
BareBox(f(self.0))
}
}
impl<T: Copy> BareBox<Option<T>> {
#[rustfmt::skip]
pub const fn unwrap_copy_or(self, default: T) -> T {
match self.0 { Some(val) => val, None => default }
}
}
impl<T: Copy, E: Copy> BareBox<Result<T, E>> {
#[rustfmt::skip]
pub const fn unwrap_copy_or(self, default: T) -> T {
match self.0 { Ok(val) => val, Err(_) => default }
}
}
mod core_impls {
use crate::{BareBox, ConstInit};
use core::{cmp, convert, fmt, hash, ops};
impl<T> ops::Deref for BareBox<T> {
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
impl<T> ops::DerefMut for BareBox<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> convert::AsRef<T> for BareBox<T> {
fn as_ref(&self) -> &T {
&self.0
}
}
impl<T> convert::AsMut<T> for BareBox<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> From<T> for BareBox<T> {
fn from(t: T) -> Self {
BareBox(t)
}
}
impl<T: Clone> Clone for BareBox<T> {
fn clone(&self) -> Self {
BareBox(self.0.clone())
}
}
impl<T: Copy> Copy for BareBox<T> {}
impl<T: Default> Default for BareBox<T> {
fn default() -> Self {
BareBox(T::default())
}
}
impl<T: ConstInit> ConstInit for BareBox<T> {
const INIT: Self = BareBox(T::INIT);
}
impl<T: PartialEq> PartialEq for BareBox<T> {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
impl<T: Eq> Eq for BareBox<T> {}
impl<T: PartialOrd> PartialOrd for BareBox<T> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.0.partial_cmp(&other.0)
}
}
impl<T: Ord> Ord for BareBox<T> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.0.cmp(&other.0)
}
}
impl<T: fmt::Debug> fmt::Debug for BareBox<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl<T: fmt::Display> fmt::Display for BareBox<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl<T: fmt::Pointer> fmt::Pointer for BareBox<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Pointer::fmt(&self.0, f)
}
}
impl<T: hash::Hash> hash::Hash for BareBox<T> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl<T: hash::Hasher> hash::Hasher for BareBox<T> {
fn finish(&self) -> u64 {
self.0.finish()
}
fn write(&mut self, bytes: &[u8]) {
self.0.write(bytes);
}
}
impl<I: Iterator> Iterator for BareBox<I> {
type Item = I::Item;
fn next(&mut self) -> Option<I::Item> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
fn nth(&mut self, n: usize) -> Option<I::Item> {
self.0.nth(n)
}
fn last(self) -> Option<I::Item> {
self.0.last()
}
}
#[cfg(all(not(feature = "safe_mem"), feature = "unsafe_sync"))]
#[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_sync")))]
unsafe impl<T: Send> Send for BareBox<T> {}
#[cfg(all(not(feature = "safe_mem"), feature = "unsafe_sync"))]
#[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_sync")))]
unsafe impl<T: Sync> Sync for BareBox<T> {}
}