Messages in this thread | | | Date | Sat, 1 Apr 2023 08:01:00 +0100 | From | Gary Guo <> | Subject | Re: [PATCH 10/13] rust: introduce `Task::current` |
| |
On Sat, 1 Apr 2023 01:09:18 -0300 Wedson Almeida Filho <wedsonaf@gmail.com> wrote:
> 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
Hi Wedson,
My apologies for the confusion. `&*Task::current()` is not sufficient. I typed too quick without double checking.
However it is still true that `TaskRef<'static>` is unsound, and it can be retrieved from `current()`. The missing step is `&'static TaskRef<'static>`.
So you can write `&*Box::leak(Box::try_new(Task::current()).unwrap())` and get `&'static Task`.
Best, Gary
| |