rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: Boqun Feng <boqun.feng@gmail.com>, 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>,
	"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 20:37:12 +0000	[thread overview]
Message-ID: <cb7b2e48-0e05-483d-a964-9f895910b494@proton.me> (raw)
In-Reply-To: <ZWTUi8mNvjszP7yw@boqun-archlinux>

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
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 :)

-- 
Cheers,
Benno



  reply	other threads:[~2023-11-27 20:37 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 [this message]
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=cb7b2e48-0e05-483d-a964-9f895910b494@proton.me \
    --to=benno.lossin@proton.me \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.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).