rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Finn Behrens <me@kloenk.de>
To: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Cc: Wedson Almeida Filho <wedsonaf@google.com>,
	rust-for-linux <rust-for-linux@vger.kernel.org>
Subject: Re: PointerWrapper Trait vs croe::ops::Deref
Date: Mon, 10 May 2021 16:04:54 +0200	[thread overview]
Message-ID: <5F0569E2-F09D-4D58-99D9-B338DE6D1A15@kloenk.de> (raw)
In-Reply-To: <CANiq72=8M3ALxrmdYvHNfDVX9F4Se0=a-EbzrZPgVv1xsh932Q@mail.gmail.com>

Howdy,

> On 9. May 2021, at 16:21, Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:
> 
> On Sun, May 9, 2021 at 12:25 PM Finn Behrens <me@kloenk.de> wrote:
>> 
>> Maybe we could also remove the result, as it would possible be an invariant, and never happen as the pointer should already be valid.
> 
> If it can fail, we cannot use `Deref` to begin with. So yeah, to make
> the examples equivalent, the first one should not return a `Result`.
> 
> Now, assuming it cannot fail, what I mentioned in the meeting is that
> with `Deref` all this compiles:
> 
> wrapper.deref().number
> (*wrapper).number
> wrapper.number
> 
> The question boils down to: do we want this shortcut or not?

I would not like to decide that on my own :-).
After thinking about it I think we don’t want that shortcut, as the other way give the same behaviour, but it is explicit.

> Having said that, I am not sure I understand the traits shown here.
> Isn't `PointerWrapper` meant only to save a `Box<T>` and similar types
> (which already implement `Deref` themselves etc.) as a pointer in a C
> data structure? (which is why `into_pointer()` consumes the object).
> 

All Pointers I’m wrapping are got from the C side. Either because there is a special malloc flag and some initialisation to the C struct, or as this is in a function call, and therefor the object is constructed out of my reach.
I usually do not consume the pointer, as I have to give the pointer to multiple function on the C side.

> In the examples shown, the new `get_pointer()` does not consume
> `self`; and `main()` creates a `Wrapper` using `from_pointer()`. But
> the idea is that you already have an object (constructed via other
> means), transform it into a raw pointer, and then later on
> re-construct the original object back from that pointer.
> 
> So `PointerWrapper` trait is like a `SavedAsPointer` trait, if you will.

Renaming to `SavedAsPointer` sounds great.

> 
> Cheers,
> Miguel

CU,
Finn


      reply	other threads:[~2021-05-10 14:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-09 10:25 PointerWrapper Trait vs croe::ops::Deref Finn Behrens
2021-05-09 14:21 ` Miguel Ojeda
2021-05-10 14:04   ` Finn Behrens [this message]

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=5F0569E2-F09D-4D58-99D9-B338DE6D1A15@kloenk.de \
    --to=me@kloenk.de \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@google.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).