Skip to content

Commit

Permalink
Merge pull request #3892 from cgwalters/more-pin-mut-2
Browse files Browse the repository at this point in the history
Remove last uses of `Pin<&mut someglibtype>`
  • Loading branch information
Luca Bruno authored Aug 1, 2022
2 parents 1517794 + 4c6aa53 commit bfa1fc2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 47 deletions.
10 changes: 5 additions & 5 deletions rpmostree-cxxrs.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ struct Bubblewrap final : public ::rust::Opaque
void bind_read (::rust::Str src, ::rust::Str dest) noexcept;
void bind_readwrite (::rust::Str src, ::rust::Str dest) noexcept;
void setup_compat_var ();
void run (::rpmostreecxx::GCancellable &cancellable);
void run (const ::rpmostreecxx::GCancellable &cancellable);
~Bubblewrap () = delete;

private:
Expand Down Expand Up @@ -2011,7 +2011,7 @@ extern "C"

::rust::repr::PtrLen
rpmostreecxx$cxxbridge1$Bubblewrap$run (::rpmostreecxx::Bubblewrap &self,
::rpmostreecxx::GCancellable &cancellable) noexcept;
const ::rpmostreecxx::GCancellable &cancellable) noexcept;

::rust::repr::PtrLen
rpmostreecxx$cxxbridge1$applylive_entrypoint (const ::rust::Vec< ::rust::String> &args) noexcept;
Expand Down Expand Up @@ -2125,7 +2125,7 @@ extern "C"

::rust::repr::PtrLen
rpmostreecxx$cxxbridge1$directory_size (::std::int32_t dfd,
::rpmostreecxx::GCancellable &cancellable,
const ::rpmostreecxx::GCancellable &cancellable,
::std::uint64_t *return$) noexcept;

::rust::repr::PtrLen
Expand Down Expand Up @@ -3483,7 +3483,7 @@ Bubblewrap::setup_compat_var ()
}

void
Bubblewrap::run (::rpmostreecxx::GCancellable &cancellable)
Bubblewrap::run (const ::rpmostreecxx::GCancellable &cancellable)
{
::rust::repr::PtrLen error$ = rpmostreecxx$cxxbridge1$Bubblewrap$run (*this, cancellable);
if (error$.ptr)
Expand Down Expand Up @@ -3868,7 +3868,7 @@ rewrite_rpmdb_for_target (::std::int32_t rootfs_dfd, bool normalize)
}

::std::uint64_t
directory_size (::std::int32_t dfd, ::rpmostreecxx::GCancellable &cancellable)
directory_size (::std::int32_t dfd, const ::rpmostreecxx::GCancellable &cancellable)
{
::rust::MaybeUninit< ::std::uint64_t> return$;
::rust::repr::PtrLen error$
Expand Down
5 changes: 3 additions & 2 deletions rpmostree-cxxrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ struct Bubblewrap final : public ::rust::Opaque
void bind_read (::rust::Str src, ::rust::Str dest) noexcept;
void bind_readwrite (::rust::Str src, ::rust::Str dest) noexcept;
void setup_compat_var ();
void run (::rpmostreecxx::GCancellable &cancellable);
void run (const ::rpmostreecxx::GCancellable &cancellable);
~Bubblewrap () = delete;

private:
Expand Down Expand Up @@ -1804,7 +1804,8 @@ void postprocess_cleanup_rpmdb (::std::int32_t rootfs_dfd);

void rewrite_rpmdb_for_target (::std::int32_t rootfs_dfd, bool normalize);

::std::uint64_t directory_size (::std::int32_t dfd, ::rpmostreecxx::GCancellable &cancellable);
::std::uint64_t directory_size (::std::int32_t dfd,
const ::rpmostreecxx::GCancellable &cancellable);

::rpmostreecxx::OstreeDeployment *deployment_for_id (::rpmostreecxx::OstreeSysroot &sysroot,
::rust::Str deploy_id);
Expand Down
8 changes: 2 additions & 6 deletions rust/src/bwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use ostree_ext::{gio, glib};
use std::num::NonZeroUsize;
use std::os::unix::io::AsRawFd;
use std::path::Path;
use std::pin::Pin;
use std::process::Command;

// Links in the rootfs to /usr
Expand Down Expand Up @@ -430,11 +429,8 @@ impl Bubblewrap {
}

/// Execute the instance; requires a cancellable for C++.
pub(crate) fn run(
&mut self,
mut cancellable: Pin<&mut crate::FFIGCancellable>,
) -> CxxResult<()> {
let cancellable = &cancellable.gobj_wrap();
pub(crate) fn run(&mut self, cancellable: &crate::FFIGCancellable) -> CxxResult<()> {
let cancellable = &cancellable.glib_reborrow();
self.run_inner(Some(cancellable))?;
Ok(())
}
Expand Down
13 changes: 5 additions & 8 deletions rust/src/composepost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,11 +999,8 @@ pub fn prepare_rpmdb_base_location(

/// Recurse into this directory and return the total size of all regular files.
#[context("Computing directory size")]
pub fn directory_size(
dfd: i32,
mut cancellable: Pin<&mut crate::FFIGCancellable>,
) -> CxxResult<u64> {
let cancellable = &cancellable.gobj_wrap();
pub fn directory_size(dfd: i32, cancellable: &crate::FFIGCancellable) -> CxxResult<u64> {
let cancellable = cancellable.glib_reborrow();
let dfd = crate::ffiutil::ffi_view_openat_dir(dfd);
fn directory_size_recurse(d: &openat::Dir, cancellable: &gio::Cancellable) -> Result<u64> {
let mut r = 0;
Expand All @@ -1026,7 +1023,7 @@ pub fn directory_size(
}
Ok(r)
}
Ok(directory_size_recurse(&dfd, cancellable)?)
Ok(directory_size_recurse(&dfd, &cancellable)?)
}

#[context("Hardlinking rpmdb to base location")]
Expand Down Expand Up @@ -1101,7 +1098,7 @@ fn rewrite_rpmdb_for_target_inner(rootfs_dfd: &openat::Dir, normalize: bool) ->
bwrap.take_stdin_fd(dbfd.into_raw_fd());
let cancellable = gio::Cancellable::new();
bwrap
.run(cancellable.gobj_rewrap())
.run(cancellable.reborrow_cxx())
.context("Failed to run rpmdb --importdb")?;

// Sometimes we can end up with build-to-build variance in the underlying rpmdb
Expand Down Expand Up @@ -1358,7 +1355,7 @@ OSTREE_VERSION='33.4'
// Also make this a sanity test for our directory size API
let cancellable = gio::Cancellable::new();
assert_eq!(
directory_size(rootfs.as_raw_fd(), cancellable.gobj_rewrap()).unwrap(),
directory_size(rootfs.as_raw_fd(), cancellable.reborrow_cxx()).unwrap(),
expected_disk_size
);

Expand Down
24 changes: 0 additions & 24 deletions rust/src/cxxrsutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use cxx::{type_id, ExternType};
use glib::translate::ToGlibPtr;
use ostree_ext::{gio, glib, ostree};
use paste::paste;
use std::pin::Pin;

/// Map an empty string to a `None`.
pub(crate) fn opt_string(input: &str) -> Option<&str> {
Expand All @@ -39,11 +38,6 @@ pub trait FFIGObjectWrapper {
pub trait FFIGObjectReWrap<'a> {
type ReWrapped;

/// Convert a glib-rs wrapper object into a Pin pointer
/// to our FFI newtype. This is necessary to call
/// cxx-rs wrapped functions from Rust.
fn gobj_rewrap(&'a self) -> Pin<&'a mut Self::ReWrapped>;

/// Convert a glib-rs wrapper object borrowed cxx-rs type.
fn reborrow_cxx(&'a self) -> &Self::ReWrapped;
}
Expand All @@ -64,24 +58,6 @@ macro_rules! impl_wrap {
}
impl<'a> FFIGObjectReWrap<'a> for $bound {
type ReWrapped = $w;
fn gobj_rewrap(&'a self) -> Pin<&'a mut Self::ReWrapped> {
// Access the underlying raw pointer behind the glib-rs
// newtype wrapper, e.g. `ostree_sys::OstreeRepo`.
let p: *const $sys = self.to_glib_none().0;
// Safety: Pin<T> is a #[repr(transparent)] newtype wrapper
// around our #[repr(transparent)] FFI newtype wrapper which
// for the glib-rs newtype wrapper, which finally holds the real
// raw pointer. Phew!
// In other words: Pin(FFINewType(GlibRs(RawPointer)))
// Here we're just powering through those layers of wrappers to
// convert the raw pointer. See also https://internals.rust-lang.org/t/pre-rfc-v2-safe-transmute/11431
//
// However, since what we're handing out is a raw pointer,
// we ensure that the lifetime of our return value is tied to
// that of the glib-rs wrapper (which holds a GObject strong reference),
// which ensures the value isn't freed.
unsafe { std::mem::transmute(p) }
}

fn reborrow_cxx(&'a self) -> &Self::ReWrapped {
let p: *const $sys = self.to_glib_none().0;
Expand Down
4 changes: 2 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub mod ffi {
fn bind_readwrite(&mut self, src: &str, dest: &str);
fn setup_compat_var(&mut self) -> Result<()>;

fn run(&mut self, cancellable: Pin<&mut GCancellable>) -> Result<()>;
fn run(&mut self, cancellable: &GCancellable) -> Result<()>;
}

// builtins/apply_live.rs
Expand Down Expand Up @@ -256,7 +256,7 @@ pub mod ffi {
fn compose_postprocess_rpm_macro(rootfs_dfd: i32) -> Result<()>;
fn postprocess_cleanup_rpmdb(rootfs_dfd: i32) -> Result<()>;
fn rewrite_rpmdb_for_target(rootfs_dfd: i32, normalize: bool) -> Result<()>;
fn directory_size(dfd: i32, mut cancellable: Pin<&mut GCancellable>) -> Result<u64>;
fn directory_size(dfd: i32, cancellable: &GCancellable) -> Result<u64>;
}

// container.cxx
Expand Down

0 comments on commit bfa1fc2

Please sign in to comment.