From 65ef4a7583bbe485d81c90b268b928df6f745d5f Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Wed, 11 Jul 2018 21:02:22 +0100 Subject: [PATCH] Add EntityList::from_slice to build a list from an existing slice --- lib/entity/src/list.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/entity/src/list.rs b/lib/entity/src/list.rs index 3c5bd25a13..f94f8ef56e 100644 --- a/lib/entity/src/list.rs +++ b/lib/entity/src/list.rs @@ -225,6 +225,23 @@ impl EntityList { Default::default() } + /// Create a new list with the contents initialized from a slice. + pub fn from_slice(slice: &[T], pool: &mut ListPool) -> Self { + let len = slice.len(); + if len == 0 { + return EntityList::new(); + } + + let block = pool.alloc(sclass_for_length(len)); + pool.data[block] = T::new(len); + pool.data[block + 1..block + len + 1].copy_from_slice(slice); + + EntityList { + index: (block + 1) as u32, + unused: PhantomData, + } + } + /// Returns `true` if the list has a length of 0. pub fn is_empty(&self) -> bool { // 0 is a magic value for the empty list. Any list in the pool array must have a positive @@ -538,6 +555,25 @@ mod tests { assert_eq!(list.first(pool), None); } + #[test] + fn from_slice() { + let pool = &mut ListPool::::new(); + + let list = EntityList::::from_slice(&[Inst(0), Inst(1)], pool); + assert!(!list.is_empty()); + assert_eq!(list.len(pool), 2); + assert_eq!(list.as_slice(pool), &[Inst(0), Inst(1)]); + assert_eq!(list.get(0, pool), Some(Inst(0))); + assert_eq!(list.get(100, pool), None); + + let list = EntityList::::from_slice(&[], pool); + assert!(list.is_empty()); + assert_eq!(list.len(pool), 0); + assert_eq!(list.as_slice(pool), &[]); + assert_eq!(list.get(0, pool), None); + assert_eq!(list.get(100, pool), None); + } + #[test] fn push() { let pool = &mut ListPool::::new();