Inner exposure of fields/methods in an Arc

One idea which has been puzzling me is that if you wrap an Arc around an instantiated struct T, you can observe the inner fields and methods of T from Arc<T>... I would think that you would have to first access the inner field of the Arc before interacting with the fields of T? Is there some type of compiler trick going on, or what's happening?

Please see the Deref trait.

1 Like

There is also a chapter in the book about it and an other one explaining methods.

Note that the same technique links String to &str instead of &String and Vec<T> to &[T] instead of &Vec<T>.

Ah, okay. So, would this also work for tuples? Let's say your target for Deref is a tuple of two types. Is that possible with the current system?

E.g., I want inner exposure of types U1 and U2

The Deref trait is not implemented for tuples, but you can write tuple.0 or tuple.1 to access each value.

1 Like

Tuples are essentially syntactic sugar for a new struct. So anything that would work for a normal struct type will work for tuples.

(u32, String)

// would be compiled as

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct __tuple_123 {
 _0: u32,
 _1: String,
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.