From a1ff43045b3f1d075c67c262ff6f623b2b5d569e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sun, 15 Sep 2019 23:09:30 +0200 Subject: [PATCH] Move stuff around --- nix-rust/local.mk | 2 +- nix-rust/src/c.rs | 42 ++++++++++++ nix-rust/src/foreign.rs | 19 ++++++ nix-rust/src/lib.rs | 65 +------------------ .../src/{ => store}/binary_cache_store.rs | 3 +- nix-rust/src/store/mod.rs | 7 ++ nix-rust/src/{ => store}/path_info.rs | 0 nix-rust/src/{ => store}/store.rs | 2 +- nix-rust/src/util/mod.rs | 1 + nix-rust/src/{ => util}/tarfile.rs | 0 10 files changed, 74 insertions(+), 67 deletions(-) create mode 100644 nix-rust/src/c.rs rename nix-rust/src/{ => store}/binary_cache_store.rs (95%) create mode 100644 nix-rust/src/store/mod.rs rename nix-rust/src/{ => store}/path_info.rs (100%) rename nix-rust/src/{ => store}/store.rs (98%) create mode 100644 nix-rust/src/util/mod.rs rename nix-rust/src/{ => util}/tarfile.rs (100%) diff --git a/nix-rust/local.mk b/nix-rust/local.mk index cf1a5c5c5..1157f7894 100644 --- a/nix-rust/local.mk +++ b/nix-rust/local.mk @@ -18,7 +18,7 @@ libnixrust_LDFLAGS_USE += -Wl,-rpath,$(abspath $(d)/target/$(RUST_DIR)) libnixrust_LDFLAGS_USE_INSTALLED += -Wl,-rpath,$(libdir) endif -$(libnixrust_PATH): $(wildcard $(d)/src/*.rs) $(d)/Cargo.toml +$(libnixrust_PATH): $(call rwildcard, $(d)/src, *.rs) $(d)/Cargo.toml $(trace-gen) cd nix-rust && CARGO_HOME=$$(if [[ -d vendor ]]; then echo vendor; fi) \ $(libnixrust_BUILD_FLAGS) \ RUSTC_BOOTSTRAP=1 cargo build $(RUST_MODE) $$(if [[ -d vendor ]]; then echo --offline; fi) \ diff --git a/nix-rust/src/c.rs b/nix-rust/src/c.rs new file mode 100644 index 000000000..1abd34198 --- /dev/null +++ b/nix-rust/src/c.rs @@ -0,0 +1,42 @@ +use super::{foreign::{self, CBox}, error, util, store}; + +#[no_mangle] +pub extern "C" fn unpack_tarfile( + source: foreign::Source, + dest_dir: &str, +) -> CBox> { + CBox::new(util::tarfile::unpack_tarfile(source, dest_dir).map_err(|err| err.into())) +} + +#[no_mangle] +pub extern "C" fn rust_test() { + use crate::store::Store; + use futures::future::{FutureExt, TryFutureExt}; + use std::path::Path; + + let fut = async move { + let store: Box = Box::new(store::BinaryCacheStore::new( + "https://cache.nixos.org".to_string(), + )); + + let path = store + .parse_store_path(&Path::new( + "/nix/store/7h7qgvs4kgzsn8a6rb273saxyqh4jxlz-konsole-18.12.3", + )) + .unwrap(); + + /* + let info = store.query_path_info(&path).await.unwrap(); + + eprintln!("INFO = {:?}", info); + */ + + let closure = store.compute_path_closure(vec![path].into_iter().collect()).await.unwrap(); + + eprintln!("CLOSURE = {:?}", closure.len()); + + Ok(()) + }; + + tokio::run(fut.boxed().compat()); +} diff --git a/nix-rust/src/foreign.rs b/nix-rust/src/foreign.rs index 7bce7753c..8e04280f3 100644 --- a/nix-rust/src/foreign.rs +++ b/nix-rust/src/foreign.rs @@ -12,3 +12,22 @@ 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/nix-rust/src/lib.rs b/nix-rust/src/lib.rs index 7445e005c..2dbe6a217 100644 --- a/nix-rust/src/lib.rs +++ b/nix-rust/src/lib.rs @@ -1,70 +1,9 @@ #![feature(await_macro, async_await)] -mod binary_cache_store; +mod c; mod error; mod foreign; mod store; -mod tarfile; -mod path_info; +mod util; pub use error::Error; - -pub struct CBox { - pub ptr: *mut libc::c_void, - phantom: std::marker::PhantomData, -} - -impl CBox { - 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, - } - } - } -} - -#[no_mangle] -pub extern "C" fn unpack_tarfile( - source: foreign::Source, - dest_dir: &str, -) -> CBox> { - CBox::new(tarfile::unpack_tarfile(source, dest_dir).map_err(|err| err.into())) -} - -#[no_mangle] -pub extern "C" fn rust_test() { - use crate::store::Store; - use futures::future::{FutureExt, TryFutureExt}; - use std::path::Path; - - let fut = async move { - let store: Box = Box::new(binary_cache_store::BinaryCacheStore::new( - "https://cache.nixos.org".to_string(), - )); - - let path = store - .parse_store_path(&Path::new( - "/nix/store/7h7qgvs4kgzsn8a6rb273saxyqh4jxlz-konsole-18.12.3", - )) - .unwrap(); - - /* - let info = store.query_path_info(&path).await.unwrap(); - - eprintln!("INFO = {:?}", info); - */ - - let closure = store.compute_path_closure(vec![path].into_iter().collect()).await.unwrap(); - - eprintln!("CLOSURE = {:?}", closure.len()); - - Ok(()) - }; - - tokio::run(fut.boxed().compat()); -} diff --git a/nix-rust/src/binary_cache_store.rs b/nix-rust/src/store/binary_cache_store.rs similarity index 95% rename from nix-rust/src/binary_cache_store.rs rename to nix-rust/src/store/binary_cache_store.rs index b7e61eb2d..f3c7b39bb 100644 --- a/nix-rust/src/binary_cache_store.rs +++ b/nix-rust/src/store/binary_cache_store.rs @@ -1,5 +1,4 @@ -use crate::store::{Store, StorePath}; -use crate::path_info::PathInfo; +use super::{Store, StorePath, PathInfo}; use crate::Error; use futures::compat::Future01CompatExt; diff --git a/nix-rust/src/store/mod.rs b/nix-rust/src/store/mod.rs new file mode 100644 index 000000000..a0d1c72ab --- /dev/null +++ b/nix-rust/src/store/mod.rs @@ -0,0 +1,7 @@ +mod binary_cache_store; +mod path_info; +mod store; + +pub use binary_cache_store::BinaryCacheStore; +pub use path_info::PathInfo; +pub use store::{Store, StorePath}; diff --git a/nix-rust/src/path_info.rs b/nix-rust/src/store/path_info.rs similarity index 100% rename from nix-rust/src/path_info.rs rename to nix-rust/src/store/path_info.rs diff --git a/nix-rust/src/store.rs b/nix-rust/src/store/store.rs similarity index 98% rename from nix-rust/src/store.rs rename to nix-rust/src/store/store.rs index 256dcf0f3..ceaed6648 100644 --- a/nix-rust/src/store.rs +++ b/nix-rust/src/store/store.rs @@ -1,4 +1,4 @@ -use crate::path_info::PathInfo; +use super::PathInfo; use crate::Error; use std::collections::{BTreeMap, BTreeSet}; use std::path::Path; diff --git a/nix-rust/src/util/mod.rs b/nix-rust/src/util/mod.rs new file mode 100644 index 000000000..209627893 --- /dev/null +++ b/nix-rust/src/util/mod.rs @@ -0,0 +1 @@ +pub mod tarfile; diff --git a/nix-rust/src/tarfile.rs b/nix-rust/src/util/tarfile.rs similarity index 100% rename from nix-rust/src/tarfile.rs rename to nix-rust/src/util/tarfile.rs