From: Boqun Feng <boqun.feng@gmail.com>
To: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Cc: wedsonaf@gmail.com, alex.gaynor@gmail.com,
bjorn3_gh@protonmail.com, gary@garyguo.net,
linux-kernel@vger.kernel.org, ojeda@kernel.org,
rust-for-linux@vger.kernel.org
Subject: Re: [PATCH 2/5] rust: types: introduce `ForeignOwnable`
Date: Sat, 28 Jan 2023 14:07:02 -0800 [thread overview]
Message-ID: <Y9WchuzdL8HBrqq7@boqun-archlinux> (raw)
In-Reply-To: <3dcd0379-b375-32d2-4956-c42afd451693@gmail.com>
On Sat, Jan 28, 2023 at 05:46:50PM -0300, Martin Rodriguez Reboredo wrote:
> On 1/28/23 14:05, Boqun Feng wrote:
> > On Sat, Jan 28, 2023 at 11:53:45AM -0300, Martin Rodriguez Reboredo wrote:
> > [...]
> >>> + /// Borrows a foreign-owned object.
> >>> + ///
> >>> + /// # Safety
> >>> + ///
> >>> + /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
> >>> + /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
> >>> + /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow_mut`]
> >>> + /// for this object must have been dropped.
> >>> + unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a>;
> >>> +
> >>> + /// Mutably borrows a foreign-owned object.
> >>> + ///
> >>> + /// # Safety
> >>> + ///
> >>> + /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
> >>> + /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
> >>> + /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
> >>> + /// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
> >>> + unsafe fn borrow_mut<T: ForeignOwnable>(ptr: *const core::ffi::c_void) -> ScopeGuard<T, fn(T)> {
> >>> + // SAFETY: The safety requirements ensure that `ptr` came from a previous call to
> >>> + // `into_foreign`.
> >>> + ScopeGuard::new_with_data(unsafe { T::from_foreign(ptr) }, |d| {
> >>> + d.into_foreign();
> >>> + })
> >>> + }
> >>
> >> Could these three methods have a borrowing equivalent? When I was
> >> working on some features for the USB module I've stumbled upon the case
> >> of having to encode a pointer (with a pivot) and I cannot do it without
> >> taking ownership of the pointer.
> >>
> >
> > *const T is Copy, so you can still use it after pass it to a function or
> > a new binding, e.g.
> >
> > pub fn use_ptr(ptr: *const i32) { .. }
> >
> > let p: *const i32 = some_func();
> >
> > let q = p;
> >
> > // q is just a copy of p
> > use_ptr(p);
> > // passing to a function parameter is just copying
> > use_ptr(p);
> >
> > maybe I'm missing something subtle, but if you have an example I can
> > help take a look.
> >
> > Regards,
> > Boqun
> >
>
> I'll use a much more simple example. If I want to take the byte offset
> between two `ForeignWrapper`s I'd have to take ownership of them, but I
> don't see it desirable in some cases.
>
> fn byte_offset<P: PointerWrapper>(ptr: P, pivot: P) -> isize {
> unsafe {
> ptr.into_pointer().cast::<u8>()
> .byte_offset(pivot.into_pointer().cast())
> }
> }
>
> But if there was an `as_pointer(&self) -> *const c_void` method then the
> above function will be able to borrow both `ForeignWrapper`s.
>
> fn byte_offset<P: PointerWrapper>(ptr: &P, pivot: &P) -> isize {
> unsafe {
> ptr.as_pointer().cast::<u8>()
> .byte_offset(pivot.as_pointer().cast())
> }
> }
>
> Obviously those methods that borrow will announce invariancies in their
> doc comments. If these can exist then great and if not then another
> solution could be explored.
>
For this particular use case, IIUC, what you need is exactly `AsRef`:
fn byte_offset<T, P: AsRef<T>>(ptr: &P, pivot: &P) -> isize {
unsafe {
(ptr.as_ref() as *const _).cast::<u8>()
.byte_offset((pivot.as_ref() *const _).cast())
}
}
or you can implement a as_pointer() helper function to avoid the
duplicate `.as_ref() as *const _`:
fn as_pointer<T, P: AsRef<T>>(ptr: &P) -> *const T {
ptr.as_ref() as *const T
}
Although, we need to `impl AsRef<T> for Arc<T>` to make it work for
`Arc<T>`, which is currently missing.
Regards,
Boqun
> >>> +
> >>> + /// Converts a foreign-owned object back to a Rust-owned one.
> >>> + ///
> >>> + /// # Safety
> >>> + ///
> >>> + /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
> >>> + /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
> >>> + /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
> >>> + /// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
> >>> + unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self;
> >>> +}
> >>> +
> >>> /// Runs a cleanup function/closure when dropped.
> >>> ///
> >>> /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.
> >>> --
> >>> 2.34.1
> >>
> >> Aside from these comments I observe that there's a possibility to make
> >> ForeignOwnable a const trait and have non const implementors. Otherwise
> >> if these things are out of scope, no problem whatsoever and this has my
> >> OK.
> >>
> >> Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
next prev parent reply other threads:[~2023-01-28 22:07 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-19 17:40 [PATCH 1/5] rust: types: introduce `ScopeGuard` Wedson Almeida Filho
2023-01-19 17:40 ` [PATCH 2/5] rust: types: introduce `ForeignOwnable` Wedson Almeida Filho
2023-01-20 19:59 ` Boqun Feng
2023-01-22 6:34 ` Wedson Almeida Filho
2023-01-27 13:55 ` Gary Guo
2023-01-30 5:59 ` Wedson Almeida Filho
2023-01-28 10:42 ` Vincenzo Palazzo
2023-01-28 14:53 ` Martin Rodriguez Reboredo
2023-01-28 17:05 ` Boqun Feng
2023-01-28 20:46 ` Martin Rodriguez Reboredo
2023-01-28 22:07 ` Boqun Feng [this message]
2023-01-29 3:52 ` Martin Rodriguez Reboredo
2023-01-19 17:40 ` [PATCH 3/5] rust: types: implement `ForeignOwnable` for `Box<T>` Wedson Almeida Filho
2023-01-27 13:56 ` Gary Guo
2023-01-28 10:50 ` Vincenzo Palazzo
2023-01-30 5:33 ` Alice Ferrazzi
2023-01-19 17:40 ` [PATCH 4/5] rust: types: implement `ForeignOwnable` for the unit type Wedson Almeida Filho
2023-01-27 14:03 ` Gary Guo
2023-01-27 14:11 ` Miguel Ojeda
2023-01-28 11:13 ` Vincenzo Palazzo
2023-01-30 17:21 ` Miguel Ojeda
2023-01-30 5:55 ` Wedson Almeida Filho
2023-01-28 11:14 ` Vincenzo Palazzo
2023-01-19 17:40 ` [PATCH 5/5] rust: types: implement `ForeignOwnable` for `Arc<T>` Wedson Almeida Filho
2023-01-27 14:04 ` Gary Guo
2023-01-28 11:15 ` Vincenzo Palazzo
2023-01-30 5:35 ` Alice Ferrazzi
2023-01-20 6:23 ` [PATCH 1/5] rust: types: introduce `ScopeGuard` Boqun Feng
2023-01-22 6:31 ` Wedson Almeida Filho
2023-01-28 10:38 ` Vincenzo Palazzo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y9WchuzdL8HBrqq7@boqun-archlinux \
--to=boqun.feng@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=bjorn3_gh@protonmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
--cc=yakoyoku@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).