From 1b56de8cd1dfc47729bddbafb023819086296e2e Mon Sep 17 00:00:00 2001 From: Leonhard Markert Date: Mon, 10 Feb 2020 09:01:43 +0100 Subject: [PATCH 1/3] Remove macro_use As of Rust 2018, macro_use is no longer required in most circumstances. I think it is generally a good idea to remove these when not needed, to stop them from polluting the crate's global namespace. https://doc.rust-lang.org/edition-guide/rust-2018/macros/macro-changes.html#macro_rules-style-macros --- nix-rust/src/lib.rs | 11 ----------- nix-rust/src/store/path.rs | 1 + nix-rust/src/util/base32.rs | 3 +++ 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/nix-rust/src/lib.rs b/nix-rust/src/lib.rs index e62613ba8..27ea69fbd 100644 --- a/nix-rust/src/lib.rs +++ b/nix-rust/src/lib.rs @@ -1,14 +1,3 @@ -#[macro_use] -extern crate lazy_static; - -#[cfg(test)] -#[macro_use] -extern crate assert_matches; - -#[cfg(test)] -#[macro_use] -extern crate proptest; - #[cfg(not(test))] mod c; mod error; diff --git a/nix-rust/src/store/path.rs b/nix-rust/src/store/path.rs index 2a5170bef..47b5975c0 100644 --- a/nix-rust/src/store/path.rs +++ b/nix-rust/src/store/path.rs @@ -138,6 +138,7 @@ impl fmt::Display for StorePathName { #[cfg(test)] mod tests { use super::*; + use assert_matches::assert_matches; #[test] fn test_parse() { diff --git a/nix-rust/src/util/base32.rs b/nix-rust/src/util/base32.rs index ba7368933..efd4a2901 100644 --- a/nix-rust/src/util/base32.rs +++ b/nix-rust/src/util/base32.rs @@ -1,4 +1,5 @@ use crate::error::Error; +use lazy_static::lazy_static; pub fn encoded_len(input_len: usize) -> usize { if input_len == 0 { @@ -87,7 +88,9 @@ pub fn decode(input: &str) -> Result, crate::Error> { #[cfg(test)] mod tests { use super::*; + use assert_matches::assert_matches; use hex; + use proptest::proptest; #[test] fn test_encode() { From d8972317fc4314864619cadd5620ae780da657a3 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 13 Feb 2020 16:12:16 +0100 Subject: [PATCH 2/3] Prevent uninitialized StorePath creation --- src/libstore/path.hh | 2 ++ src/libutil/rust-ffi.hh | 2 ++ src/nix/why-depends.cc | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libstore/path.hh b/src/libstore/path.hh index c31ea2179..c90bb1fff 100644 --- a/src/libstore/path.hh +++ b/src/libstore/path.hh @@ -18,6 +18,8 @@ extern "C" { struct StorePath : rust::Value<3 * sizeof(void *) + 24, ffi_StorePath_drop> { + StorePath() = delete; + static StorePath make(std::string_view path, std::string_view storeDir); static StorePath make(unsigned char hash[20], std::string_view name); diff --git a/src/libutil/rust-ffi.hh b/src/libutil/rust-ffi.hh index 469a5fba3..228e2eead 100644 --- a/src/libutil/rust-ffi.hh +++ b/src/libutil/rust-ffi.hh @@ -113,6 +113,8 @@ extern "C" { struct String : Vec { + String() = delete; + String(std::string_view s) { ffi_String_new(StringSlice(s), this); diff --git a/src/nix/why-depends.cc b/src/nix/why-depends.cc index 8566e5851..c24ae7c8e 100644 --- a/src/nix/why-depends.cc +++ b/src/nix/why-depends.cc @@ -103,7 +103,7 @@ struct CmdWhyDepends : SourceExprCommand std::map graph; for (auto & path : closure) - graph.emplace(path.clone(), Node{path.clone(), cloneStorePathSet(store->queryPathInfo(path)->references)}); + graph.emplace(path.clone(), Node { .path = path.clone(), .refs = cloneStorePathSet(store->queryPathInfo(path)->references) }); // Transpose the graph. for (auto & node : graph) @@ -112,7 +112,7 @@ struct CmdWhyDepends : SourceExprCommand /* Run Dijkstra's shortest path algorithm to get the distance of every path in the closure to 'dependency'. */ - graph[dependencyPath.clone()].dist = 0; + graph.emplace(dependencyPath.clone(), Node { .path = dependencyPath.clone(), .dist = 0 }); std::priority_queue queue; From 9af10b753c8a636f828b148fc3a9aecd1c0067fa Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 13 Feb 2020 17:15:05 +0100 Subject: [PATCH 3/3] Bindings::get(): std::optional -> Attr * Returning a nullable type in an optional is silly. --- src/libexpr/attr-set.hh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libexpr/attr-set.hh b/src/libexpr/attr-set.hh index d6af99912..118c7bd5d 100644 --- a/src/libexpr/attr-set.hh +++ b/src/libexpr/attr-set.hh @@ -64,12 +64,12 @@ public: return end(); } - std::optional get(const Symbol & name) + Attr * get(const Symbol & name) { Attr key(name, 0); iterator i = std::lower_bound(begin(), end(), key); if (i != end() && i->name == name) return &*i; - return {}; + return nullptr; } Attr & need(const Symbol & name, const Pos & pos = noPos) @@ -77,7 +77,7 @@ public: auto a = get(name); if (!a) throw Error("attribute '%s' missing, at %s", name, pos); - return **a; + return *a; } iterator begin() { return &attrs[0]; }