From 1eaf262703c2da38cdd96e141467795d69d9e085 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 15 Dec 2024 13:26:29 +0100 Subject: [PATCH] Move asserts to index construction --- src/table.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/table.rs b/src/table.rs index c8a287cf..af6ace70 100644 --- a/src/table.rs +++ b/src/table.rs @@ -21,6 +21,7 @@ mod util; const PAGE_LEN_BITS: usize = 10; const PAGE_LEN_MASK: usize = PAGE_LEN - 1; const PAGE_LEN: usize = 1 << PAGE_LEN_BITS; +const MAX_PAGES: usize = 1 << (32 - PAGE_LEN_BITS); pub(crate) struct Table { pages: AppendOnlyVec>, @@ -91,9 +92,23 @@ impl RefUnwindSafe for Page {} #[derive(Copy, Clone, Debug)] pub struct PageIndex(usize); +impl PageIndex { + fn new(idx: usize) -> Self { + assert!(idx < MAX_PAGES); + Self(idx) + } +} + #[derive(Copy, Clone, Debug)] pub struct SlotIndex(usize); +impl SlotIndex { + fn new(idx: usize) -> Self { + assert!(idx < PAGE_LEN); + Self(idx) + } +} + impl Default for Table { fn default() -> Self { Self { @@ -141,7 +156,7 @@ impl Table { /// Allocate a new page for the given ingredient and with slots of type `T` pub fn push_page(&self, ingredient: IngredientIndex) -> PageIndex { let page = Box::new(>::new(ingredient)); - PageIndex(self.pages.push(page)) + PageIndex::new(self.pages.push(page)) } /// Get the memo table associated with `id` @@ -229,7 +244,7 @@ impl Page { self.allocated.store(index + 1, Ordering::Release); drop(guard); - Ok(make_id(page, SlotIndex(index))) + Ok(make_id(page, SlotIndex::new(index))) } } @@ -276,8 +291,6 @@ impl dyn TablePage { } fn make_id(page: PageIndex, slot: SlotIndex) -> Id { - assert!(slot.0 < PAGE_LEN); - assert!(page.0 < (1 << (32 - PAGE_LEN_BITS))); let page = page.0 as u32; let slot = slot.0 as u32; Id::from_u32(page << PAGE_LEN_BITS | slot) @@ -287,5 +300,5 @@ fn split_id(id: Id) -> (PageIndex, SlotIndex) { let id = id.as_u32() as usize; let slot = id & PAGE_LEN_MASK; let page = id >> PAGE_LEN_BITS; - (PageIndex(page), SlotIndex(slot)) + (PageIndex::new(page), SlotIndex::new(slot)) }