From ca87707c90c05289d0c7c1015f5750f6dd93708b Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 13 Dec 2019 18:29:16 +0100 Subject: [PATCH] Get rid of CBox --- nix-rust/src/c.rs | 11 ++++++----- nix-rust/src/foreign.rs | 19 ------------------- src/libutil/rust-ffi.hh | 20 -------------------- src/libutil/tarfile.cc | 6 ++++-- 4 files changed, 10 insertions(+), 46 deletions(-) diff --git a/nix-rust/src/c.rs b/nix-rust/src/c.rs index 3feeb71a3..e0462742f 100644 --- a/nix-rust/src/c.rs +++ b/nix-rust/src/c.rs @@ -1,20 +1,21 @@ use super::{ error, - foreign::{self, CBox}, + foreign::{self}, store::path, store::StorePath, util, }; #[no_mangle] -pub extern "C" fn unpack_tarfile( +pub unsafe extern "C" fn unpack_tarfile( source: foreign::Source, dest_dir: &str, -) -> CBox> { - CBox::new( + out: *mut Result<(), error::CppException>, +) { + out.write( util::tarfile::unpack_tarfile(source, std::path::Path::new(dest_dir)) .map_err(|err| err.into()), - ) + ); } #[no_mangle] diff --git a/nix-rust/src/foreign.rs b/nix-rust/src/foreign.rs index 8e04280f3..7bce7753c 100644 --- a/nix-rust/src/foreign.rs +++ b/nix-rust/src/foreign.rs @@ -12,22 +12,3 @@ impl std::io::Read for Source { Ok(n) } } - -pub struct CBox { - pub ptr: *mut libc::c_void, - phantom: std::marker::PhantomData, -} - -impl CBox { - pub fn new(t: T) -> Self { - unsafe { - let size = std::mem::size_of::(); - let ptr = libc::malloc(size); - *(ptr as *mut T) = t; // FIXME: probably UB - Self { - ptr, - phantom: std::marker::PhantomData, - } - } - } -} diff --git a/src/libutil/rust-ffi.hh b/src/libutil/rust-ffi.hh index a77f83ac5..1466eabec 100644 --- a/src/libutil/rust-ffi.hh +++ b/src/libutil/rust-ffi.hh @@ -180,24 +180,4 @@ struct Result } }; -template -struct CBox -{ - T * ptr; - - T * operator ->() - { - return ptr; - } - - CBox(T * ptr) : ptr(ptr) { } - CBox(const CBox &) = delete; - CBox(CBox &&) = delete; - - ~CBox() - { - free(ptr); - } -}; - } diff --git a/src/libutil/tarfile.cc b/src/libutil/tarfile.cc index 79fb9f721..1be0ba24c 100644 --- a/src/libutil/tarfile.cc +++ b/src/libutil/tarfile.cc @@ -3,7 +3,7 @@ extern "C" { rust::Result> * - unpack_tarfile(rust::Source source, rust::StringSlice dest_dir); + unpack_tarfile(rust::Source source, rust::StringSlice dest_dir, rust::Result> & out); } namespace nix { @@ -11,7 +11,9 @@ namespace nix { void unpackTarfile(Source & source, const Path & destDir) { rust::Source source2(source); - rust::CBox(unpack_tarfile(source2, destDir))->unwrap(); + rust::Result> res; + unpack_tarfile(source2, destDir, res); + res.unwrap(); } void unpackTarfile(const Path & tarFile, const Path & destDir,