From: Boqun Feng <boqun.feng@gmail.com>
To: Benno Lossin <benno.lossin@proton.me>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
"Maíra Canal" <mcanal@igalia.com>,
"Asahi Lina" <lina@asahilina.net>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Matthew Wilcox" <willy@infradead.org>,
rust-for-linux@vger.kernel.org, kernel-dev@igalia.com
Subject: Re: [PATCH v4] rust: xarray: Add an abstraction for XArray
Date: Mon, 27 Nov 2023 15:43:04 -0800 [thread overview]
Message-ID: <ZWUpiKWEjUlvjBXs@boqun-archlinux> (raw)
In-Reply-To: <cb7b2e48-0e05-483d-a964-9f895910b494@proton.me>
On Mon, Nov 27, 2023 at 08:37:12PM +0000, Benno Lossin wrote:
> On 27.11.23 18:40, Boqun Feng wrote:
> > On Mon, Nov 27, 2023 at 02:51:33PM +0100, Alice Ryhl wrote:
> >> On Sun, Nov 26, 2023 at 2:13 PM Maíra Canal <mcanal@igalia.com> wrote:
> >>> +// Convenience impl for `ForeignOwnable` types whose `Borrowed`
> >>> +// form implements Deref.
> >>> +impl<'a, T: ForeignOwnable> Deref for Guard<'a, T>
> >>> +where
> >>> + T::Borrowed<'static>: Deref,
> >>> +{
> >>> + type Target = <T::Borrowed<'static> as Deref>::Target;
> >>> +
> >>> + fn deref(&self) -> &Self::Target {
> >>> + // SAFETY: See the `borrow()` method. The dereferenced `T::Borrowed` value
> >>> + // must share the same lifetime, so we can return a reference to it.
> >>> + unsafe { &*(T::borrow(self.0 as _).deref() as *const _) }
> >>> + }
> >>> +}
> >>
> >> I don't think this is sound. Deref could return a reference into the
> >> `T::Borrowed` itself, but it doesn't outlive this function. I would
> >> either omit this impl, or provide a sub-trait for ForeignOwnable that
> >
> > Agreed. FWIW, there was some discussion around this:
> >
> > https://github.com/Rust-for-Linux/linux/pull/952#discussion_r1096791211
> >
> > now think about it, how about the following?
> >
> > impl<'a, T: ForeignOwnable> Deref for Guard<'a, T>
> > where
> > for<'b> T::Borrowed<'b>: Into<&'b T>,
> >
> > {
> > type Target = T;
> >
> > fn deref(&self) -> &Self::Target {
> > // SAFETY: See the `borrow()` method.
> > unsafe { T::borrow(self.0 as _) }.into()
> > }
> > }
>
> This one seems wrong, since `Borrowed` will be `&U` when `T = Box<U>`
> and `&U: Into<&Box<U>>` is not satisfied. And if `T = Arc<U>`, then
> `Borrowed = ArcBorrow`. If you want to take this, then you can try to
Right, I was missing that T is ForeignOwnable.
> make Gary's suggestion work (from the Github discussion that Boqun posted):
>
> impl<'a, T: ForeignOwnable> Deref for Guard<'a, T>
> where
> T::Borrowed<'a>: Deref,
> for<'b> T::Borrowed<'b>: Into<&'b <T::Borrowed<'a> as Deref>::Target>,
> {
> type Target = <T::Borrowed<'a> as Deref>::Target;
>
> fn deref(&self) -> &Self::Target {
> self.borrow().into()
> }
> }
>
> Also this is nicer, since there is no `unsafe` code :)
>
One thing is that it seems impossible to impl Into<&T> for ArcBorrow,
for example, if we have an Wrapper struct representting ArcBorrow:
struct Wrapper<'a, T>(&'a T);
`From` side doesn't work:
impl<'a, T> From<Wrapper<'a, T>> for &'a T {
fn from(value: Wrapper<'a, T>) -> Self {
value.0
}
}
IIUC, it's because of the orphan rules.
`Into` side doesn't work either:
impl<'a, T> Into<&'a T> for Wrapper<'a, T> {
fn into(self) -> &'a T {
self.0
}
}
I'm still trying to understand the reason:
error[E0119]: conflicting implementations of trait `Into<&_>` for type `Wrapper<'_, _>`
--> src/lib.rs:9:1
|
9 | impl<'a, T> Into<&'a T> for Wrapper<'a, T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `core`:
- impl<T, U> Into<U> for T
where U: From<T>;
= note: downstream crates may implement trait `std::convert::From<Wrapper<'_, _>>` for type `&_`
Playground link:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=36a6a94740c00ba8ca47f773a423eb9e
Interesting enough, `Into<Option<&'a T>>` works:
impl<'a, T> Into<Option<&'a T>> for Wrapper<'a, T> {
fn into(self) -> Option<&'a T> {
Some(self.0)
}
}
Of course, we can always implement our own Into-like trait, e.g.
IntoRef.
Regards,
Boqun
> --
> Cheers,
> Benno
>
>
next prev parent reply other threads:[~2023-11-27 23:43 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-26 13:01 [PATCH v4] rust: xarray: Add an abstraction for XArray Maíra Canal
2023-11-27 13:51 ` Alice Ryhl
2023-11-27 17:40 ` Boqun Feng
2023-11-27 20:37 ` Benno Lossin
2023-11-27 23:43 ` Boqun Feng [this message]
2023-11-27 17:19 ` Benno Lossin
2023-11-27 17:47 ` Boqun Feng
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=ZWUpiKWEjUlvjBXs@boqun-archlinux \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=gary@garyguo.net \
--cc=kernel-dev@igalia.com \
--cc=lina@asahilina.net \
--cc=mcanal@igalia.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
--cc=willy@infradead.org \
/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).