Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
Misc patches
Merged did:key:z6MkwcUR...q1kL opened 8 months ago

These are misc patches I found while browsing the source code. The individual patches are not related.

5 files changed +37 -20 ded0d19d 55cdd880
modified crates/radicle-fetch/src/state.rs
@@ -140,10 +140,7 @@ impl FetchResult {
    }

    pub fn is_success(&self) -> bool {
-
        match self {
-
            Self::Success { .. } => true,
-
            Self::Failed { .. } => false,
-
        }
+
        std::matches!(self, Self::Success { .. })
    }
}

modified crates/radicle-term/src/element.rs
@@ -183,13 +183,24 @@ impl Line {
    }

    /// Return a line with a single space between the given labels.
+
    ///
+
    /// TODO: Make this impl trivial once Iterator::intersperse() is stable
    pub fn spaced(items: impl IntoIterator<Item = Label>) -> Self {
-
        let mut line = Self::default();
-
        for item in items.into_iter() {
-
            // Don't create spaces around empty labels.
-
            if item.is_blank() {
-
                continue;
-
            }
+
        let iter = items.into_iter();
+

+
        let mut line = Self {
+
            items: Vec::with_capacity({
+
                let (min, max) = iter.size_hint();
+
                let likely = max.unwrap_or(min);
+

+
                // technically (likely + (likely - 1), but we push the last space before
+
                // we pop it again, so we need that additional space anyways
+
                likely * 2
+
            }),
+
        };
+

+
        // Don't create spaces around empty labels.
+
        for item in iter.filter(|i| !i.is_blank()) {
            line.push(item);
            line.push(Label::space());
        }
@@ -394,4 +405,18 @@ mod test {
        let line = Line::new("❤️");
        assert_eq!(line.width(), 2, "{line}");
    }
+

+
    #[test]
+
    fn test_spaced() {
+
        let line = Line::spaced(["banana", "peach", "apple"].into_iter().map(Label::new));
+

+
        let iterated: Vec<_> = line.into_iter().collect();
+
        assert_eq!(
+
            iterated
+
                .into_iter()
+
                .map(|l| l.to_string())
+
                .collect::<Vec<_>>(),
+
            ["banana", " ", "peach", " ", "apple"]
+
        );
+
    }
}
modified crates/radicle-term/src/vstack.rs
@@ -72,16 +72,13 @@ impl<'a> VStack<'a> {
    }

    /// Add multiple elements to the stack.
-
    pub fn children<I>(self, children: I) -> Self
+
    pub fn children<I>(mut self, children: I) -> Self
    where
        I: IntoIterator<Item = Box<dyn Element>>,
    {
-
        let mut vstack = self;
-

-
        for child in children.into_iter() {
-
            vstack = vstack.child(child);
-
        }
-
        vstack
+
        self.rows
+
            .extend(children.into_iter().map(|e| Row::Element(Box::new(e))));
+
        self
    }

    /// Merge with another `VStack`.
modified crates/radicle/src/node/address.rs
@@ -67,7 +67,6 @@ impl<K: hash::Hash + Eq + Ord + Copy, V> AddressBook<K, V> {
    /// Return a shuffled iterator.
    pub fn shuffled(&self) -> std::vec::IntoIter<(&K, &V)> {
        let mut items = self.inner.iter().collect::<Vec<_>>();
-
        items.sort_by_key(|(k, _)| *k);
        self.rng.borrow_mut().shuffle(&mut items);

        items.into_iter()
@@ -76,7 +75,6 @@ impl<K: hash::Hash + Eq + Ord + Copy, V> AddressBook<K, V> {
    /// Turn this object into a shuffled iterator.
    pub fn into_shuffled(self) -> impl Iterator<Item = (K, V)> {
        let mut items = self.inner.into_iter().collect::<Vec<_>>();
-
        items.sort_by_key(|(k, _)| *k);
        self.rng.borrow_mut().shuffle(&mut items);

        items.into_iter()
modified crates/radicle/src/node/config.rs
@@ -496,7 +496,7 @@ impl Config {
    }

    pub fn peers(&self) -> impl Iterator<Item = NodeId> + '_ {
-
        self.connect.iter().cloned().map(|p| p.id)
+
        self.connect.iter().map(|p| p.0.id)
    }

    pub fn is_persistent(&self, id: &NodeId) -> bool {