diff --git a/src/cursor.rs b/src/cursor.rs index 5807d6b..8bd6454 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -1,5 +1,5 @@ use std::marker::PhantomData; -use std::{fmt, mem, ptr, result, slice}; +use std::{fmt, iter, mem, ptr, result, slice}; use libc::{EINVAL, c_void, size_t, c_uint}; @@ -57,12 +57,12 @@ pub trait Cursor<'txn> { /// For databases with duplicate data items (`DatabaseFlags::DUP_SORT`), the /// duplicate data items of each key will be returned before moving on to /// the next key. - fn iter_from(&mut self, key: K) -> Result> where K: AsRef<[u8]> { + fn iter_from(&mut self, key: K) -> Box>> where K: AsRef<[u8]> { match self.get(Some(key.as_ref()), None, ffi::MDB_SET_RANGE) { Ok(_) | Err(Error::NotFound) => (), - Err(error) => return Err(error), + Err(error) => return Box::new(iter::once(Err(error))), }; - Ok(Iter::new(self.cursor(), ffi::MDB_GET_CURRENT, ffi::MDB_NEXT)) + Box::new(Iter::new(self.cursor(), ffi::MDB_GET_CURRENT, ffi::MDB_NEXT)) } /// Iterate over duplicate database items. The iterator will begin with the @@ -89,12 +89,12 @@ pub trait Cursor<'txn> { } /// Iterate over the duplicates of the item in the database with the given key. - fn iter_dup_of(&mut self, key: &K) -> Result> where K: AsRef<[u8]> { + fn iter_dup_of(&mut self, key: &K) -> Box>> where K: AsRef<[u8]> { match self.get(Some(key.as_ref()), None, ffi::MDB_SET) { Ok(_) | Err(Error::NotFound) => (), - Err(error) => return Err(error), + Err(error) => return Box::new(iter::once(Err(error))), }; - Ok(Iter::new(self.cursor(), ffi::MDB_GET_CURRENT, ffi::MDB_NEXT_DUP)) + Box::new(Iter::new(self.cursor(), ffi::MDB_GET_CURRENT, ffi::MDB_NEXT_DUP)) } } @@ -450,18 +450,18 @@ mod test { assert_eq!(items, cursor.iter_start().collect::>>().unwrap()); assert_eq!(items.clone().into_iter().skip(1).collect::>(), - cursor.iter_from(b"key2").unwrap().collect::>>().unwrap()); + cursor.iter_from(b"key2").collect::>>().unwrap()); assert_eq!(items.clone().into_iter().skip(3).collect::>(), - cursor.iter_from(b"key4").unwrap().collect::>>().unwrap()); + cursor.iter_from(b"key4").collect::>>().unwrap()); assert_eq!(vec!().into_iter().collect::>(), - cursor.iter_from(b"key6").unwrap().collect::>>().unwrap()); + cursor.iter_from(b"key6").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::>>() { + match cursor.iter_from("a").collect::>>() { Ok(_) => Ok(()), Err(error) => Err(error), } @@ -479,7 +479,7 @@ mod test { assert_eq!(0, cursor.iter().count()); assert_eq!(0, cursor.iter_start().count()); - assert_eq!(0, cursor.iter_from(b"foo").unwrap().count()); + assert_eq!(0, cursor.iter_from(b"foo").count()); } #[test] @@ -492,11 +492,11 @@ mod test { assert_eq!(0, cursor.iter().count()); assert_eq!(0, cursor.iter_start().count()); - assert_eq!(0, cursor.iter_from(b"foo").unwrap().count()); + assert_eq!(0, cursor.iter_from(b"foo").count()); assert_eq!(0, cursor.iter_dup().count()); assert_eq!(0, cursor.iter_dup_start().count()); assert_eq!(0, cursor.iter_dup_from(b"foo").unwrap().count()); - assert_eq!(0, cursor.iter_dup_of(b"foo").unwrap().count()); + assert_eq!(0, cursor.iter_dup_of(b"foo").count()); } #[test] @@ -550,9 +550,9 @@ mod test { cursor.iter_dup_from(b"f").unwrap().flat_map(|x| x).collect::>>().unwrap()); assert_eq!(items.clone().into_iter().skip(3).take(3).collect::>(), - cursor.iter_dup_of(b"b").unwrap().collect::>>().unwrap()); + cursor.iter_dup_of(b"b").collect::>>().unwrap()); - assert_eq!(0, cursor.iter_dup_of(b"foo").unwrap().count()); + assert_eq!(0, cursor.iter_dup_of(b"foo").count()); } #[test]