From 7875d6ed2c16ded8419e9e1bb237d5a6f759234b Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 20 May 2021 11:22:19 +0100 Subject: [PATCH] utils: Break out [Index]Vec::ensure_element_with ext traits Signed-off-by: Ian Jackson --- src/bundles.rs | 5 +---- src/utils.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/bundles.rs b/src/bundles.rs index 6c8af351..7232ac29 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -886,10 +886,7 @@ impl InstanceBundles { }, }; - let iu: usize = parsed.index().into(); - if ib.bundles.get(iu).is_none() { - ib.bundles.resize_with(iu+1, default); - } + ib.bundles.ensure_element_with(parsed.index().into(), default); if_let!{ BundleSavefile::Bundle(id) = parsed; else continue; } diff --git a/src/utils.rs b/src/utils.rs index fd2e530b..335de872 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -623,3 +623,19 @@ impl T { #[throws(io::Error)] fn rewind(&mut self) { self.seek(io::SeekFrom::Start(0))? } } + +#[ext(pub)] +impl Vec { + fn ensure_element_with(&mut self, i: usize, f: F) where F: FnMut() -> T { + if self.get(i).is_none() { + self.resize_with(i+1, f); + } + } +} + +#[ext(pub)] +impl IndexVec where I: index_vec::Idx { + fn ensure_element_with(&mut self, i: I, f: F) where F: FnMut() -> T { + self.raw.ensure_element_with(i.index(), f) + } +} -- 2.30.2