Expand description
§IntoInner Crate
The into_inner
crate provides a trait and a procedural macro (both named IntoInner
) for simplifying the process of extracting
the inner value from wrapper types, such as tuple structs with a single field. This is particularly useful
when working with types that encapsulate a single value but require an easy way to consume the wrapper
and access the inner value.
§Features
IntoInner
Trait: A trait for consuming a wrapper type and extracting its inner value.#[derive(IntoInner)]
Macro: A procedural macro for automatically implementing theIntoInner
trait for tuple structs with a single field.
§Usage
The crate is designed to work seamlessly with both manual implementations of the IntoInner
trait and
the #[derive(IntoInner)]
macro for automatic implementations.
§Example: Manual Implementation of the IntoInner
Trait
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!");
§Example: 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!");
§Procedural Macro: #[derive(IntoInner)]
The #[derive(IntoInner)]
macro generates an implementation of the IntoInner
trait for tuple structs
with a single field. This allows you to avoid manually implementing the trait for simple wrapper types.
§Requirements
- The macro can only be applied to tuple structs with exactly one field.
- Applying the macro to a struct with named fields or multiple fields will result in a compile-time error.
§Generated Code
For a tuple struct like:
struct MyWrapper(String);
The macro generates the following implementation:
impl IntoInner for MyWrapper {
type InnerType = String;
fn into_inner(self) -> Self::InnerType {
self.0
}
}
§Compile-Time Errors
The macro will generate a compile-time error if applied to unsupported struct types:
use into_inner::IntoInner; // import both the trait and the derive macro
#[derive(IntoInner)]
struct InvalidWrapper(String, i32); // Error: `#[derive(IntoInner)]` supports only tuple structs with one field
use into_inner::IntoInner; // import both the trait and the derive macro
#[derive(IntoInner)]
struct NamedFieldsWrapper {
field: String,
} // Error: `#[derive(IntoInner)]` can only be used on tuple structs
§Generic Tuple Structs
The macro also works with generic tuple structs:
use into_inner::IntoInner; // import both the trait and the derive macro
#[derive(IntoInner)]
struct GenericWrapper<T>(T);
let wrapper = GenericWrapper(42);
let inner = wrapper.into_inner();
assert_eq!(inner, 42);
§Testing
The crate includes tests to verify the functionality of both the IntoInner
trait and the #[derive(IntoInner)]
macro.
§Example Test
use into_inner::IntoInner; // import both the trait and the derive macro
#[derive(IntoInner)]
struct MyWrapper(String);
fn print_inner<T>(val: T) -> T::InnerType
where
T: IntoInner,
T::InnerType: std::fmt::Debug,
{
let v = val.into_inner();
println!("From trait: {:?}", &v);
v
}
#[test]
fn test_into_inner_derive() {
let wrapper = MyWrapper("Hello, world!".to_string());
let inner = wrapper.into_inner();
println!("From derive: {:?}", &inner);
assert_eq!(inner, "Hello, world!");
}
#[test]
fn test_into_inner_trait() {
let wrapper = MyWrapper("Hello, world!".to_string());
let inner = print_inner(wrapper);
assert_eq!(inner, "Hello, world!");
}
Traits§
- Into
Inner - A trait for consuming a wrapper type and extracting its inner value.
Derive Macros§
- Into
Inner - A derive macro for automatically implementing the
IntoInner
trait for tuple structs.