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
// 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 ).
use super::components::VarZeroVecComponents;
use super::*;
use crate::ule::*;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::cmp::{Ord, Ordering, PartialOrd};
use core::fmt;
use core::marker::PhantomData;
use core::mem;
use core::ops::Index;
use core::ops::Range;
/// A zero-copy "slice", that works for unsized types, i.e. the zero-copy version of `[T]`
/// where `T` is not `Sized`.
///
/// This behaves similarly to [`VarZeroVec<T>`], however [`VarZeroVec<T>`] is allowed to contain
/// owned data and as such is ideal for deserialization since most human readable
/// serialization formats cannot unconditionally deserialize zero-copy.
///
/// This type can be used inside [`VarZeroVec<T>`](crate::VarZeroVec) and [`ZeroMap`](crate::ZeroMap):
/// This essentially allows for the construction of zero-copy types isomorphic to `Vec<Vec<T>>` by instead
/// using `VarZeroVec<ZeroSlice<T>>`.
///
/// 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`].
///
/// This type can be nested within itself to allow for multi-level nested `Vec`s.
///
/// # Examples
///
/// ## Nested Slices
///
/// The following code constructs the conceptual zero-copy equivalent of `Vec<Vec<Vec<str>>>`
///
/// ```rust
/// use zerovec::{VarZeroSlice, VarZeroVec};
/// let strings_1: Vec<&str> = vec!["foo", "bar", "baz"];
/// let strings_2: Vec<&str> = vec!["twelve", "seventeen", "forty two"];
/// let strings_3: Vec<&str> = vec!["我", "喜歡", "烏龍茶"];
/// let strings_4: Vec<&str> = vec!["w", "ω", "文", "𑄃"];
/// let strings_12 = vec![&*strings_1, &*strings_2];
/// let strings_34 = vec![&*strings_3, &*strings_4];
/// let all_strings = vec![strings_12, strings_34];
///
/// let vzv_1: VarZeroVec<str> = VarZeroVec::from(&strings_1);
/// let vzv_2: VarZeroVec<str> = VarZeroVec::from(&strings_2);
/// let vzv_3: VarZeroVec<str> = VarZeroVec::from(&strings_3);
/// let vzv_4: VarZeroVec<str> = VarZeroVec::from(&strings_4);
/// let vzv_12 = VarZeroVec::from(&[vzv_1.as_slice(), vzv_2.as_slice()]);
/// let vzv_34 = VarZeroVec::from(&[vzv_3.as_slice(), vzv_4.as_slice()]);
/// let vzv_all = VarZeroVec::from(&[vzv_12.as_slice(), vzv_34.as_slice()]);
///
/// let reconstructed: Vec<Vec<Vec<String>>> = vzv_all
/// .iter()
/// .map(|v: &VarZeroSlice<VarZeroSlice<str>>| {
/// v.iter()
/// .map(|x: &VarZeroSlice<_>| {
/// x.as_varzerovec()
/// .iter()
/// .map(|s| s.to_owned())
/// .collect::<Vec<String>>()
/// })
/// .collect::<Vec<_>>()
/// })
/// .collect::<Vec<_>>();
/// assert_eq!(reconstructed, all_strings);
///
/// let bytes = vzv_all.as_bytes();
/// let vzv_from_bytes: VarZeroVec<VarZeroSlice<VarZeroSlice<str>>> =
/// VarZeroVec::parse_byte_slice(bytes).unwrap();
/// assert_eq!(vzv_from_bytes, vzv_all);
/// ```
///
/// ## Iterate over Windows
///
/// Although [`VarZeroSlice`] does not itself have a `.windows` iterator like
/// [core::slice::Windows], this behavior can be easily modeled using an iterator:
///
/// ```
/// use zerovec::VarZeroVec;
///
/// let vzv = VarZeroVec::<str>::from(&["a", "b", "c", "d"]);
/// # let mut pairs: Vec<(&str, &str)> = Vec::new();
///
/// let mut it = vzv.iter().peekable();
/// while let (Some(x), Some(y)) = (it.next(), it.peek()) {
/// // Evaluate (x, y) here.
/// # pairs.push((x, y));
/// }
/// # assert_eq!(pairs, &[("a", "b"), ("b", "c"), ("c", "d")]);
/// ```
//
// safety invariant: The slice MUST be one which parses to
// a valid VarZeroVecComponents<T>
#[repr(transparent)]
pub struct VarZeroSlice<T: ?Sized, F = Index16> {
marker: PhantomData<(F, T)>,
/// The original slice this was constructed from
entire_slice: [u8],
}
impl<T: VarULE + ?Sized, F: VarZeroVecFormat> VarZeroSlice<T, F> {
/// Construct a new empty VarZeroSlice
pub const fn new_empty() -> &'static Self {
// The empty VZV is special-cased to the empty slice
unsafe { mem::transmute(&[] as &[u8]) }
}
/// Obtain a [`VarZeroVecComponents`] borrowing from the internal buffer
#[inline]
pub(crate) fn as_components<'a>(&'a self) -> VarZeroVecComponents<'a, T, F> {
unsafe {
// safety: VarZeroSlice is guaranteed to parse here
VarZeroVecComponents::from_bytes_unchecked(&self.entire_slice)
}
}
/// Uses a `&[u8]` buffer as a `VarZeroSlice<T>` without any verification.
///
/// # Safety
///
/// `bytes` need to be an output from [`VarZeroSlice::as_bytes()`].
pub const unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self {
// self is really just a wrapper around a byte slice
mem::transmute(bytes)
}
/// Get the number of elements in this slice
///
/// # Example
///
/// ```rust
/// # use zerovec::ule::ZeroVecError;
/// # use zerovec::VarZeroVec;
///
/// let strings = vec!["foo", "bar", "baz", "quux"];
/// let vec = VarZeroVec::<str>::from(&strings);
///
/// assert_eq!(vec.len(), 4);
/// # Ok::<(), ZeroVecError>(())
/// ```
pub fn len(&self) -> usize {
self.as_components().len()
}
/// Returns `true` if the slice contains no elements.
///
/// # Examples
///
/// ```
/// # use zerovec::ule::ZeroVecError;
/// # use zerovec::VarZeroVec;
///
/// let strings: Vec<String> = vec![];
/// let vec = VarZeroVec::<str>::from(&strings);
///
/// assert!(vec.is_empty());
/// # Ok::<(), ZeroVecError>(())
/// ```
pub fn is_empty(&self) -> bool {
self.as_components().is_empty()
}
/// Obtain an iterator over this slice's elements
///
/// # Example
///
/// ```rust
/// # use zerovec::ule::ZeroVecError;
/// # use zerovec::VarZeroVec;
///
/// let strings = vec!["foo", "bar", "baz", "quux"];
/// let vec = VarZeroVec::<str>::from(&strings);
///
/// let mut iter_results: Vec<&str> = vec.iter().collect();
/// assert_eq!(iter_results[0], "foo");
/// assert_eq!(iter_results[1], "bar");
/// assert_eq!(iter_results[2], "baz");
/// assert_eq!(iter_results[3], "quux");
/// # Ok::<(), ZeroVecError>(())
/// ```
pub fn iter<'b>(&'b self) -> impl Iterator<Item = &'b T> {
self.as_components().iter()
}
/// Get one of this slice's elements, returning `None` if the index is out of bounds
///
/// # Example
///
/// ```rust
/// # use zerovec::ule::ZeroVecError;
/// # use zerovec::VarZeroVec;
///
/// let strings = vec!["foo", "bar", "baz", "quux"];
/// let vec = VarZeroVec::<str>::from(&strings);
///
/// let mut iter_results: Vec<&str> = vec.iter().collect();
/// assert_eq!(vec.get(0), Some("foo"));
/// assert_eq!(vec.get(1), Some("bar"));
/// assert_eq!(vec.get(2), Some("baz"));
/// assert_eq!(vec.get(3), Some("quux"));
/// assert_eq!(vec.get(4), None);
/// # Ok::<(), ZeroVecError>(())
/// ```
pub fn get(&self, idx: usize) -> Option<&T> {
self.as_components().get(idx)
}
/// Get one of this slice's elements
///
/// # Safety
///
/// `index` must be in range
///
/// # Example
///
/// ```rust
/// # use zerovec::ule::ZeroVecError;
/// # use zerovec::VarZeroVec;
///
/// let strings = vec!["foo", "bar", "baz", "quux"];
/// let vec = VarZeroVec::<str>::from(&strings);
///
/// let mut iter_results: Vec<&str> = vec.iter().collect();
/// unsafe {
/// assert_eq!(vec.get_unchecked(0), "foo");
/// assert_eq!(vec.get_unchecked(1), "bar");
/// assert_eq!(vec.get_unchecked(2), "baz");
/// assert_eq!(vec.get_unchecked(3), "quux");
/// }
/// # Ok::<(), ZeroVecError>(())
/// ```
pub unsafe fn get_unchecked(&self, idx: usize) -> &T {
self.as_components().get_unchecked(idx)
}
/// Obtain an owned `Vec<Box<T>>` out of this
pub fn to_vec(&self) -> Vec<Box<T>> {
self.as_components().to_vec()
}
/// Get a reference to the entire encoded backing buffer of this slice
///
/// The bytes can be passed back to [`Self::parse_byte_slice()`].
///
/// To take the bytes as a vector, see [`VarZeroVec::into_bytes()`].
///
/// # Example
///
/// ```rust
/// # use zerovec::ule::ZeroVecError;
/// # use zerovec::VarZeroVec;
///
/// let strings = vec!["foo", "bar", "baz"];
/// let vzv = VarZeroVec::<str>::from(&strings);
///
/// assert_eq!(vzv, VarZeroVec::parse_byte_slice(vzv.as_bytes()).unwrap());
///
/// # Ok::<(), ZeroVecError>(())
/// ```
#[inline]
pub const fn as_bytes(&self) -> &[u8] {
&self.entire_slice
}
/// Get this [`VarZeroSlice`] as a borrowed [`VarZeroVec`]
///
/// If you wish to repeatedly call methods on this [`VarZeroSlice`],
/// it is more efficient to perform this conversion first
pub const fn as_varzerovec<'a>(&'a self) -> VarZeroVec<'a, T, F> {
VarZeroVec::Borrowed(self)
}
/// Parse a VarZeroSlice from a slice of the appropriate format
///
/// Slices of the right format can be obtained via [`VarZeroSlice::as_bytes()`]
pub fn parse_byte_slice<'a>(slice: &'a [u8]) -> Result<&'a Self, ZeroVecError> {
<Self as VarULE>::parse_byte_slice(slice)
}
/// Convert a `bytes` array known to represent a `VarZeroSlice` to a mutable reference to a `VarZeroSlice`
///
/// # Safety
/// - `bytes` must be a valid sequence of bytes for this VarZeroVec
pub(crate) unsafe fn from_byte_slice_unchecked_mut(bytes: &mut [u8]) -> &mut Self {
// self is really just a wrapper around a byte slice
mem::transmute(bytes)
}
pub(crate) unsafe fn get_bytes_at_mut(&mut self, idx: usize) -> &mut [u8] {
let range = self.as_components().get_range(idx);
#[allow(clippy::indexing_slicing)] // get_range() is known to return in-bounds ranges
&mut self.entire_slice[range]
}
}
impl<T, F> VarZeroSlice<T, F>
where
T: VarULE,
T: ?Sized,
T: Ord,
F: VarZeroVecFormat,
{
/// Binary searches a sorted `VarZeroVec<T>` for the given element. For more information, see
/// the standard library function [`binary_search`].
///
/// # Example
///
/// ```
/// # use zerovec::ule::ZeroVecError;
/// # use zerovec::VarZeroVec;
///
/// let strings = vec!["a", "b", "f", "g"];
/// let vec = VarZeroVec::<str>::from(&strings);
///
/// assert_eq!(vec.binary_search("f"), Ok(2));
/// assert_eq!(vec.binary_search("e"), Err(2));
/// # Ok::<(), ZeroVecError>(())
/// ```
///
/// [`binary_search`]: https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search
#[inline]
pub fn binary_search(&self, x: &T) -> Result<usize, usize> {
self.as_components().binary_search(x)
}
/// Binary searches a `VarZeroVec<T>` for the given element within a certain sorted range.
///
/// If the range is out of bounds, returns `None`. Otherwise, returns a `Result` according
/// to the behavior of the standard library function [`binary_search`].
///
/// The index is returned relative to the start of the range.
///
/// # Example
///
/// ```
/// # use zerovec::ule::ZeroVecError;
/// # use zerovec::VarZeroVec;
///
/// let strings = vec!["a", "b", "f", "g", "m", "n", "q"];
/// let vec = VarZeroVec::<str>::from(&strings);
///
/// // Same behavior as binary_search when the range covers the whole slice:
/// assert_eq!(vec.binary_search_in_range("g", 0..7), Some(Ok(3)));
/// assert_eq!(vec.binary_search_in_range("h", 0..7), Some(Err(4)));
///
/// // Will not look outside of the range:
/// assert_eq!(vec.binary_search_in_range("g", 0..1), Some(Err(1)));
/// assert_eq!(vec.binary_search_in_range("g", 6..7), Some(Err(0)));
///
/// // Will return indices relative to the start of the range:
/// assert_eq!(vec.binary_search_in_range("g", 1..6), Some(Ok(2)));
/// assert_eq!(vec.binary_search_in_range("h", 1..6), Some(Err(3)));
///
/// // Will return `None` if the range is out of bounds:
/// assert_eq!(vec.binary_search_in_range("x", 100..200), None);
/// assert_eq!(vec.binary_search_in_range("x", 0..200), None);
/// # Ok::<(), ZeroVecError>(())
/// ```
///
/// [`binary_search`]: https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search
#[inline]
pub fn binary_search_in_range(
&self,
x: &T,
range: Range<usize>,
) -> Option<Result<usize, usize>> {
self.as_components().binary_search_in_range(x, range)
}
}
impl<T, F> VarZeroSlice<T, F>
where
T: VarULE,
T: ?Sized,
F: VarZeroVecFormat,
{
/// Binary searches a sorted `VarZeroVec<T>` for the given predicate. For more information, see
/// the standard library function [`binary_search_by`].
///
/// # Example
///
/// ```
/// # use zerovec::ule::ZeroVecError;
/// # use zerovec::VarZeroVec;
///
/// let strings = vec!["a", "b", "f", "g"];
/// let vec = VarZeroVec::<str>::from(&strings);
///
/// assert_eq!(vec.binary_search_by(|probe| probe.cmp("f")), Ok(2));
/// assert_eq!(vec.binary_search_by(|probe| probe.cmp("e")), Err(2));
/// # Ok::<(), ZeroVecError>(())
/// ```
///
/// [`binary_search_by`]: https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search_by
#[inline]
pub fn binary_search_by(&self, predicate: impl FnMut(&T) -> Ordering) -> Result<usize, usize> {
self.as_components().binary_search_by(predicate)
}
/// Binary searches a `VarZeroVec<T>` for the given predicate within a certain sorted range.
///
/// If the range is out of bounds, returns `None`. Otherwise, returns a `Result` according
/// to the behavior of the standard library function [`binary_search`].
///
/// The index is returned relative to the start of the range.
///
/// # Example
///
/// ```
/// # use zerovec::ule::ZeroVecError;
/// # use zerovec::VarZeroVec;
///
/// let strings = vec!["a", "b", "f", "g", "m", "n", "q"];
/// let vec = VarZeroVec::<str>::from(&strings);
///
/// // Same behavior as binary_search when the range covers the whole slice:
/// assert_eq!(
/// vec.binary_search_in_range_by(|v| v.cmp("g"), 0..7),
/// Some(Ok(3))
/// );
/// assert_eq!(
/// vec.binary_search_in_range_by(|v| v.cmp("h"), 0..7),
/// Some(Err(4))
/// );
///
/// // Will not look outside of the range:
/// assert_eq!(
/// vec.binary_search_in_range_by(|v| v.cmp("g"), 0..1),
/// Some(Err(1))
/// );
/// assert_eq!(
/// vec.binary_search_in_range_by(|v| v.cmp("g"), 6..7),
/// Some(Err(0))
/// );
///
/// // Will return indices relative to the start of the range:
/// assert_eq!(
/// vec.binary_search_in_range_by(|v| v.cmp("g"), 1..6),
/// Some(Ok(2))
/// );
/// assert_eq!(
/// vec.binary_search_in_range_by(|v| v.cmp("h"), 1..6),
/// Some(Err(3))
/// );
///
/// // Will return `None` if the range is out of bounds:
/// assert_eq!(
/// vec.binary_search_in_range_by(|v| v.cmp("x"), 100..200),
/// None
/// );
/// assert_eq!(vec.binary_search_in_range_by(|v| v.cmp("x"), 0..200), None);
/// # Ok::<(), ZeroVecError>(())
/// ```
///
/// [`binary_search`]: https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search
pub fn binary_search_in_range_by(
&self,
predicate: impl FnMut(&T) -> Ordering,
range: Range<usize>,
) -> Option<Result<usize, usize>> {
self.as_components()
.binary_search_in_range_by(predicate, range)
}
}
// Safety (based on the safety checklist on the VarULE trait):
// 1. VarZeroSlice does not include any uninitialized or padding bytes (achieved by `#[repr(transparent)]` on a
// `[u8]` slice which satisfies this invariant)
// 2. VarZeroSlice is aligned to 1 byte (achieved by `#[repr(transparent)]` on a
// `[u8]` slice which satisfies this invariant)
// 3. The impl of `validate_byte_slice()` returns an error if any byte is not valid.
// 4. The impl of `validate_byte_slice()` returns an error if the slice cannot be used in its entirety
// 5. The impl of `from_byte_slice_unchecked()` returns a reference to the same data.
// 6. `as_byte_slice()` is equivalent to a regular transmute of the underlying data
// 7. VarZeroSlice byte equality is semantic equality (relying on the guideline of the underlying VarULE type)
unsafe impl<T: VarULE + ?Sized + 'static, F: VarZeroVecFormat> VarULE for VarZeroSlice<T, F> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
let _: VarZeroVecComponents<T, F> = VarZeroVecComponents::parse_byte_slice(bytes)?;
Ok(())
}
unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self {
// self is really just a wrapper around a byte slice
mem::transmute(bytes)
}
fn as_byte_slice(&self) -> &[u8] {
&self.entire_slice
}
}
impl<T: VarULE + ?Sized, F: VarZeroVecFormat> Index<usize> for VarZeroSlice<T, F> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
#[allow(clippy::panic)] // documented
match self.get(index) {
Some(x) => x,
None => panic!(
"index out of bounds: the len is {} but the index is {index}",
self.len()
),
}
}
}
impl<T, F> PartialEq<VarZeroSlice<T, F>> for VarZeroSlice<T, F>
where
T: VarULE,
T: ?Sized,
T: PartialEq,
F: VarZeroVecFormat,
{
#[inline]
fn eq(&self, other: &VarZeroSlice<T, F>) -> bool {
// VarULE has an API guarantee that this is equivalent
// to `T::VarULE::eq()`
self.entire_slice.eq(&other.entire_slice)
}
}
impl<T, F> Eq for VarZeroSlice<T, F>
where
T: VarULE,
T: ?Sized,
T: Eq,
F: VarZeroVecFormat,
{
}
impl<T: VarULE + ?Sized + PartialOrd, F: VarZeroVecFormat> PartialOrd for VarZeroSlice<T, F> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.iter().partial_cmp(other.iter())
}
}
impl<T: VarULE + ?Sized + Ord, F: VarZeroVecFormat> Ord for VarZeroSlice<T, F> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.iter().cmp(other.iter())
}
}
impl<T: VarULE + ?Sized, F: VarZeroVecFormat> fmt::Debug for VarZeroSlice<T, F>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
impl<T: ?Sized, F: VarZeroVecFormat> AsRef<VarZeroSlice<T, F>> for VarZeroSlice<T, F> {
fn as_ref(&self) -> &VarZeroSlice<T, F> {
self
}
}