From: Paolo Bonzini <pbonzini@redhat.com>
To: Marc-Andre Lureau <marcandre.lureau@redhat.com>
Cc: "P. Berrange, Daniel" <berrange@redhat.com>,
"Sergio Lopez Pascual" <slp@redhat.com>,
"Hajnoczi, Stefan" <stefanha@gmail.com>,
qemu-devel <qemu-devel@nongnu.org>,
"Armbruster, Markus" <armbru@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"John Snow" <jsnow@redhat.com>
Subject: Re: [PATCH] PoC: Rust binding for QAPI (qemu-ga only, for now)
Date: Fri, 11 Sep 2020 12:24:44 +0200 [thread overview]
Message-ID: <eeead7ec-ebde-637d-de06-31c9b343b3b8@redhat.com> (raw)
In-Reply-To: <20200910174850.716104-1-marcandre.lureau@redhat.com>
On Thu, Sep 10, 2020 at 7:49 PM <marcandre.lureau@redhat.com> wrote:
> The usage is relatively simple:
>
> - from_qemu_none(ptr: *const sys::P) -> T
> Return a Rust type T for a const ffi pointer P.
>
> - from_qemu_full(ptr: *mut sys::P) -> T
> Return a Rust type T for a ffi pointer P, taking ownership.
>
> - T::to_qemu_none() -> Stash<P>
> Returns a borrowed ffi pointer P (using a Stash to destroy "glue"
> storage data, if any).
>
> - T::to_qemu_full() -> P
> Returns a ffi pointer P. (P resources are leaked/passed to C/ffi)
I know these come from glib-rs, but still the names are awful. :)
What about:
- an unsafe variant of From/Into for from_qemu_full:
trait UnsafeFrom<T> {
unsafe fn unsafe_from(_: T) -> Self;
}
trait UnsafeInto<T> {
unsafe fn unsafe_into(self) -> T;
}
impl <T, U> UnsafeInto<U> for T where U: UnsafeFrom<T> {
unsafe fn unsafe_into(self) -> U { U::unsafe_from(self) }
}
Example:
impl UnsafeFrom<*mut c_char> for String {
unsafe fn unsafe_from(ptr: *mut c_char) -> Self {
let res = Self::new_from_foreign(ptr);
libc::free(ptr as *mut c_void);
res
}
}
- likewise, a generic IntoRaw trait for to_qemu_full:
trait IntoRaw<T> {
fn into_raw(self) -> *mut T;
}
Example:
impl IntoRaw<c_char> for String {
fn into_raw(self) -> *mut c_char {
unsafe {
libc::strndup(self.as_ptr() as *const c_char,
self.len() as size_t)
}
}
}
- and a simpler/nicer version of Stash, from_qemu_none and to_qemu_none like this:
pub struct BorrowedPointer<'a, P, T: 'a> {
pub native: *const P,
pub storage: T,
_marker: PhantomData<&'a T>,
}
impl<'a, P: Copy, T: 'a> BorrowedPointer<'a, P, T> {
fn new(native: *const P, storage: T) -> Self {
BorrowedPointer {
native,
storage,
_marker: PhantomData
}
}
fn as_ptr(&self) -> *const P {
self.native
}
}
trait ForeignConvertible<'a> {
type Native: Copy;
type Storage: 'a;
unsafe fn new_from_foreign(p: *const Self::Native) -> Self;
fn as_foreign(&'a self) -> BorrowedPointer<'a, Self::Native, Self::Storage>;
}
Implemented like this:
impl ForeignConvertible<'_> for String {
type Native = c_char;
type Storage = CString;
unsafe fn new_from_foreign(p: *const c_char) -> Self {
let cstr = CStr::from_ptr(p);
String::from_utf8_lossy(cstr.to_bytes()).into_owned()
}
fn as_foreign(&self) -> BorrowedPointer<c_char, CString> {
let tmp = CString::new(&self[..]).unwrap();
BorrowedPointer::new(tmp.as_ptr(), tmp)
}
}
and possibly:
impl<'a, P: Copy, T: 'a> BorrowedMutPointer<'a, P, T> {
fn new(native: *mut P, storage: T) -> Self {
BorrowedMutPointer {
native,
storage,
_marker: PhantomData
}
}
fn as_ptr(&self) -> *const P {
self.native
}
fn as_mut_ptr(&mut self) -> *mut P {
self.native
}
}
trait ForeignMutConvertible<'a>: ForeignConvertible<'a> {
fn as_foreign_mut(&self) -> BorrowedMutPointer<Self::Native, Self::Storage>;
}
I placed the source code for the above at https://github.com/bonzini/rust-ptr
I'll look later at the rest of the code. It's quite big. :)
Paolo
next prev parent reply other threads:[~2020-09-11 10:27 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-10 17:48 [PATCH] PoC: Rust binding for QAPI (qemu-ga only, for now) marcandre.lureau
2020-09-11 10:24 ` Paolo Bonzini [this message]
2020-09-11 13:08 ` Paolo Bonzini
2020-09-11 14:00 ` Marc-André Lureau
2020-09-11 15:17 ` Paolo Bonzini
2020-09-29 17:55 ` Marc-André Lureau
2020-09-29 18:23 ` Paolo Bonzini
2020-09-30 9:15 ` Marc-André Lureau
2020-09-30 14:02 ` Paolo Bonzini
2020-09-11 10:46 ` Daniel P. Berrangé
2020-09-11 11:03 ` Marc-André Lureau
2020-09-11 11:28 ` Paolo Bonzini
2020-09-11 11:31 ` Daniel P. Berrangé
2020-09-11 14:19 ` John Snow
2020-09-11 14:17 ` Marc-André Lureau
2020-09-21 9:16 ` Markus Armbruster
2020-09-21 9:30 ` Paolo Bonzini
2020-09-22 14:59 ` Markus Armbruster
2020-09-21 10:04 ` Marc-André Lureau
2020-09-22 15:09 ` Markus Armbruster
2020-09-22 16:35 ` Marc-André Lureau
2020-09-22 17:08 ` Paolo Bonzini
2020-09-29 7:45 ` Marc-André Lureau
2020-09-29 10:14 ` Paolo Bonzini
2020-09-29 10:34 ` Marc-André Lureau
2020-09-29 11:00 ` Paolo Bonzini
2020-09-29 11:34 ` Marc-André Lureau
2020-09-30 7:34 ` Markus Armbruster
2020-09-30 7:51 ` Marc-André Lureau
2020-09-30 13:14 ` Paolo Bonzini
2020-09-22 16:52 ` Daniel P. Berrangé
2020-09-23 11:51 ` Markus Armbruster
2020-09-21 10:11 ` Marc-André Lureau
2020-09-22 15:37 ` Markus Armbruster
2020-09-22 16:25 ` Marc-André Lureau
2020-09-24 7:31 ` Markus Armbruster
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=eeead7ec-ebde-637d-de06-31c9b343b3b8@redhat.com \
--to=pbonzini@redhat.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=jsnow@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=slp@redhat.com \
--cc=stefanha@gmail.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).