Re: [PATCH v3 5/7] rust: devres: add DevresLt for ForLt-aware device resource access
From: Alexandre Courbot
Date: Wed Jun 24 2026 - 10:54:52 EST
On Fri Jun 19, 2026 at 8:08 AM JST, Danilo Krummrich wrote:
> Devres<T> stores resources as T and returns &'a T from access(). For
> lifetime-parameterized types like Bar<'a, SIZE> that are transmuted to
> 'static for storage, this exposes the synthetic 'static lifetime to
> callers -- any method on the stored type that returns a reference with
> its lifetime parameter would yield a &'static reference, which is
> unsound.
>
> Add DevresLt<F: ForLt>, a thin wrapper around Devres<F::Of<'static>>
> that shortens the stored 'static lifetime to the caller's borrow
> lifetime in all access methods.
>
> DevresLt::new() is unsafe because the caller must guarantee that the
> data remains valid for the device's full bound scope; the internal
> transmute from F::Of<'a> to F::Of<'static> would otherwise allow
> use-after-free.
>
> Two access patterns are provided:
>
> - CovariantForLt types get direct-reference accessors (access,
> try_access) that return shortened references via
> CovariantForLt::cast_ref.
>
> - Plain ForLt types use closure-based accessors (access_with,
> try_access_with) whose universally quantified lifetime prevents
> callers from smuggling in concrete short-lived references.
>
> Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
Reviewed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>