| Crates.io | deref-derives |
| lib.rs | deref-derives |
| version | 0.1.3 |
| created_at | 2025-11-07 09:15:07.186287+00 |
| updated_at | 2025-11-08 18:46:11.198804+00 |
| description | A Rust library for dereferencing types |
| homepage | |
| repository | https://github.com/symphony-rs/deref-rs |
| max_upload_size | |
| id | 1921366 |
| size | 14,053 |
A simple Rust autodereference implementation that provides macros and derive macros to easily implement the Deref and DerefMut traits for your custom types.
Deref and DerefMut implementationderef_mut! or DerefMut, Deref is automatically implementedAdd this to your Cargo.toml:
[dependencies]
deref = "0.1.0"
use deref::{deref, deref_mut};
struct MyType {
field: TargetType,
}
struct TargetType;
// Implement Deref for MyType
deref!(MyType, TargetType, field);
// Implement both Deref and DerefMut for MyType
// Note: This automatically implements Deref as well
deref_mut!(MyType, TargetType, field);
use deref::{deref, deref_mut};
struct SrVec<T> {
vec: Vec<T>,
}
// Implement Deref for a generic type
deref!(<T>, SrVec<T>, Vec<T>, vec);
// Implement both Deref and DerefMut for a generic type
// Note: This automatically implements Deref as well
deref_mut!(<T>, SrVec<T>, Vec<T>, vec);
use deref::{Deref, DerefMut};
#[derive(Deref)]
struct Hello<T> {
#[auto_ref]
inner: T,
}
#[derive(DerefMut)]
struct HelloMut<T> {
#[auto_ref]
inner: T,
}
// Note: DerefMut automatically implements Deref as well
use deref::Deref;
#[derive(Deref)]
struct Wrapper {
#[auto_ref]
data: String,
}
fn main() {
let wrapper = Wrapper {
data: "Hello, World!".to_string(),
};
// Can access String methods directly through wrapper
println!("Length: {}", wrapper.len());
}
use deref_rs::Deref;
#[derive(Deref)]
struct TupleWrapper(String);
#[derive(Deref)]
struct GenericTupleWrapper<T>(#[auto_ref] T);
fn main() {
let wrapper = TupleWrapper("Hello, Tuple!".to_string());
// Can access String methods directly through wrapper
println!("Length: {}", wrapper.len());
}
use deref_rs::Deref;
use std::ops::Deref as DerefTrait;
#[derive(Deref)]
struct MyBox<T> {
#[auto_ref]
value: T,
}
impl<T> MyBox<T> {
fn new(value: T) -> Self {
MyBox { value }
}
}
fn main() {
let my_box = MyBox::new(42);
// Can access the inner value directly
println!("Value: {}", *my_box);
// Can call methods on the inner type
let string_box = MyBox::new("Hello".to_string());
println!("Length: {}", string_box.len());
}
use deref::{Deref, DerefMut, deref_mut};
#[derive(Debug, DerefMut)]
struct Inner<T> {
#[auto_ref]
value: T,
}
// Note: DerefMut automatically implements Deref for Inner<T>
#[derive(Debug)]
struct Outer<T> {
inner: Inner<T>,
}
// Implement DerefMut for Outer using the macro
// Note: This automatically implements Deref as well
deref_mut!(<T>, Outer<T>, T, inner);
fn main() {
let mut outer = Outer {
inner: Inner {
value: "Hello, World".to_string(),
},
};
println!("{:?}", outer);
// Can modify the inner value directly
*outer += "!";
println!("{:?}", outer);
}
deref! MacroImplements the Deref trait for a struct.
deref!(TYPE, TARGET_TYPE, FIELD)
deref!(GENERIC_PARAMS; TYPE, TARGET_TYPE, FIELD)
deref_mut! MacroImplements both Deref and DerefMut traits for a struct.
deref_mut!(TYPE, TARGET_TYPE, FIELD)
deref_mut!(GENERIC_PARAMS, TYPE, TARGET_TYPE, FIELD)
Note: The deref_mut! macro automatically implements both Deref and DerefMut traits. You don't need to separately use deref! when using deref_mut!.
Deref Derive MacroImplements the Deref trait using the #[auto_ref] attribute to mark the target field.
DerefMut Derive MacroImplements both Deref and DerefMut traits using the #[auto_ref] attribute to mark the target field.
Note: The DerefMut derive macro automatically implements both Deref and DerefMut traits. You don't need to separately derive Deref when deriving DerefMut.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
deref! and deref_mut! macrosDeref and DerefMut derive macros