Skip to content

Commit

Permalink
Make the Store API more type-safe
Browse files Browse the repository at this point in the history
Most functions now take a StorePath argument rather than a Path (which
is just an alias for std::string). The StorePath constructor ensures
that the path is syntactically correct (i.e. it looks like
<store-dir>/<base32-hash>-<name>). Similarly, functions like
buildPaths() now take a StorePathWithOutputs, rather than abusing Path
by adding a '!<outputs>' suffix.

Note that the StorePath type is implemented in Rust. This involves
some hackery to allow Rust values to be used directly in C++, via a
helper type whose destructor calls the Rust type's drop()
function. The main issue is the dynamic nature of C++ move semantics:
after we have moved a Rust value, we should not call the drop function
on the original value. So when we move a value, we set the original
value to bitwise zero, and the destructor only calls drop() if the
value is not bitwise zero. This should be sufficient for most types.

Also lots of minor cleanups to the C++ API to make it more modern
(e.g. using std::optional and std::string_view in some places).
  • Loading branch information
edolstra committed Dec 10, 2019
1 parent ebd8999 commit bbe97df
Show file tree
Hide file tree
Showing 98 changed files with 2,625 additions and 2,867 deletions.
760 changes: 0 additions & 760 deletions nix-rust/Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions nix-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ crate-type = ["cdylib"]
[dependencies]
tar = "0.4"
libc = "0.2"
futures-preview = { version = "=0.3.0-alpha.19" }
hyper = "0.13.0-alpha.4"
http = "0.1"
tokio = { version = "0.2.0-alpha.6", default-features = false, features = ["rt-full"] }
#futures-preview = { version = "=0.3.0-alpha.19" }

This comment has been minimized.

Copy link
@arianvp

arianvp Feb 4, 2020

Member

Euhm; was comitting these as commented intended? As far as I can see the code still uses all these dependencies, no?

#hyper = "0.13.0-alpha.4"
#http = "0.1"
#tokio = { version = "0.2.0-alpha.6", default-features = false, features = ["rt-full"] }
lazy_static = "1.4"
byteorder = "1.3"
#byteorder = "1.3"

[dev-dependencies]
hex = "0.3"
Expand Down
4 changes: 3 additions & 1 deletion nix-rust/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ clean: clean-rust
clean-rust:
$(suppress) rm -rfv nix-rust/target

ifneq ($(OS), Darwin)
check: rust-tests

rust-tests:
cd nix-rust && CARGO_HOME=$$(if [[ -d vendor ]]; then echo vendor; fi) RUSTC_BOOTSTRAP=1 cargo test --release $$(if [[ -d vendor ]]; then echo -Z offline; fi)
cd nix-rust && CARGO_HOME=$$(if [[ -d vendor ]]; then echo vendor; fi) cargo test --release $$(if [[ -d vendor ]]; then echo --offline; fi)
endif
88 changes: 59 additions & 29 deletions nix-rust/src/c.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{
error,
foreign::{self, CBox},
store::StorePath,
util,
};

Expand All @@ -13,43 +14,72 @@ pub extern "C" fn unpack_tarfile(
}

#[no_mangle]
pub extern "C" fn rust_test() {
use crate::store::{self, Store};
use std::path::Path;
use tokio::runtime::Runtime;
pub unsafe extern "C" fn ffi_String_new(s: &str, out: *mut String) {
// FIXME: check whether 's' is valid UTF-8?
out.write(s.to_string())
}

let fut = async move {
let store: Box<dyn Store> = Box::new(store::BinaryCacheStore::new(
"http:https://cache.nixos.org".to_string(),
));
#[no_mangle]
pub unsafe extern "C" fn ffi_String_drop(self_: *mut String) {
std::ptr::drop_in_place(self_);
}

let path = store
.parse_store_path(&Path::new(
"/nix/store/7h7qgvs4kgzsn8a6rb273saxyqh4jxlz-konsole-18.12.3",
))
.unwrap();
#[no_mangle]
pub extern "C" fn ffi_StorePath_new(
path: &str,
store_dir: &str,
) -> Result<StorePath, error::CppException> {
StorePath::new(std::path::Path::new(path), store_dir).map_err(|err| err.into())
}

/*
let info = store.query_path_info(&path).await.unwrap();
#[no_mangle]
pub extern "C" fn ffi_StorePath_new2(
hash: &[u8; crate::store::path::STORE_PATH_HASH_BYTES],
name: &str,
) -> Result<StorePath, error::CppException> {
StorePath::from_parts(*hash, name).map_err(|err| err.into())
}

eprintln!("INFO = {:?}", info);
*/
#[no_mangle]
pub extern "C" fn ffi_StorePath_fromBaseName(
base_name: &str,
) -> Result<StorePath, error::CppException> {
StorePath::new_from_base_name(base_name).map_err(|err| err.into())
}

let closure = store
.compute_path_closure(vec![path].into_iter().collect())
.await
.unwrap();
#[no_mangle]
pub unsafe extern "C" fn ffi_StorePath_drop(self_: *mut StorePath) {
std::ptr::drop_in_place(self_);
}

eprintln!("CLOSURE = {:?}", closure.len());
};
#[no_mangle]
pub extern "C" fn ffi_StorePath_to_string(self_: &StorePath) -> String {
format!("{}", self_)
}

let rt = Runtime::new().unwrap();
#[no_mangle]
pub extern "C" fn ffi_StorePath_less_than(a: &StorePath, b: &StorePath) -> bool {
a < b
}

rt.block_on(fut);
#[no_mangle]
pub extern "C" fn ffi_StorePath_eq(a: &StorePath, b: &StorePath) -> bool {
a == b
}

/*
let file = std::fs::File::open("test.nar").unwrap();
#[no_mangle]
pub extern "C" fn ffi_StorePath_clone(self_: &StorePath) -> StorePath {
self_.clone()
}

crate::nar::parse(&mut std::io::BufReader::new(file)).unwrap();
*/
#[no_mangle]
pub extern "C" fn ffi_StorePath_name(self_: &StorePath) -> &str {
self_.name.name()
}

#[no_mangle]
pub extern "C" fn ffi_StorePath_hash_data(
self_: &StorePath,
) -> &[u8; crate::store::path::STORE_PATH_HASH_BYTES] {
self_.hash.hash()
}
3 changes: 3 additions & 0 deletions nix-rust/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum Error {
BadNarField(String),
BadExecutableField,
IOError(std::io::Error),
#[cfg(unused)]
HttpError(hyper::error::Error),
Misc(String),
Foreign(CppException),
Expand All @@ -30,6 +31,7 @@ impl From<std::io::Error> for Error {
}
}

#[cfg(unused)]
impl From<hyper::error::Error> for Error {
fn from(err: hyper::error::Error) -> Self {
Error::HttpError(err)
Expand Down Expand Up @@ -58,6 +60,7 @@ impl fmt::Display for Error {
Error::BadNarField(s) => write!(f, "unrecognized NAR field '{}'", s),
Error::BadExecutableField => write!(f, "bad 'executable' field in NAR"),
Error::IOError(err) => write!(f, "I/O error: {}", err),
#[cfg(unused)]
Error::HttpError(err) => write!(f, "HTTP error: {}", err),
Error::Foreign(_) => write!(f, "<C++ exception>"), // FIXME
Error::Misc(s) => write!(f, "{}", s),
Expand Down
1 change: 1 addition & 0 deletions nix-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern crate proptest;
mod c;
mod error;
mod foreign;
#[cfg(unused)]
mod nar;
mod store;
mod util;
Expand Down
12 changes: 10 additions & 2 deletions nix-rust/src/store/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
pub mod path;

#[cfg(unused)]
mod binary_cache_store;
mod path;
#[cfg(unused)]
mod path_info;
#[cfg(unused)]
mod store;

pub use binary_cache_store::BinaryCacheStore;
pub use path::{StorePath, StorePathHash, StorePathName};

#[cfg(unused)]
pub use binary_cache_store::BinaryCacheStore;
#[cfg(unused)]
pub use path_info::PathInfo;
#[cfg(unused)]
pub use store::Store;
32 changes: 31 additions & 1 deletion nix-rust/src/store/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ impl StorePath {
)
}

pub fn from_parts(hash: [u8; STORE_PATH_HASH_BYTES], name: &str) -> Result<Self, Error> {
Ok(StorePath {
hash: StorePathHash(hash),
name: StorePathName::new(name)?,
})
}

pub fn new_from_base_name(base_name: &str) -> Result<Self, Error> {
if base_name.len() < STORE_PATH_HASH_CHARS + 1
|| base_name.as_bytes()[STORE_PATH_HASH_CHARS] != '-' as u8
Expand All @@ -43,7 +50,7 @@ impl fmt::Display for StorePath {
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct StorePathHash([u8; STORE_PATH_HASH_BYTES]);

impl StorePathHash {
Expand All @@ -55,6 +62,10 @@ impl StorePathHash {
bytes.copy_from_slice(&v[0..STORE_PATH_HASH_BYTES]);
Ok(Self(bytes))
}

pub fn hash<'a>(&'a self) -> &'a [u8; STORE_PATH_HASH_BYTES] {
&self.0
}
}

impl fmt::Display for StorePathHash {
Expand All @@ -63,6 +74,21 @@ impl fmt::Display for StorePathHash {
}
}

impl Ord for StorePathHash {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// Historically we've sorted store paths by their base32
// serialization, but our base32 encodes bytes in reverse
// order. So compare them in reverse order as well.
self.0.iter().rev().cmp(other.0.iter().rev())
}
}

impl PartialOrd for StorePathHash {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct StorePathName(String);

Expand Down Expand Up @@ -93,6 +119,10 @@ impl StorePathName {

Ok(Self(s.to_string()))
}

pub fn name<'a>(&'a self) -> &'a str {
&self.0
}
}

impl fmt::Display for StorePathName {
Expand Down
Loading

0 comments on commit bbe97df

Please sign in to comment.