Get rid of CBox

This commit is contained in:
Eelco Dolstra 2019-12-13 18:29:16 +01:00
parent 5a6d6da7ae
commit ca87707c90
4 changed files with 10 additions and 46 deletions

View file

@ -1,20 +1,21 @@
use super::{ use super::{
error, error,
foreign::{self, CBox}, foreign::{self},
store::path, store::path,
store::StorePath, store::StorePath,
util, util,
}; };
#[no_mangle] #[no_mangle]
pub extern "C" fn unpack_tarfile( pub unsafe extern "C" fn unpack_tarfile(
source: foreign::Source, source: foreign::Source,
dest_dir: &str, dest_dir: &str,
) -> CBox<Result<(), error::CppException>> { out: *mut Result<(), error::CppException>,
CBox::new( ) {
out.write(
util::tarfile::unpack_tarfile(source, std::path::Path::new(dest_dir)) util::tarfile::unpack_tarfile(source, std::path::Path::new(dest_dir))
.map_err(|err| err.into()), .map_err(|err| err.into()),
) );
} }
#[no_mangle] #[no_mangle]

View file

@ -12,22 +12,3 @@ impl std::io::Read for Source {
Ok(n) Ok(n)
} }
} }
pub struct CBox<T> {
pub ptr: *mut libc::c_void,
phantom: std::marker::PhantomData<T>,
}
impl<T> CBox<T> {
pub fn new(t: T) -> Self {
unsafe {
let size = std::mem::size_of::<T>();
let ptr = libc::malloc(size);
*(ptr as *mut T) = t; // FIXME: probably UB
Self {
ptr,
phantom: std::marker::PhantomData,
}
}
}
}

View file

@ -180,24 +180,4 @@ struct Result
} }
}; };
template<typename T>
struct CBox
{
T * ptr;
T * operator ->()
{
return ptr;
}
CBox(T * ptr) : ptr(ptr) { }
CBox(const CBox &) = delete;
CBox(CBox &&) = delete;
~CBox()
{
free(ptr);
}
};
} }

View file

@ -3,7 +3,7 @@
extern "C" { extern "C" {
rust::Result<std::tuple<>> * rust::Result<std::tuple<>> *
unpack_tarfile(rust::Source source, rust::StringSlice dest_dir); unpack_tarfile(rust::Source source, rust::StringSlice dest_dir, rust::Result<std::tuple<>> & out);
} }
namespace nix { namespace nix {
@ -11,7 +11,9 @@ namespace nix {
void unpackTarfile(Source & source, const Path & destDir) void unpackTarfile(Source & source, const Path & destDir)
{ {
rust::Source source2(source); rust::Source source2(source);
rust::CBox(unpack_tarfile(source2, destDir))->unwrap(); rust::Result<std::tuple<>> res;
unpack_tarfile(source2, destDir, res);
res.unwrap();
} }
void unpackTarfile(const Path & tarFile, const Path & destDir, void unpackTarfile(const Path & tarFile, const Path & destDir,