[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CANeycqpUaKuKCjZyw1PiPzMo3rqZQXiFONZOKq1hbhVUA25+tQ@mail.gmail.com>
Date: Mon, 30 Jan 2023 02:55:31 -0300
From: Wedson Almeida Filho <wedsonaf@...il.com>
To: Gary Guo <gary@...yguo.net>
Cc: rust-for-linux@...r.kernel.org, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH 4/5] rust: types: implement `ForeignOwnable` for the unit type
On Fri, 27 Jan 2023 at 11:03, Gary Guo <gary@...yguo.net> wrote:
>
> On Thu, 19 Jan 2023 14:40:35 -0300
> Wedson Almeida Filho <wedsonaf@...il.com> wrote:
>
> > This allows us to use the unit type `()` when we have no object whose
> > ownership must be managed but one implementing the `ForeignOwnable`
> > trait is needed.
> >
> > Signed-off-by: Wedson Almeida Filho <wedsonaf@...il.com>
> > ---
> > rust/kernel/types.rs | 13 +++++++++++++
> > 1 file changed, 13 insertions(+)
> >
> > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> > index e037c262f23e..8f80cffbff59 100644
> > --- a/rust/kernel/types.rs
> > +++ b/rust/kernel/types.rs
> > @@ -85,6 +85,19 @@ impl<T: 'static> ForeignOwnable for Box<T> {
> > }
> > }
> >
> > +impl ForeignOwnable for () {
> > + type Borrowed<'a> = ();
> > +
> > + fn into_foreign(self) -> *const core::ffi::c_void {
> > + // We use 1 to be different from a null pointer.
> > + 1usize as _
>
> this should really be `core::ptr::invalid(1)`. That's currently
> unstable, but can be equivalently written as
> `NonNull::<()>::dangling().as_ptr()`.
This has the wrong type, but I agree with the spirit of the
suggestion. I'll add it to V2.
> This has a different semantic meaning from `as` since it explicitly
> suggests an invalid provenance and thus will not alias with other
> pointers. (Although I don't think compiler currently can take advantage
> of this fact yet)
>
> > + }
> > +
> > + unsafe fn borrow<'a>(_: *const core::ffi::c_void) -> Self::Borrowed<'a> {}
> > +
> > + unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {}
> > +}
> > +
> > /// Runs a cleanup function/closure when dropped.
> > ///
> > /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.
>
Powered by blists - more mailing lists