From: Kevin Wolf <kwolf@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org,
"Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
"Manos Pitsidianakis" <manos.pitsidianakis@linaro.org>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Daniel P . Berrangé" <berrange@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Hanna Czenczek" <hreitz@redhat.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>
Subject: Re: [PATCH 03/14] rust: define traits and pointer wrappers to convert from/to C representations
Date: Fri, 27 Sep 2024 18:09:52 +0200 [thread overview]
Message-ID: <ZvbY0PMWmJPZN7Sq@redhat.com> (raw)
In-Reply-To: <20240701145853.1394967-4-pbonzini@redhat.com>
Hi Paolo,
as you asked me at KVM Forum to have a look at this, I'm going through
it now.
Am 01.07.2024 um 16:58 hat Paolo Bonzini geschrieben:
> The qemu::util::foreign module provides:
>
> - A trait for structs that can be converted to a C ("foreign") representation
> (CloneToForeign)
>
> - A trait for structs that can be built from a C ("foreign") representation
> (FromForeign), and the utility IntoNative that can be used with less typing
> (similar to the standard library's From and Into pair)
It makes sense to me that we'll need something to convert data and that
this usually means creating a new instance, i.e. cloning. However, while
it's obvious that this is similar to From/Into, the part I'm missing
here is what's different from it.
In other words, couldn't we just implement the normal From trait between
FFI types and the native equivalent?
> - Automatic implementations of the above traits for Option<>, supporting NULL
> pointers
This is nice.
> - A wrapper for a pointer that automatically frees the contained data. If
> a struct XYZ implements CloneToForeign, you can build an OwnedPointer<XYZ>
> and it will free the contents automatically unless you retrieve it with
> owned_ptr.into_inner()
Something here feels off to me.
At first, I thought it might be only about naming. This is not about
owning the pointer (which you probably do anyway), but that the pointer
owns the object it points to. This concept has in fact a name in Rust:
It's a Box.
The major difference compared to Box is that we're using a different
allocator. Not sure if the allocator APIs would actually be viable, but
they're not stable anyway - but let's at least name this thing in way
that draws the obvious parallel. Maybe ForeignBox.
But the other thing that doesn't feel quite right is how this is coupled
with CloneToForeign. Freeing is different from cloning, and it really
relates to the foreign type itself, and not to the one that can be
cloned into a foreign type.
Bringing both together, what a Box doesn't usually have is a function
pointer for freeing. We probably don't need it here either, almost
everything needs g_free(). There is a different free_foreign()
implementation for Error, but arguably this could be changed:
bindings::Error should then implement Drop for the inner value (with an
error_free_inner() which needs to be exported separately from C first),
and then ForeignBox can just drop the Error object and g_free() the
pointer itself like it would do with any other value.
(Your implementation actually calls free() instead of g_free(). We
generally try to avoid that in our C code, so we should probably avoid
it in Rust, too.)
Kevin
next prev parent reply other threads:[~2024-09-27 16:11 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-01 14:58 [PATCH 00/14] rust: example of bindings code for Rust in QEMU Paolo Bonzini
2024-07-01 14:58 ` [PATCH 01/14] add skeleton Paolo Bonzini
2024-07-01 14:58 ` [PATCH 02/14] set expectations Paolo Bonzini
2024-07-01 14:58 ` [PATCH 03/14] rust: define traits and pointer wrappers to convert from/to C representations Paolo Bonzini
2024-07-03 12:48 ` Marc-André Lureau
2024-07-03 12:58 ` Marc-André Lureau
2024-07-03 15:55 ` Paolo Bonzini
2024-09-27 16:09 ` Kevin Wolf [this message]
2024-09-27 20:19 ` Paolo Bonzini
2024-09-27 19:36 ` Stefan Hajnoczi
2024-09-27 20:26 ` Paolo Bonzini
2025-05-26 12:35 ` Paolo Bonzini
2024-07-01 14:58 ` [PATCH 04/14] rust: add tests for util::foreign Paolo Bonzini
2024-07-01 14:58 ` [PATCH 05/14] rust: define wrappers for Error Paolo Bonzini
2024-07-01 14:58 ` [PATCH 06/14] rust: define wrappers for basic QOM concepts Paolo Bonzini
2024-07-01 14:58 ` [PATCH 07/14] rust: define wrappers for methods of the QOM Object class Paolo Bonzini
2024-07-01 14:58 ` [PATCH 08/14] rust: define wrappers for methods of the QOM Device class Paolo Bonzini
2024-07-01 14:58 ` [PATCH 09/14] rust: add idiomatic bindings to define Object subclasses Paolo Bonzini
2024-07-01 14:58 ` [PATCH 10/14] rust: add idiomatic bindings to define Device subclasses Paolo Bonzini
2024-07-01 14:58 ` [PATCH 11/14] rust: replace std::ffi::c_char with libc::c_char Paolo Bonzini
2024-07-01 14:58 ` [PATCH 12/14] rust: replace c"" literals with cstr crate Paolo Bonzini
2024-07-01 14:58 ` [PATCH 13/14] rust: introduce alternative to offset_of! Paolo Bonzini
2024-07-01 14:58 ` [PATCH 14/14] rust: use version of toml_edit that does not require new Rust Paolo Bonzini
2024-07-04 19:26 ` [PATCH 00/14] rust: example of bindings code for Rust in QEMU Pierrick Bouvier
2024-07-05 8:06 ` Paolo Bonzini
2024-07-05 18:52 ` Pierrick Bouvier
2024-07-05 21:08 ` Paolo Bonzini
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=ZvbY0PMWmJPZN7Sq@redhat.com \
--to=kwolf@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=berrange@redhat.com \
--cc=hreitz@redhat.com \
--cc=manos.pitsidianakis@linaro.org \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=pierrick.bouvier@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.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).