Variance of raw pointers in structs

⚓ Rust    📅 2025-07-22    👤 surdeus    👁️ 2      

surdeus

Hi!

Can someone helps me understand why is *const T covariant over T (is it really?)?
I'm onto a problem where Borrower<T> needs to be... fullyvariant I guess? It means that Borrower<T> can accept T regardless of its lifetime. (Borrower<T> can outlive T and T can outlive Borrower<T>). Borrower<T> stores a raw pointer of T.

My code has a tiny bit of unsafe code, that should prevent UB by not dereferencing *const T when it doesn't exist. I do manage lifetimes manually. (Borrower::get returns Some with a guard that ensures data isn't mutated during the process, and if the data (T) has outlived Borrower, it just returns None)

How to define Borrower<T> so that its lifetime isn't related / tight to T?

4 posts - 2 participants

Read full topic

🏷️ Rust_feed