From c3cb55c8255134f38dc70b997683acac32b6a1b4 Mon Sep 17 00:00:00 2001 From: Myk Melez Date: Tue, 7 Aug 2018 22:56:29 -0700 Subject: [PATCH] demonstrate various uses of API --- src/cursor.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/cursor.rs b/src/cursor.rs index 1dbcc2c..997b226 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -433,7 +433,10 @@ mod test { let txn = env.begin_ro_txn().unwrap(); let mut cursor = txn.open_ro_cursor(db).unwrap(); + assert_eq!(items, cursor.iter().collect::>>().unwrap()); + let retr: Result> = cursor.iter().collect(); + assert_eq!(items, retr.unwrap()); cursor.get(Some(b"key2"), None, MDB_SET).unwrap(); assert_eq!(items.clone().into_iter().skip(2).collect::>(), @@ -449,6 +452,16 @@ mod test { assert_eq!(vec!().into_iter().collect::>(), cursor.iter_from(b"key6").unwrap().collect::>>().unwrap()); + + // Demonstrate how a function that returns a result can use the "?" + // operator to propagate an error returned by Cursor::iter*() methods. + fn iterate<'a>(cursor: &mut RoCursor) -> Result<()> { + match cursor.iter_from("a")?.collect::>>() { + Ok(_) => Ok(()), + Err(error) => Err(error), + } + } + iterate(&mut cursor).unwrap(); } #[test] @@ -571,10 +584,30 @@ mod test { let mut i = 0; let mut count = 0u32; - for (key, data) in cursor.iter().map(|x| x.unwrap()) { + for (key, data) in cursor.iter().map(Result::unwrap) { i = i + key.len() + data.len(); count = count + 1; } + for (key, data) in cursor.iter().filter_map(Result::ok) { + i = i + key.len() + data.len(); + count = count + 1; + } + + fn iterate<'a>(cursor: &mut RoCursor) -> Result<()> { + let mut i = 0; + let mut count = 0u32; + for result in cursor.iter() { + // let (key, data) = match result { + // Ok((key, data)) => (key, data), + // Err(error) => return Err(error), + // }; + let (key, data) = result?; + i = i + key.len() + data.len(); + count = count + 1; + } + Ok(()) + } + iterate(&mut cursor).unwrap(); black_box(i); assert_eq!(count, n);