From 8748e155a3b0669c40945ccf61e1b6ebeb4f0504 Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Sun, 28 Jan 2024 18:17:12 +0100 Subject: [PATCH] Add code example to docs --- src/discv5.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/discv5.rs b/src/discv5.rs index 420801445..2e13d884b 100644 --- a/src/discv5.rs +++ b/src/discv5.rs @@ -478,7 +478,24 @@ impl Discv5

{ } /// Takes a closure parameterized by type `Arc>>` as - /// parameter. Caution: caller is responsible of dropping a lock taken on the kbuckets. + /// parameter. Caution: caller is responsible of dropping a lock taken on the kbuckets. We can + /// then for example take a read lock only to optimistically view the current keys in the + /// kbuckets (optimistic since it doesn't apply pending entries, which requires a write lock). + /// ``` + /// use std::str::FromStr; + /// use discv5::{ConfigBuilder, Discv5, ListenConfig, Enr, enr::CombinedKey}; + /// + /// let sk = CombinedKey::generate_secp256k1(); + /// let enr = Enr::builder().build(&sk).unwrap(); + /// let config = ConfigBuilder::new(ListenConfig::default()).build(); + /// let discv5: Discv5 = Discv5::new(enr, sk, config).unwrap(); + /// + /// let entries = discv5.with_kbuckets(|kbuckets| kbuckets + /// .read() + /// .iter_ref() + /// .map(|entry| *entry.node.key.preimage()) + /// .collect::>()); + /// ``` pub fn with_kbuckets(&self, f: F) -> T where F: FnOnce(&Arc>>) -> T,