Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,26 @@ impl<T: Storable, INDEX: Memory, DATA: Memory> Log<T, INDEX, DATA> {
log
}

/// Returns the first entry in the log, if any.
///
/// Complexity: O(1)
#[inline]
pub fn first(&self) -> Option<T> {
self.get(0)
}

/// Returns the last entry in the log, if any.
///
/// Complexity: O(1)
#[inline]
pub fn last(&self) -> Option<T> {
let len = self.len();
if len == 0 {
return None;
}
self.get(len - 1)
}

/// Initializes the log based on the contents of the provided memory trait objects.
/// If the memory trait objects already contain a stable log, this function recovers it from the stable
/// memory. Otherwise, this function allocates a new empty log.
Expand Down
19 changes: 19 additions & 0 deletions src/log/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@ fn test_iter() {
assert_eq!(log.iter().skip(usize::MAX).count(), 0);
}

#[test]
fn test_first_last() {
let log = Log::<String, _, _>::new(VectorMemory::default(), VectorMemory::default());
assert_eq!(log.first(), None);
assert_eq!(log.last(), None);

log.append(&"apple".to_string()).unwrap();
assert_eq!(log.first(), Some("apple".to_string()));
assert_eq!(log.last(), Some("apple".to_string()));

log.append(&"banana".to_string()).unwrap();
assert_eq!(log.first(), Some("apple".to_string()));
assert_eq!(log.last(), Some("banana".to_string()));

log.append(&"cider".to_string()).unwrap();
assert_eq!(log.first(), Some("apple".to_string()));
assert_eq!(log.last(), Some("cider".to_string()));
}

#[allow(clippy::iter_nth_zero)]
#[test]
fn test_thread_local_iter() {
Expand Down