rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "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>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"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 09:40:27 -0800	[thread overview]
Message-ID: <ZWTUi8mNvjszP7yw@boqun-archlinux> (raw)
In-Reply-To: <CAH5fLghV0jtXCJ2aetH9ZNvJ=Tu-48DCjNQMPxAWf1GW-1Mbcw@mail.gmail.com>

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()
	    }
	}

Regards,
Boqun

> provides a borrow as a reference.
> 
> Alice

  reply	other threads:[~2023-11-27 17:41 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 [this message]
2023-11-27 20:37     ` Benno Lossin
2023-11-27 23:43       ` Boqun Feng
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=ZWTUi8mNvjszP7yw@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).