Re: [PATCH 10/13] rust: introduce `Task::current`
From: Wedson Almeida Filho
Date: Sat Apr 01 2023 - 00:09:35 EST
Gary, thanks for reviewing!
On Fri, Mar 31, 2023 at 03:47:01AM +0100, Gary Guo wrote:
>
> I don't think this API is sound, as you can do `&*Task::current()` and
> get a `&'static Task`, which is very problematic.
One thing that isn't clear to me is: how do you get a 'static lifetime in the
example above?
Altough `TaskRef` does have an arbitrary lifetime param, that's not the lifetime
that the returned `Task` reference gets. For illustration, I've explicitly added
a lifetime 'a in the impl below:
impl Deref for TaskRef<'_> {
type Target = Task;
fn deref(&'a self) -> &'a Self::Target {
self.task
}
}
Which means that the borrow of the `TaskRef` you use to call `deref` must
outlive the returned `Task`.
So how do you get a `TaskRef` with a static lifetime to begin with? Or is there
another trick to get the `&'static Task` that I can't see?
Thanks,
-Wedson