Set map observers initial value to T::default() on creation (#520)

This commit is contained in:
Andrea Fioraldi 2022-02-03 14:25:25 +01:00 committed by GitHub
parent f527aab15e
commit c561182f07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -255,22 +255,20 @@ where
/// Creates a new [`MapObserver`] /// Creates a new [`MapObserver`]
#[must_use] #[must_use]
pub fn new(name: &'static str, map: &'a mut [T]) -> Self { pub fn new(name: &'static str, map: &'a mut [T]) -> Self {
let initial = if map.is_empty() { T::default() } else { map[0] };
Self { Self {
map: OwnedSliceMut::from(map), map: OwnedSliceMut::from(map),
name: name.to_string(), name: name.to_string(),
initial, initial: T::default(),
} }
} }
/// Creates a new [`MapObserver`] with an owned map /// Creates a new [`MapObserver`] with an owned map
#[must_use] #[must_use]
pub fn new_owned(name: &'static str, map: Vec<T>) -> Self { pub fn new_owned(name: &'static str, map: Vec<T>) -> Self {
let initial = if map.is_empty() { T::default() } else { map[0] };
Self { Self {
map: OwnedSliceMut::from(map), map: OwnedSliceMut::from(map),
name: name.to_string(), name: name.to_string(),
initial, initial: T::default(),
} }
} }
@ -280,16 +278,10 @@ where
/// Will dereference the owned slice with up to len elements. /// Will dereference the owned slice with up to len elements.
#[must_use] #[must_use]
pub fn new_from_ownedref(name: &'static str, map: OwnedSliceMut<'a, T>) -> Self { pub fn new_from_ownedref(name: &'static str, map: OwnedSliceMut<'a, T>) -> Self {
let map_slice = map.as_slice();
let initial = if map_slice.is_empty() {
T::default()
} else {
map_slice[0]
};
Self { Self {
map, map,
name: name.to_string(), name: name.to_string(),
initial, initial: T::default(),
} }
} }
@ -298,11 +290,10 @@ where
/// # Safety /// # Safety
/// Will dereference the `map_ptr` with up to len elements. /// Will dereference the `map_ptr` with up to len elements.
pub unsafe fn new_from_ptr(name: &'static str, map_ptr: *mut T, len: usize) -> Self { pub unsafe fn new_from_ptr(name: &'static str, map_ptr: *mut T, len: usize) -> Self {
let initial = if len > 0 { *map_ptr } else { T::default() };
StdMapObserver { StdMapObserver {
map: OwnedSliceMut::from_raw_parts_mut(map_ptr, len), map: OwnedSliceMut::from_raw_parts_mut(map_ptr, len),
name: name.to_string(), name: name.to_string(),
initial, initial: T::default(),
} }
} }
} }
@ -447,11 +438,10 @@ where
#[must_use] #[must_use]
pub fn new(name: &'static str, map: &'a mut [T]) -> Self { pub fn new(name: &'static str, map: &'a mut [T]) -> Self {
assert!(map.len() >= N); assert!(map.len() >= N);
let initial = if map.is_empty() { T::default() } else { map[0] };
Self { Self {
map: OwnedSliceMut::from(map), map: OwnedSliceMut::from(map),
name: name.to_string(), name: name.to_string(),
initial, initial: T::default(),
} }
} }
@ -472,11 +462,10 @@ where
/// # Safety /// # Safety
/// Will dereference the `map_ptr` with up to len elements. /// Will dereference the `map_ptr` with up to len elements.
pub unsafe fn new_from_ptr(name: &'static str, map_ptr: *mut T) -> Self { pub unsafe fn new_from_ptr(name: &'static str, map_ptr: *mut T) -> Self {
let initial = if N > 0 { *map_ptr } else { T::default() };
ConstMapObserver { ConstMapObserver {
map: OwnedSliceMut::from_raw_parts_mut(map_ptr, N), map: OwnedSliceMut::from_raw_parts_mut(map_ptr, N),
name: name.to_string(), name: name.to_string(),
initial, initial: T::default(),
} }
} }
} }
@ -617,12 +606,11 @@ where
{ {
/// Creates a new [`MapObserver`] /// Creates a new [`MapObserver`]
pub fn new(name: &'static str, map: &'a mut [T], size: &'a mut usize) -> Self { pub fn new(name: &'static str, map: &'a mut [T], size: &'a mut usize) -> Self {
let initial = if map.is_empty() { T::default() } else { map[0] };
Self { Self {
map: OwnedSliceMut::from(map), map: OwnedSliceMut::from(map),
size: OwnedRefMut::Ref(size), size: OwnedRefMut::Ref(size),
name: name.into(), name: name.into(),
initial, initial: T::default(),
} }
} }
@ -636,12 +624,11 @@ where
max_len: usize, max_len: usize,
size: &'a mut usize, size: &'a mut usize,
) -> Self { ) -> Self {
let initial = if max_len > 0 { *map_ptr } else { T::default() };
VariableMapObserver { VariableMapObserver {
map: OwnedSliceMut::from_raw_parts_mut(map_ptr, max_len), map: OwnedSliceMut::from_raw_parts_mut(map_ptr, max_len),
size: OwnedRefMut::Ref(size), size: OwnedRefMut::Ref(size),
name: name.into(), name: name.into(),
initial, initial: T::default(),
} }
} }
} }
@ -917,14 +904,10 @@ where
pub fn new(name: &'static str, maps: &'a mut [&'a mut [T]]) -> Self { pub fn new(name: &'static str, maps: &'a mut [&'a mut [T]]) -> Self {
let mut idx = 0; let mut idx = 0;
let mut v = 0; let mut v = 0;
let mut initial = T::default();
let mut builder = vec![]; let mut builder = vec![];
let maps: Vec<_> = maps let maps: Vec<_> = maps
.iter_mut() .iter_mut()
.map(|x| { .map(|x| {
if !x.is_empty() {
initial = x[0];
}
let l = x.len(); let l = x.len();
let r = (idx..(idx + l), v); let r = (idx..(idx + l), v);
idx += l; idx += l;
@ -938,7 +921,7 @@ where
intervals: builder.into_iter().collect::<IntervalTree<usize, usize>>(), intervals: builder.into_iter().collect::<IntervalTree<usize, usize>>(),
len: idx, len: idx,
name: name.to_string(), name: name.to_string(),
initial, initial: T::default(),
iter_idx: 0, iter_idx: 0,
} }
} }
@ -948,14 +931,10 @@ where
pub fn new_owned(name: &'static str, maps: Vec<Vec<T>>) -> Self { pub fn new_owned(name: &'static str, maps: Vec<Vec<T>>) -> Self {
let mut idx = 0; let mut idx = 0;
let mut v = 0; let mut v = 0;
let mut initial = T::default();
let mut builder = vec![]; let mut builder = vec![];
let maps: Vec<_> = maps let maps: Vec<_> = maps
.into_iter() .into_iter()
.map(|x| { .map(|x| {
if !x.is_empty() {
initial = x[0];
}
let l = x.len(); let l = x.len();
let r = (idx..(idx + l), v); let r = (idx..(idx + l), v);
idx += l; idx += l;
@ -969,7 +948,7 @@ where
intervals: builder.into_iter().collect::<IntervalTree<usize, usize>>(), intervals: builder.into_iter().collect::<IntervalTree<usize, usize>>(),
len: idx, len: idx,
name: name.to_string(), name: name.to_string(),
initial, initial: T::default(),
iter_idx: 0, iter_idx: 0,
} }
} }