Pinning & lazy-static

I am using a Pin<P> type for my API. Within the functions which require self: Pin<&mut Self> in the signature, some of them call upon a lazy static singleton. I am getting this message:

error[E0277]: the trait bound `connection::connection::ConnectionHandle<'_, '_>: lazy_static::__Deref` is not satisfied
  --> hyxewave_net\src\connection\
31 |         Self { handle: unsafe { Pin::new(handle) }, connection_client: cnac.clone(), adjacent_node: peer_nac.clone() }
   |                                 ^^^^^^^^^^^^^^^^^^ the trait `lazy_static::__Deref` is not implemented for `connection::connection::ConnectionHandle<'_, '_>`
   = note: required by `std::pin::Pin::<P>::new`

As such, I implemented lazy_static::__Deref for ConnectionHandle<'_, '_> as follows:

impl<'cxn, 'bridge: 'cxn> lazy_static::__Deref for ConnectionHandle<'cxn, 'bridge> {
    type Target = Self;

    fn deref(&self) -> &Self::Target {

I am now getting a recursion error since autoderefing seems to be happening cyclically. What would be a good way to solve this problem?

What is ConnectionHandle a pointer to? Pin<P> only makes sense if P is a pointer (see std::pin::Pin).

The impl you wrote says ConnectionHandle is a pointer to a ConnectionHandle which is probably not right.

I'm trying to keep the struct pinned to memory. It exists in a higher-level container struct as Pin<T>. Should i make an inner type (e.g., RefCell<T>) and pin that to memory?

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