From 67bf4250caf14d3a632abdbfa367c04b318d782c Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 25 Nov 2021 10:07:23 +0800 Subject: Optimize space for DocumentId with NonZeroUsize (#1097) Now Option uses one byte rather than two--- helix-view/src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'helix-view/src/lib.rs') diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index 3e779356..4d19ee2e 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -12,8 +12,18 @@ pub mod theme; pub mod tree; pub mod view; -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)] -pub struct DocumentId(usize); +use std::num::NonZeroUsize; + +// uses NonZeroUsize so Option use a byte rather than two +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +pub struct DocumentId(NonZeroUsize); + +impl Default for DocumentId { + fn default() -> DocumentId { + // Safety: 1 is non-zero + DocumentId(unsafe { NonZeroUsize::new_unchecked(1) }) + } +} slotmap::new_key_type! { pub struct ViewId; -- cgit v1.2.3-70-g09d2