aboutsummaryrefslogtreecommitdiff
path: root/nix-rust/src/store
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-12-05 19:11:09 +0100
committerEelco Dolstra <edolstra@gmail.com>2019-12-10 22:06:05 +0100
commitbbe97dff8b3054d96e758f486f9ce3fa09e64de3 (patch)
tree9dd575bc61ec93de4bc693f00a84fc8686a613f9 /nix-rust/src/store
parentebd89999c28e25e71048d523f9c4f8b856dee9a9 (diff)
Make the Store API more type-safe
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).
Diffstat (limited to 'nix-rust/src/store')
-rw-r--r--nix-rust/src/store/mod.rs12
-rw-r--r--nix-rust/src/store/path.rs32
2 files changed, 41 insertions, 3 deletions
diff --git a/nix-rust/src/store/mod.rs b/nix-rust/src/store/mod.rs
index 85355b594..da972482c 100644
--- a/nix-rust/src/store/mod.rs
+++ b/nix-rust/src/store/mod.rs
@@ -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;
diff --git a/nix-rust/src/store/path.rs b/nix-rust/src/store/path.rs
index 03307ca7c..3ded5c428 100644
--- a/nix-rust/src/store/path.rs
+++ b/nix-rust/src/store/path.rs
@@ -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
@@ -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 {
@@ -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 {
@@ -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);
@@ -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 {