pub trait IntoInner {
type InnerType;
// Required method
fn into_inner(self) -> Self::InnerType;
}Expand description
A trait for consuming a wrapper type and extracting its inner value.
The IntoInner trait is designed for types that act as wrappers around a single inner value.
It provides a method, IntoInner::into_inner(), which consumes the wrapper and returns the inner value.
This trait is particularly useful for tuple structs with a single field, where the wrapper is used to encapsulate or add functionality to the inner value.
§Associated Types
InnerType: The type of the inner value that will be extracted.
§Examples
§Manual Implementation
use into_inner::IntoInner; // import both the trait and the derive macro
struct MyWrapper(String);
impl IntoInner for MyWrapper {
type InnerType = String;
fn into_inner(self) -> Self::InnerType {
self.0
}
}
let wrapper = MyWrapper("Hello, world!".to_string());
let inner = wrapper.into_inner();
assert_eq!(inner, "Hello, world!");§Using the Derive Macro
The IntoInner trait can also be automatically implemented for tuple structs with a single field
using the #[derive(IntoInner)] macro:
use into_inner::IntoInner; // import both the trait and the derive macro
#[derive(IntoInner)]
struct MyWrapper(String);
let wrapper = MyWrapper("Hello, world!".to_string());
let inner = wrapper.into_inner();
assert_eq!(inner, "Hello, world!");Required Associated Types§
Required Methods§
Sourcefn into_inner(self) -> Self::InnerType
fn into_inner(self) -> Self::InnerType
Consumes the wrapper and returns the inner value.