wrapper/lib.rs
1//! A `Wrapper` is a value which took ownership of some of the value, such that the wrapped value
2//! can later be retrieved again. Conceptually this is identical to the regular `Into` trait, but
3//! that trait is hard to implement because of conflicts with the reflexive blanket implementation.
4//!
5//! ## Feature Flags
6//!
7//! By default, this crate only provides implementations for types in `core`. The `alloc` feature
8//! enables implementations for types in `alloc`, likewise `std` for `std`. Implementations for
9//! unstable types are enabled with the `unstable` feature.
10
11#![no_std]
12#![cfg_attr(feature = "unstable", feature(associated_type_bounds))]
13extern crate maybe_std as base;
14
15/// A type that wraps a value of type `Inner` that can be retrieved via `into_inner`.
16pub trait Wrapper<Inner> {
17 /// Retrieve ownership of the wrapped value.
18 fn into_inner(self) -> Inner;
19}
20
21// Internal macros for quickly defining implementations.
22
23macro_rules! implement_newtype_t {
24 ($t:ty) => {
25 impl<T> Wrapper<T> for $t {
26 fn into_inner(self) -> T {
27 self.0
28 }
29 }
30 }
31}
32
33macro_rules! implement_into_inner {
34 ($t:ty, $from:ty) => {
35 impl Wrapper<$from> for $t {
36 fn into_inner(self) -> $from {
37 <$t>::into_inner(self)
38 }
39 }
40 }
41}
42
43macro_rules! implement_into_inner_t {
44 ($t:ty) => {
45 impl<T> Wrapper<T> for $t {
46 fn into_inner(self) -> T {
47 <$t>::into_inner(self)
48 }
49 }
50 }
51}
52
53mod impls_core;
54pub use impls_core::*;
55
56#[cfg(any(feature = "alloc", feature = "std"))]
57mod impls_alloc;
58#[cfg(any(feature = "alloc", feature = "std"))]
59pub use impls_alloc::*;
60
61#[cfg(feature = "std")]
62mod impls_std;
63#[cfg(feature = "std")]
64pub use impls_std::*;