deref-rs
A simple Rust autodereference implementation that provides macros and derive macros to easily implement the Deref and DerefMut traits for your custom types.
Features
- Macro-based implementation: Simple procedural macros for quick
DerefandDerefMutimplementation - Derive macro support: Attribute-based derive macros for more ergonomic usage
- Generic type support: Works with both regular types and generic types
- Field access: Supports both named fields and tuple-style field access
- Automatic Deref implementation: When using
deref_mut!orDerefMut,Derefis automatically implemented
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Using Procedural Macros
Basic Usage
use ;
;
// Implement Deref for MyType
deref!;
// Implement both Deref and DerefMut for MyType
// Note: This automatically implements Deref as well
deref_mut!;
With Generic Types
use ;
// Implement Deref for a generic type
deref!;
// Implement both Deref and DerefMut for a generic type
// Note: This automatically implements Deref as well
deref_mut!;
Using Derive Macros
Basic Usage
use ;
// Note: DerefMut automatically implements Deref as well
With Named Fields
use Deref;
With Tuple Structs
use Deref;
;
;
Examples
Smart Pointer Implementation
use Deref;
use Deref as DerefTrait;
Nested Dereferencing
use ;
// Note: DerefMut automatically implements Deref for Inner<T>
// Implement DerefMut for Outer using the macro
// Note: This automatically implements Deref as well
deref_mut!;
API Reference
Macros
deref! Macro
Implements the Deref trait for a struct.
deref!
deref!
deref_mut! Macro
Implements both Deref and DerefMut traits for a struct.
deref_mut!
deref_mut!
Note: The deref_mut! macro automatically implements both Deref and DerefMut traits. You don't need to separately use deref! when using deref_mut!.
Derive Macros
Deref Derive Macro
Implements the Deref trait using the #[auto_ref] attribute to mark the target field.
DerefMut Derive Macro
Implements 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.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Changelog
0.1.0
- Initial release
- Basic
deref!andderef_mut!macros DerefandDerefMutderive macros- Support for generic types
- Automatic Deref implementation when using DerefMut