`Deref` and `DerefMut` derive macros

⚓ Rust    📅 2025-08-25    👤 surdeus    👁️ 13      

surdeus

Warning

This post was published 99 days ago. The information described in this article may have changed.

Hello! I've been working with Rust for the past couple of months. One thing I've seen many times in our codebase is something like the following:

struct MyStruct(SomeType);
// ...
my_struct.0.my_method();

For that I've implemented some Deref and DerefMut from time to time. Why isn't there a derive macro for that? It could be useful to expose the wrapped type interface whilst extending more functionality. Is there anything I'm completely missing here?

#[derive(deref(0))]
struct MyStruct(SomeType);
// ...
my_struct.my_method();

// Or acessing a field
#[derive(deref(inner_field))]
struct MyStruct{
    inner_field: SomeType,
    other_field: OtherType,
};
// ...
my_struct.my_method();

3 posts - 3 participants

Read full topic

🏷️ Rust_feed