fix Queue::last

This commit is contained in:
soruh 2023-08-14 17:42:59 +02:00
parent c0623ce64f
commit 88ed1ddafd

View File

@ -24,6 +24,7 @@ impl<T: FromBytes + FromZeroes + AsBytes + Unaligned + Clone + Copy> Slabable fo
} }
} }
// TODO: make this return the bare Queue to avoid additional allocations
impl<T: FromBytes + FromZeroes + AsBytes + Unaligned + Clone + Copy> Queue<T> { impl<T: FromBytes + FromZeroes + AsBytes + Unaligned + Clone + Copy> Queue<T> {
pub fn new<R>(transaction: &mut TransactionHandle<R>) -> FilePointer<Queue<T>> { pub fn new<R>(transaction: &mut TransactionHandle<R>) -> FilePointer<Queue<T>> {
let (queue, data) = transaction.allocate(); let (queue, data) = transaction.allocate();
@ -276,7 +277,13 @@ impl<T: FromBytes + FromZeroes + AsBytes + Unaligned + Clone + Copy> FilePointer
} }
pub fn last(self, reader: &impl ReaderTrait) -> Option<T> { pub fn last(self, reader: &impl ReaderTrait) -> Option<T> {
self.get(reader, self.length(reader) - 1) let length = self.length(reader);
if length == 0 {
None
} else {
self.get(reader, length - 1)
}
} }
pub fn length(self, reader: &impl ReaderTrait) -> u64 { pub fn length(self, reader: &impl ReaderTrait) -> u64 {