1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
// The mutation operations in this file should panic to prevent undefined behavior
#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]
#![allow(clippy::indexing_slicing)]
#![allow(clippy::panic)]
use super::*;
use crate::ule::*;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::any;
use core::convert::TryInto;
use core::marker::PhantomData;
use core::ops::Deref;
use core::ops::Range;
use core::{fmt, ptr, slice};
use super::components::IntegerULE;
/// A fully-owned [`VarZeroVec`]. This type has no lifetime but has the same
/// internal buffer representation of [`VarZeroVec`], making it cheaply convertible to
/// [`VarZeroVec`] and [`VarZeroSlice`].
///
/// The `F` type parameter is a [`VarZeroVecFormat`] (see its docs for more details), which can be used to select the
/// precise format of the backing buffer with various size and performance tradeoffs. It defaults to [`Index16`].
pub struct VarZeroVecOwned<T: ?Sized, F = Index16> {
marker: PhantomData<(Box<T>, F)>,
// safety invariant: must parse into a valid VarZeroVecComponents
entire_slice: Vec<u8>,
}
impl<T: ?Sized, F> Clone for VarZeroVecOwned<T, F> {
fn clone(&self) -> Self {
VarZeroVecOwned {
marker: self.marker,
entire_slice: self.entire_slice.clone(),
}
}
}
// The effect of a shift on the indices in the varzerovec.
#[derive(PartialEq)]
enum ShiftType {
Insert,
Replace,
Remove,
}
impl<T: VarULE + ?Sized, F: VarZeroVecFormat> Deref for VarZeroVecOwned<T, F> {
type Target = VarZeroSlice<T, F>;
fn deref(&self) -> &VarZeroSlice<T, F> {
self.as_slice()
}
}
impl<T: VarULE + ?Sized, F> VarZeroVecOwned<T, F> {
/// Construct an empty VarZeroVecOwned
pub fn new() -> Self {
Self {
marker: PhantomData,
entire_slice: Vec::new(),
}
}
}
impl<T: VarULE + ?Sized, F: VarZeroVecFormat> VarZeroVecOwned<T, F> {
/// Construct a VarZeroVecOwned from a [`VarZeroSlice`] by cloning the internal data
pub fn from_slice(slice: &VarZeroSlice<T, F>) -> Self {
Self {
marker: PhantomData,
entire_slice: slice.as_bytes().into(),
}
}
/// Construct a VarZeroVecOwned from a list of elements
pub fn try_from_elements<A>(elements: &[A]) -> Result<Self, &'static str>
where
A: EncodeAsVarULE<T>,
{
Ok(if elements.is_empty() {
Self::from_slice(VarZeroSlice::new_empty())
} else {
Self {
marker: PhantomData,
// TODO(#1410): Rethink length errors in VZV.
entire_slice: components::get_serializable_bytes_non_empty::<T, A, F>(elements)
.ok_or(F::Index::TOO_LARGE_ERROR)?,
}
})
}
/// Obtain this `VarZeroVec` as a [`VarZeroSlice`]
pub fn as_slice(&self) -> &VarZeroSlice<T, F> {
let slice: &[u8] = &self.entire_slice;
unsafe {
// safety: the slice is known to come from a valid parsed VZV
VarZeroSlice::from_bytes_unchecked(slice)
}
}
/// Try to allocate a buffer with enough capacity for `capacity`
/// elements. Since `T` can take up an arbitrary size this will
/// just allocate enough space for 4-byte Ts
pub(crate) fn with_capacity(capacity: usize) -> Self {
Self {
marker: PhantomData,
entire_slice: Vec::with_capacity(capacity * (F::Index::SIZE + 4)),
}
}
/// Try to reserve space for `capacity`
/// elements. Since `T` can take up an arbitrary size this will
/// just allocate enough space for 4-byte Ts
pub(crate) fn reserve(&mut self, capacity: usize) {
self.entire_slice.reserve(capacity * (F::Index::SIZE + 4))
}
/// Get the position of a specific element in the data segment.
///
/// If `idx == self.len()`, it will return the size of the data segment (where a new element would go).
///
/// ## Safety
/// `idx <= self.len()` and `self.as_encoded_bytes()` is well-formed.
unsafe fn element_position_unchecked(&self, idx: usize) -> usize {
let len = self.len();
let out = if idx == len {
self.entire_slice.len() - F::Len::SIZE - (F::Index::SIZE * (len - 1))
} else if let Some(idx) = self.index_data(idx) {
idx.iule_to_usize()
} else {
0
};
debug_assert!(out + F::Len::SIZE + (len - 1) * F::Index::SIZE <= self.entire_slice.len());
out
}
/// Get the range of a specific element in the data segment.
///
/// ## Safety
/// `idx < self.len()` and `self.as_encoded_bytes()` is well-formed.
unsafe fn element_range_unchecked(&self, idx: usize) -> core::ops::Range<usize> {
let start = self.element_position_unchecked(idx);
let end = self.element_position_unchecked(idx + 1);
debug_assert!(start <= end, "{start} > {end}");
start..end
}
/// Set the number of elements in the list without any checks.
///
/// ## Safety
/// No safe functions may be called until `self.as_encoded_bytes()` is well-formed.
unsafe fn set_len(&mut self, len: usize) {
assert!(len <= F::Len::MAX_VALUE as usize);
let len_bytes = len.to_le_bytes();
let len_ule = F::Len::iule_from_usize(len).expect(F::Len::TOO_LARGE_ERROR);
self.entire_slice[0..F::Len::SIZE].copy_from_slice(ULE::slice_as_bytes(&[len_ule]));
// Double-check that the length fits in the length field
assert_eq!(len_bytes[F::Len::SIZE..].iter().sum::<u8>(), 0);
}
/// Get the range in the full data for a given index. Returns None for index 0
/// since there is no stored index for it.
fn index_range(index: usize) -> Option<Range<usize>> {
let index_minus_one = index.checked_sub(1)?;
let pos = F::Len::SIZE + F::Index::SIZE * index_minus_one;
Some(pos..pos + F::Index::SIZE)
}
/// Return the raw bytes representing the given `index`. Returns None when given index 0
///
/// ## Safety
/// The index must be valid, and self.as_encoded_bytes() must be well-formed
unsafe fn index_data(&self, index: usize) -> Option<&F::Index> {
let index_range = Self::index_range(index)?;
Some(&F::Index::slice_from_bytes_unchecked(&self.entire_slice[index_range])[0])
}
/// Return the mutable slice representing the given `index`. Returns None when given index 0
///
/// ## Safety
/// The index must be valid. self.as_encoded_bytes() must have allocated space
/// for this index, but need not have its length appropriately set.
unsafe fn index_data_mut(&mut self, index: usize) -> Option<&mut F::Index> {
let ptr = self.entire_slice.as_mut_ptr();
let range = Self::index_range(index)?;
// Doing this instead of just `get_unchecked_mut()` because it's unclear
// if `get_unchecked_mut()` can be called out of bounds on a slice even
// if we know the buffer is larger.
let data = slice::from_raw_parts_mut(ptr.add(range.start), F::Index::SIZE);
Some(&mut F::Index::iule_from_bytes_unchecked_mut(data)[0])
}
/// Shift the indices starting with and after `starting_index` by the provided `amount`.
///
/// ## Panics
/// Should never be called with a starting index of 0, since that index cannot be shifted.
///
/// ## Safety
/// Adding `amount` to each index after `starting_index` must not result in the slice from becoming malformed.
/// The length of the slice must be correctly set.
unsafe fn shift_indices(&mut self, starting_index: usize, amount: i32) {
let normalized_idx = starting_index
.checked_sub(1)
.expect("shift_indices called with a 0 starting index");
let len = self.len();
let indices = F::Index::iule_from_bytes_unchecked_mut(
&mut self.entire_slice[F::Len::SIZE..F::Len::SIZE + F::Index::SIZE * (len - 1)],
);
for idx in &mut indices[normalized_idx..] {
let mut new_idx = idx.iule_to_usize();
if amount > 0 {
new_idx = new_idx.checked_add(amount.try_into().unwrap()).unwrap();
} else {
new_idx = new_idx.checked_sub((-amount).try_into().unwrap()).unwrap();
}
*idx = F::Index::iule_from_usize(new_idx).expect(F::Index::TOO_LARGE_ERROR);
}
}
/// Get this [`VarZeroVecOwned`] as a borrowed [`VarZeroVec`]
///
/// If you wish to repeatedly call methods on this [`VarZeroVecOwned`],
/// it is more efficient to perform this conversion first
pub fn as_varzerovec<'a>(&'a self) -> VarZeroVec<'a, T, F> {
self.as_slice().into()
}
/// Empty the vector
pub fn clear(&mut self) {
self.entire_slice.clear()
}
/// Consume this vector and return the backing buffer
#[inline]
pub fn into_bytes(self) -> Vec<u8> {
self.entire_slice
}
/// Invalidate and resize the data at an index, optionally inserting or removing the index.
/// Also updates affected indices and the length.
///
/// `new_size` is the encoded byte size of the element that is going to be inserted
///
/// Returns a slice to the new element data - it doesn't contain uninitialized data but its value is indeterminate.
///
/// ## Safety
/// - `index` must be a valid index, or, if `shift_type == ShiftType::Insert`, `index == self.len()` is allowed.
/// - `new_size` musn't result in the data segment growing larger than `F::Index::MAX_VALUE`.
unsafe fn shift(&mut self, index: usize, new_size: usize, shift_type: ShiftType) -> &mut [u8] {
// The format of the encoded data is:
// - four bytes of "len"
// - len*4 bytes for an array of indices
// - the actual data to which the indices point
//
// When inserting or removing an element, the size of the indices segment must be changed,
// so the data before the target element must be shifted by 4 bytes in addition to the
// shifting needed for the new element size.
let len = self.len();
let slice_len = self.entire_slice.len();
let prev_element = match shift_type {
ShiftType::Insert => {
let pos = self.element_position_unchecked(index);
// In the case of an insert, there's no previous element,
// so it's an empty range at the new position.
pos..pos
}
_ => self.element_range_unchecked(index),
};
// How much shifting must be done in bytes due to removal/insertion of an index.
let index_shift: i64 = match shift_type {
ShiftType::Insert => F::Index::SIZE as i64,
ShiftType::Replace => 0,
ShiftType::Remove => -(F::Index::SIZE as i64),
};
// The total shift in byte size of the owned slice.
let shift: i64 =
new_size as i64 - (prev_element.end - prev_element.start) as i64 + index_shift;
let new_slice_len = slice_len.wrapping_add(shift as usize);
if shift > 0 {
if new_slice_len > F::Index::MAX_VALUE as usize {
panic!(
"Attempted to grow VarZeroVec to an encoded size that does not fit within the length size used by {}",
any::type_name::<F>()
);
}
self.entire_slice.resize(new_slice_len, 0);
}
// Now that we've ensured there's enough space, we can shift the data around.
{
// Note: There are no references introduced between pointer creation and pointer use, and all
// raw pointers are derived from a single &mut. This preserves pointer provenance.
let slice_range = self.entire_slice.as_mut_ptr_range();
// The start of the indices buffer
let indices_start = slice_range.start.add(F::Len::SIZE);
let old_slice_end = slice_range.start.add(slice_len);
let data_start = indices_start.add((len - 1) * F::Index::SIZE);
let prev_element_p =
data_start.add(prev_element.start)..data_start.add(prev_element.end);
// The memory range of the affected index.
// When inserting: where the new index goes.
// When removing: where the index being removed is.
// When replacing: unused.
// Will be None when the affected index is index 0, which is special
let index_range = if let Some(index_minus_one) = index.checked_sub(1) {
let index_start = indices_start.add(F::Index::SIZE * index_minus_one);
Some(index_start..index_start.add(F::Index::SIZE))
} else {
None
};
unsafe fn shift_bytes(block: Range<*const u8>, to: *mut u8) {
debug_assert!(block.end >= block.start);
ptr::copy(block.start, to, block.end.offset_from(block.start) as usize);
}
if shift_type == ShiftType::Remove {
if let Some(ref index_range) = index_range {
shift_bytes(index_range.end..prev_element_p.start, index_range.start);
} else {
// We are removing the first index, so we skip the second index and copy it over. The second index
// is now zero and unnecessary.
shift_bytes(
indices_start.add(F::Index::SIZE)..prev_element_p.start,
indices_start,
)
}
}
// Shift data after the element to its new position.
shift_bytes(
prev_element_p.end..old_slice_end,
prev_element_p
.start
.offset((new_size as i64 + index_shift) as isize),
);
let first_affected_index = match shift_type {
ShiftType::Insert => {
if let Some(index_range) = index_range {
// Move data before the element forward by 4 to make space for a new index.
shift_bytes(index_range.start..prev_element_p.start, index_range.end);
let index_data = self
.index_data_mut(index)
.expect("If index_range is some, index is > 0 and should not panic in index_data_mut");
*index_data = F::Index::iule_from_usize(prev_element.start)
.expect(F::Index::TOO_LARGE_ERROR);
} else {
// We are adding a new index 0. There's nothing in the indices array for index 0, but the element
// that is currently at index 0 will become index 1 and need a value
// We first shift bytes to make space
shift_bytes(
indices_start..prev_element_p.start,
indices_start.add(F::Index::SIZE),
);
// And then we write a temporary zero to the zeroeth index, which will get shifted later
let index_data = self
.index_data_mut(1)
.expect("Should be able to write to index 1");
*index_data = F::Index::iule_from_usize(0).expect("0 is always valid!");
}
self.set_len(len + 1);
index + 1
}
ShiftType::Remove => {
self.set_len(len - 1);
if index == 0 {
// We don't need to shift index 0 since index 0 is not stored in the indices buffer
index + 1
} else {
index
}
}
ShiftType::Replace => index + 1,
};
// No raw pointer use should occur after this point (because of self.index_data and self.set_len).
// Set the new slice length. This must be done after shifting data around to avoid uninitialized data.
self.entire_slice.set_len(new_slice_len);
// Shift the affected indices.
self.shift_indices(first_affected_index, (shift - index_shift) as i32);
};
debug_assert!(self.verify_integrity());
// Return a mut slice to the new element data.
let element_pos = F::Len::SIZE
+ (self.len() - 1) * F::Index::SIZE
+ self.element_position_unchecked(index);
&mut self.entire_slice[element_pos..element_pos + new_size]
}
/// Checks the internal invariants of the vec to ensure safe code will not cause UB.
/// Returns whether integrity was verified.
///
/// Note: an index is valid if it doesn't point to data past the end of the slice and is
/// less than or equal to all future indices. The length of the index segment is not part of each index.
fn verify_integrity(&self) -> bool {
if self.is_empty() {
if self.entire_slice.is_empty() {
return true;
} else {
panic!(
"VarZeroVecOwned integrity: Found empty VarZeroVecOwned with a nonempty slice"
);
}
}
let len = unsafe {
<F::Len as ULE>::slice_from_bytes_unchecked(&self.entire_slice[..F::Len::SIZE])[0]
.iule_to_usize()
};
if len == 0 {
// An empty vec must have an empty slice: there is only a single valid byte representation.
panic!("VarZeroVecOwned integrity: Found empty VarZeroVecOwned with a nonempty slice");
}
if self.entire_slice.len() < F::Len::SIZE + (len - 1) * F::Index::SIZE {
panic!("VarZeroVecOwned integrity: Not enough room for the indices");
}
let data_len = self.entire_slice.len() - F::Len::SIZE - (len - 1) * F::Index::SIZE;
if data_len > F::Index::MAX_VALUE as usize {
panic!("VarZeroVecOwned integrity: Data segment is too long");
}
// Test index validity.
let indices = unsafe {
F::Index::slice_from_bytes_unchecked(
&self.entire_slice[F::Len::SIZE..F::Len::SIZE + (len - 1) * F::Index::SIZE],
)
};
for idx in indices {
if idx.iule_to_usize() > data_len {
panic!("VarZeroVecOwned integrity: Indices must not point past the data segment");
}
}
for window in indices.windows(2) {
if window[0].iule_to_usize() > window[1].iule_to_usize() {
panic!("VarZeroVecOwned integrity: Indices must be in non-decreasing order");
}
}
true
}
/// Insert an element at the end of this vector
pub fn push<A: EncodeAsVarULE<T> + ?Sized>(&mut self, element: &A) {
self.insert(self.len(), element)
}
/// Insert an element at index `idx`
pub fn insert<A: EncodeAsVarULE<T> + ?Sized>(&mut self, index: usize, element: &A) {
let len = self.len();
if index > len {
panic!("Called out-of-bounds insert() on VarZeroVec, index {index} len {len}");
}
let value_len = element.encode_var_ule_len();
if len == 0 {
let header_len = F::Len::SIZE; // Index array is size 0 for len = 1
let cap = header_len + value_len;
self.entire_slice.resize(cap, 0);
self.entire_slice[0] = 1; // set length
element.encode_var_ule_write(&mut self.entire_slice[header_len..]);
return;
}
assert!(value_len < F::Index::MAX_VALUE as usize);
unsafe {
let place = self.shift(index, value_len, ShiftType::Insert);
element.encode_var_ule_write(place);
}
}
/// Remove the element at index `idx`
pub fn remove(&mut self, index: usize) {
let len = self.len();
if index >= len {
panic!("Called out-of-bounds remove() on VarZeroVec, index {index} len {len}");
}
if len == 1 {
// This is removing the last element. Set the slice to empty to ensure all empty vecs have empty data slices.
self.entire_slice.clear();
return;
}
unsafe {
self.shift(index, 0, ShiftType::Remove);
}
}
/// Replace the element at index `idx` with another
pub fn replace<A: EncodeAsVarULE<T> + ?Sized>(&mut self, index: usize, element: &A) {
let len = self.len();
if index >= len {
panic!("Called out-of-bounds replace() on VarZeroVec, index {index} len {len}");
}
let value_len = element.encode_var_ule_len();
assert!(value_len < F::Index::MAX_VALUE as usize);
unsafe {
let place = self.shift(index, value_len, ShiftType::Replace);
element.encode_var_ule_write(place);
}
}
}
impl<T: VarULE + ?Sized, F: VarZeroVecFormat> fmt::Debug for VarZeroVecOwned<T, F>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
VarZeroSlice::fmt(self, f)
}
}
impl<T: VarULE + ?Sized, F> Default for VarZeroVecOwned<T, F> {
fn default() -> Self {
Self::new()
}
}
impl<T, A, F> PartialEq<&'_ [A]> for VarZeroVecOwned<T, F>
where
T: VarULE + ?Sized,
T: PartialEq,
A: AsRef<T>,
F: VarZeroVecFormat,
{
#[inline]
fn eq(&self, other: &&[A]) -> bool {
self.iter().eq(other.iter().map(|t| t.as_ref()))
}
}
impl<'a, T: ?Sized + VarULE, F: VarZeroVecFormat> From<&'a VarZeroSlice<T, F>>
for VarZeroVecOwned<T, F>
{
fn from(other: &'a VarZeroSlice<T, F>) -> Self {
Self::from_slice(other)
}
}
#[cfg(test)]
mod test {
use super::VarZeroVecOwned;
#[test]
fn test_insert_integrity() {
let mut items: Vec<String> = Vec::new();
let mut zerovec = VarZeroVecOwned::<str>::new();
// Insert into an empty vec.
items.insert(0, "1234567890".into());
zerovec.insert(0, "1234567890");
assert_eq!(zerovec, &*items);
zerovec.insert(1, "foo3");
items.insert(1, "foo3".into());
assert_eq!(zerovec, &*items);
// Insert at the end.
items.insert(items.len(), "qwertyuiop".into());
zerovec.insert(zerovec.len(), "qwertyuiop");
assert_eq!(zerovec, &*items);
items.insert(0, "asdfghjkl;".into());
zerovec.insert(0, "asdfghjkl;");
assert_eq!(zerovec, &*items);
items.insert(2, "".into());
zerovec.insert(2, "");
assert_eq!(zerovec, &*items);
}
#[test]
// ensure that inserting empty items works
fn test_empty_inserts() {
let mut items: Vec<String> = Vec::new();
let mut zerovec = VarZeroVecOwned::<str>::new();
// Insert into an empty vec.
items.insert(0, "".into());
zerovec.insert(0, "");
assert_eq!(zerovec, &*items);
items.insert(0, "".into());
zerovec.insert(0, "");
assert_eq!(zerovec, &*items);
items.insert(0, "1234567890".into());
zerovec.insert(0, "1234567890");
assert_eq!(zerovec, &*items);
items.insert(0, "".into());
zerovec.insert(0, "");
assert_eq!(zerovec, &*items);
}
#[test]
fn test_small_insert_integrity() {
// Tests that insert() works even when there
// is not enough space for the new index in entire_slice.len()
let mut items: Vec<String> = Vec::new();
let mut zerovec = VarZeroVecOwned::<str>::new();
// Insert into an empty vec.
items.insert(0, "abc".into());
zerovec.insert(0, "abc");
assert_eq!(zerovec, &*items);
zerovec.insert(1, "def");
items.insert(1, "def".into());
assert_eq!(zerovec, &*items);
}
#[test]
#[should_panic]
fn test_insert_past_end() {
VarZeroVecOwned::<str>::new().insert(1, "");
}
#[test]
fn test_remove_integrity() {
let mut items: Vec<&str> = vec!["apples", "bananas", "eeples", "", "baneenees", "five", ""];
let mut zerovec = VarZeroVecOwned::<str>::try_from_elements(&items).unwrap();
for index in [0, 2, 4, 0, 1, 1, 0] {
items.remove(index);
zerovec.remove(index);
assert_eq!(zerovec, &*items, "index {}, len {}", index, items.len());
}
}
#[test]
fn test_removing_last_element_clears() {
let mut zerovec = VarZeroVecOwned::<str>::try_from_elements(&["buy some apples"]).unwrap();
assert!(!zerovec.as_bytes().is_empty());
zerovec.remove(0);
assert!(zerovec.as_bytes().is_empty());
}
#[test]
#[should_panic]
fn test_remove_past_end() {
VarZeroVecOwned::<str>::new().remove(0);
}
#[test]
fn test_replace_integrity() {
let mut items: Vec<&str> = vec!["apples", "bananas", "eeples", "", "baneenees", "five", ""];
let mut zerovec = VarZeroVecOwned::<str>::try_from_elements(&items).unwrap();
// Replace with an element of the same size (and the first element)
items[0] = "blablah";
zerovec.replace(0, "blablah");
assert_eq!(zerovec, &*items);
// Replace with a smaller element
items[1] = "twily";
zerovec.replace(1, "twily");
assert_eq!(zerovec, &*items);
// Replace an empty element
items[3] = "aoeuidhtns";
zerovec.replace(3, "aoeuidhtns");
assert_eq!(zerovec, &*items);
// Replace the last element
items[6] = "0123456789";
zerovec.replace(6, "0123456789");
assert_eq!(zerovec, &*items);
// Replace with an empty element
items[2] = "";
zerovec.replace(2, "");
assert_eq!(zerovec, &*items);
}
#[test]
#[should_panic]
fn test_replace_past_end() {
VarZeroVecOwned::<str>::new().replace(0, "");
}
}