ByAddress over weak

I've a set of layers over other layers, where the basemost layer structure is a one-element tuple struct over a std::rc::Weak.

According to the ByAddress's README, the ByAddress type works for any type that implements Deref.

I've gave a little look at this part of the source code:

impl<T> ByAddress<T>
where
    T: ?Sized + Deref,
{
    /// Convenience method for pointer casts.
    fn addr(&self) -> *const T::Target {
        &*self.0
    }

    /// Convert `&T` to `&ByAddress<T>`.
    pub fn from_ref(r: &T) -> &Self {
        // SAFETY: `struct ByAddress` is `repr(transparent)`.
        unsafe {
            &*(r as *const T as *const Self)
        }
    }
}

Will something like ByAddress(weak_pointer) and ByAddress(Layer2(Layer1(weak_pointer)) work for taking the address of weak_pointer rather than what the layers directly dereference to?

it looks like ByAddress doesn't support weak refs

1 Like

I forgot that in my case I already implement Hash, and PartialEq for reference hashing or equality, just missed Eq, so I suppose I don't need ByAddress in this context, as I can use my data type as a key in HashMap.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.