planus/vectors/vector.rs
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
use core::{marker::PhantomData, num::NonZeroUsize};
use crate::{
errors::{self, ErrorKind},
impls::array_from_buffer,
slice_helpers::SliceWithStartOffset,
traits::VectorRead,
TableRead,
};
/// A [`slice`]-like view into a serialized flatbuffer that deserializes on demand.
pub struct Vector<'buf, T: ?Sized> {
buffer: SliceWithStartOffset<'buf>,
len: usize,
_marker: PhantomData<&'buf T>,
}
impl<'buf, T: ?Sized> Copy for Vector<'buf, T> {}
impl<'buf, T: ?Sized> Clone for Vector<'buf, T> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<'buf, T: ?Sized> Vector<'buf, T> {
/// Returns an empty `Vector`
///
/// This is typically not very useful, since the vector is read-only, but
/// has uses for instance as a default value.
#[inline]
#[must_use]
pub const fn new_empty() -> Vector<'buf, T> {
Self {
buffer: SliceWithStartOffset {
buffer: &[],
offset_from_start: 0,
},
len: 0,
_marker: PhantomData,
}
}
/// Checks if the vector is empty.
#[inline]
#[must_use]
pub fn is_empty(self) -> bool {
self.len == 0
}
/// Returns the number of elements in the vector.
#[inline]
#[must_use]
pub fn len(self) -> usize {
self.len
}
}
impl<'buf, T: VectorRead<'buf>> Vector<'buf, T> {
/// Returns the first element of the `Vector`, or `None` if it is empty.
#[inline]
#[must_use]
pub fn first(self) -> Option<T> {
self.get(0)
}
/// Returns the last element of the `Vector`, or `None` if it is empty.
#[inline]
#[must_use]
pub fn last(self) -> Option<T> {
self.get(self.len().checked_sub(1)?)
}
/// Returns an element or sub-vector depending on the type of
/// index.
///
/// - If given a position, returns the element at that
/// position or `None` if out of bounds.
/// - If given a range, returns the sub-vector corresponding to that range,
/// or `None` if out of bounds.
#[inline]
#[must_use]
pub fn get<I>(self, index: I) -> Option<I::Output>
where
I: VectorIndex<'buf, T>,
{
index.get(self)
}
/// Returns an element or sub-vector, without doing bounds checking.
///
/// For a safe alternative see [`get`].
///
/// # Safety
///
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
/// even if the resulting output is not used.
///
/// [`get`]: Vector::get
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
#[inline]
#[must_use]
pub unsafe fn get_unchecked<I>(self, index: I) -> I::Output
where
I: VectorIndex<'buf, T>,
{
index.get_unchecked(self)
}
/// Returns an iterator over the vector.
#[inline]
#[must_use]
pub fn iter(self) -> super::Iter<'buf, T> {
super::Iter::new(self)
}
/// Returns an iterator over `chunk_size` elements of the [`Vector`] at a time, starting at the
/// beginning of the vector.
///
/// The chunks are [`Vector`]s themselves and do not overlap. If `chunk_size` does not
/// divide the length of the [`Vector`], then the last chunk will not have length `chunk_size`.
///
/// See [`chunks_exact`] for a variant of this iterator that returns chunks of always exactly
/// `chunk_size` elements, and [`rchunks`] for the same iterator but starting at the end of the
/// vector.
///
/// # Panics
///
/// Panics if `chunk_size` is 0.
///
/// [`chunks_exact`]: Vector::chunks_exact
/// [`rchunks`]: Vector::rchunks
#[inline]
#[must_use]
pub fn chunks(self, chunk_size: usize) -> super::Chunks<'buf, T> {
let chunk_size = NonZeroUsize::new(chunk_size).expect("chunks cannot have a size of zero");
super::Chunks::new(self, chunk_size)
}
/// Returns an iterator over `chunk_size` elements of the [`Vector`] at a time, starting at the end
/// of the vector.
///
/// The chunks are [`Vector`]s themselves and do not overlap. If `chunk_size` does not
/// divide the length of the [`Vector`], then the last chunk will not have length `chunk_size`.
///
/// See [`rchunks_exact`] for a variant of this iterator that returns chunks of always exactly
/// `chunk_size` elements, and [`chunks`] for the same iterator but starting at the beginning
/// of the vector.
///
/// # Panics
///
/// Panics if `chunk_size` is 0.
///
/// [`rchunks_exact`]: Vector::rchunks_exact
/// [`chunks`]: Vector::chunks
#[inline]
#[must_use]
pub fn rchunks(self, chunk_size: usize) -> super::RChunks<'buf, T> {
let chunk_size = NonZeroUsize::new(chunk_size).expect("chunks cannot have a size of zero");
super::RChunks::new(self, chunk_size)
}
/// Returns an iterator over `chunk_size` elements of the [`Vector`] at a time, starting at the
/// beginning of the vector.
///
/// The chunks are [`Vector`]s themselves and do not overlap. If `chunk_size` does not
/// divide the length of the vector, then the last up to `chunk_size-1` elements will
/// be omitted and can be retrieved from the `remainder` function of the iterator.
///
/// Due to each chunk having exactly `chunk_size` elements, the compiler can often optimize the
/// resulting code better than in the case of [`chunks`].
///
/// See [`chunks`] for a variant of this iterator that also returns the remainder as a smaller
/// chunk, and [`rchunks_exact`] for the same iterator but starting at the end of the vector.
///
/// # Panics
///
/// Panics if `chunk_size` is 0.
///
/// [`chunks`]: Vector::chunks
/// [`rchunks_exact`]: Vector::rchunks_exact
#[inline]
#[must_use]
pub fn chunks_exact(self, chunk_size: usize) -> super::ChunksExact<'buf, T> {
let chunk_size = NonZeroUsize::new(chunk_size).expect("chunks cannot have a size of zero");
super::ChunksExact::new(self, chunk_size)
}
/// Returns an iterator over `chunk_size` elements of the [`Vector`] at a time, starting at the
/// end of the vector.
///
/// The chunks are [`Vector`]s themselves and do not overlap. If `chunk_size` does not
/// divide the length of the vector, then the last up to `chunk_size-1` elements will
/// be omitted and can be retrieved from the `remainder` function of the iterator.
///
/// Due to each chunk having exactly `chunk_size` elements, the compiler can often optimize the
/// resulting code better than in the case of [`rchunks`].
///
/// See [`rchunks`] for a variant of this iterator that also returns the remainder as a smaller
/// chunk, and [`chunks_exact`] for the same iterator but starting at the beginning of the
/// vector.
///
/// # Panics
///
/// Panics if `chunk_size` is 0.
///
/// [`rchunks`]: Vector::rchunks
/// [`chunks_exact`]: Vector::chunks_exact
#[inline]
#[must_use]
pub fn rchunks_exact(self, chunk_size: usize) -> super::RChunksExact<'buf, T> {
let chunk_size = NonZeroUsize::new(chunk_size).expect("chunks cannot have a size of zero");
super::RChunksExact::new(self, chunk_size)
}
/// Returns an iterator over all contiguous windows of length
/// `size`. The windows overlap. If the vector is shorter than
/// `size`, the iterator returns no values.
///
/// # Panics
///
/// Panics if `size` is 0.
#[inline]
#[must_use]
pub fn windows(self, size: usize) -> super::Windows<'buf, T> {
let size = NonZeroUsize::new(size).expect("windows cannot have a size of zero");
super::Windows::new(self, size)
}
/// Returns the first and all the rest of the elements of the `Vector`, or `None` if it is empty
#[inline]
#[must_use]
pub fn split_first(self) -> Option<(T, Vector<'buf, T>)> {
if self.is_empty() {
None
} else {
Some(unsafe { (self.get_unchecked(0), self.get_unchecked(1..)) })
}
}
/// Returns the last and all the rest of the elements of the `Vector`, or `None` if it is empty
#[inline]
#[must_use]
pub fn split_last(self) -> Option<(T, Vector<'buf, T>)> {
if self.is_empty() {
None
} else {
Some(unsafe {
(
self.get_unchecked(self.len - 1),
self.get_unchecked(..self.len - 1),
)
})
}
}
/// Divides one `Vector` into two at an index.
///
/// The first will contain all indices from `[0, mid)` (excluding
/// the index `mid` itself) and the second will contain all
/// indices from `[mid, len)` (excluding the index `len` itself).
#[inline]
#[must_use]
pub fn split_at(self, mid: usize) -> Option<(Vector<'buf, T>, Vector<'buf, T>)> {
if mid <= self.len {
Some(unsafe { self.split_at_unchecked(mid) })
} else {
None
}
}
/// Divides one [`Vector`] into two at an index, without doing bounds checking.
///
/// The first will contain all indices from `[0, mid)` (excluding
/// the index `mid` itself) and the second will contain all
/// indices from `[mid, len)` (excluding the index `len` itself).
///
/// For a safe alternative see [`split_at`].
///
/// # Safety
///
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
/// even if the resulting output is not used. The caller has to ensure that
/// `0 <= mid <= self.len()`.
///
/// [`split_at`]: Vector::split_at
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
#[inline]
#[must_use]
pub unsafe fn split_at_unchecked(self, mid: usize) -> (Vector<'buf, T>, Vector<'buf, T>) {
(self.get_unchecked(..mid), self.get_unchecked(mid..))
}
}
impl<'buf, T: VectorRead<'buf>> Vector<'buf, T> {
/// Copies self into a new `Vec`.
pub fn to_vec<O>(self) -> crate::Result<alloc::vec::Vec<O>>
where
O: core::convert::TryFrom<T>,
crate::errors::Error: From<O::Error>,
{
self.iter()
.map(|v| O::try_from(v).map_err(crate::errors::Error::from))
.collect()
}
}
impl<'buf, T, E> Vector<'buf, core::result::Result<T, E>> {
/// Copies self into a new `Vec`.
pub fn to_vec_result<O>(self) -> crate::Result<alloc::vec::Vec<O>>
where
T: crate::traits::VectorReadInner<'buf>,
E: core::convert::From<T::Error>,
O: core::convert::TryFrom<T>,
crate::errors::Error: From<E> + From<O::Error>,
{
self.iter()
.map(|v| O::try_from(v?).map_err(|e| e.into()))
.collect()
}
}
impl<'buf, T: VectorRead<'buf>> IntoIterator for Vector<'buf, T> {
type Item = T;
type IntoIter = super::Iter<'buf, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'buf, T: VectorRead<'buf> + core::fmt::Debug> core::fmt::Debug for Vector<'buf, T>
where
T: core::fmt::Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_list().entries(*self).finish()
}
}
/// A helper trait used for indexing operations.
pub trait VectorIndex<'buf, T: VectorRead<'buf>>: private::Sealed {
/// The output type returned by methods.
type Output;
/// Returns a the output at this location, if in bounds.
fn get(self, vector: Vector<'buf, T>) -> Option<Self::Output>;
/// Returns a mutable reference to the output at this location, without
/// performing any bounds checking.
/// Calling this method with an out-of-bounds index is
/// *[undefined behavior]* even if the resulting output is not used.
///
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
unsafe fn get_unchecked(self, vector: Vector<'buf, T>) -> Self::Output;
}
/// Convert pair of `ops::Bound`s into `core::ops::Range` without performing any bounds checking and (in debug) overflow checking
fn into_range_unchecked(
len: usize,
(start, end): (core::ops::Bound<usize>, core::ops::Bound<usize>),
) -> core::ops::Range<usize> {
use core::ops::Bound;
let start = match start {
Bound::Included(i) => i,
Bound::Excluded(i) => i + 1,
Bound::Unbounded => 0,
};
let end = match end {
Bound::Included(i) => i + 1,
Bound::Excluded(i) => i,
Bound::Unbounded => len,
};
start..end
}
/// Convert pair of `core::ops::Bound`s into `core::ops::Range`.
/// Returns `None` on overflowing indices.
fn into_range(
len: usize,
(start, end): (core::ops::Bound<usize>, core::ops::Bound<usize>),
) -> Option<core::ops::Range<usize>> {
use core::ops::Bound;
let start = match start {
Bound::Included(start) => start,
Bound::Excluded(start) => start.checked_add(1)?,
Bound::Unbounded => 0,
};
let end = match end {
Bound::Included(end) => end.checked_add(1)?,
Bound::Excluded(end) => end,
Bound::Unbounded => len,
};
// Don't bother with checking `start < end` and `end <= len`
// since these checks are handled by `Range` impls
Some(start..end)
}
impl<'buf, T: VectorRead<'buf>> VectorIndex<'buf, T>
for (core::ops::Bound<usize>, core::ops::Bound<usize>)
{
type Output = Vector<'buf, T>;
#[inline]
fn get(self, vector: Vector<'buf, T>) -> Option<Self::Output> {
into_range(vector.len, self)?.get(vector)
}
#[inline]
unsafe fn get_unchecked(self, vector: Vector<'buf, T>) -> Self::Output {
into_range_unchecked(vector.len, self).get_unchecked(vector)
}
}
impl<'buf, T: VectorRead<'buf>> VectorIndex<'buf, T> for usize {
type Output = T;
#[inline]
fn get(self, vector: Vector<'buf, T>) -> Option<Self::Output> {
if self < vector.len {
Some(unsafe { self.get_unchecked(vector) })
} else {
None
}
}
#[inline]
unsafe fn get_unchecked(self, vector: Vector<'buf, T>) -> Self::Output {
debug_assert!(self < vector.len);
debug_assert!(vector.len.checked_mul(T::STRIDE).unwrap() <= vector.buffer.len());
T::from_buffer(vector.buffer, T::STRIDE * self)
}
}
impl<'buf, T: VectorRead<'buf>> VectorIndex<'buf, T> for core::ops::Range<usize> {
type Output = Vector<'buf, T>;
#[inline]
fn get(self, vector: Vector<'buf, T>) -> Option<Self::Output> {
if self.start > self.end || self.end > vector.len {
None
} else {
// SAFETY: `self` is checked to be valid and in bounds above.
unsafe { Some(self.get_unchecked(vector)) }
}
}
#[inline]
unsafe fn get_unchecked(self, vector: Vector<'buf, T>) -> Self::Output {
debug_assert!(self.start <= self.end);
debug_assert!(self.end <= vector.len);
Vector {
buffer: vector
.buffer
.advance(T::STRIDE * self.start)
.expect("IMPOSSIBLE: the length was checked on creation"),
len: self.end - self.start,
_marker: PhantomData,
}
}
}
impl<'buf, T: VectorRead<'buf>> VectorIndex<'buf, T> for core::ops::RangeFrom<usize> {
type Output = Vector<'buf, T>;
#[inline]
fn get(self, vector: Vector<'buf, T>) -> Option<Self::Output> {
(self.start..vector.len).get(vector)
}
#[inline]
unsafe fn get_unchecked(self, vector: Vector<'buf, T>) -> Self::Output {
(self.start..vector.len).get_unchecked(vector)
}
}
impl<'buf, T: VectorRead<'buf>> VectorIndex<'buf, T> for core::ops::RangeFull {
type Output = Vector<'buf, T>;
#[inline]
fn get(self, vector: Vector<'buf, T>) -> Option<Self::Output> {
Some(vector)
}
#[inline]
unsafe fn get_unchecked(self, vector: Vector<'buf, T>) -> Self::Output {
vector
}
}
impl<'buf, T: VectorRead<'buf>> VectorIndex<'buf, T> for core::ops::RangeInclusive<usize> {
type Output = Vector<'buf, T>;
#[inline]
fn get(self, vector: Vector<'buf, T>) -> Option<Self::Output> {
(*self.start()..self.end().checked_add(1)?).get(vector)
}
#[inline]
unsafe fn get_unchecked(self, vector: Vector<'buf, T>) -> Self::Output {
(*self.start()..self.end() + 1).get_unchecked(vector)
}
}
impl<'buf, T: VectorRead<'buf>> VectorIndex<'buf, T> for core::ops::RangeTo<usize> {
type Output = Vector<'buf, T>;
#[inline]
fn get(self, vector: Vector<'buf, T>) -> Option<Self::Output> {
(0..self.end).get(vector)
}
#[inline]
unsafe fn get_unchecked(self, vector: Vector<'buf, T>) -> Self::Output {
(0..self.end).get_unchecked(vector)
}
}
impl<'buf, T: VectorRead<'buf>> VectorIndex<'buf, T> for core::ops::RangeToInclusive<usize> {
type Output = Vector<'buf, T>;
#[inline]
fn get(self, vector: Vector<'buf, T>) -> Option<Self::Output> {
(0..=self.end).get(vector)
}
#[inline]
unsafe fn get_unchecked(self, vector: Vector<'buf, T>) -> Self::Output {
(0..=self.end).get_unchecked(vector)
}
}
impl<'buf, T: VectorRead<'buf>, O> TryFrom<Vector<'buf, T>> for alloc::vec::Vec<O>
where
O: core::convert::TryFrom<T>,
errors::Error: From<O::Error>,
{
type Error = crate::errors::Error;
fn try_from(value: Vector<'buf, T>) -> Result<Self, Self::Error> {
value
.iter()
.map(|v| O::try_from(v).map_err(errors::Error::from))
.collect()
}
}
impl<'buf, T: ?Sized + VectorRead<'buf>> TableRead<'buf> for Vector<'buf, T> {
fn from_buffer(
buffer: SliceWithStartOffset<'buf>,
offset: usize,
) -> core::result::Result<Self, ErrorKind> {
let (buffer, len) = array_from_buffer(buffer, offset)?;
if len.checked_mul(T::STRIDE).ok_or(ErrorKind::InvalidLength)? <= buffer.len() {
Ok(Vector {
buffer,
len,
_marker: PhantomData,
})
} else {
Err(ErrorKind::InvalidLength)
}
}
}
mod private {
pub trait Sealed {}
// Implement for those same types, but no others.
impl Sealed for (core::ops::Bound<usize>, core::ops::Bound<usize>) {}
impl Sealed for usize {}
impl Sealed for core::ops::Range<usize> {}
impl Sealed for core::ops::RangeFrom<usize> {}
impl Sealed for core::ops::RangeFull {}
impl Sealed for core::ops::RangeInclusive<usize> {}
impl Sealed for core::ops::RangeTo<usize> {}
impl Sealed for core::ops::RangeToInclusive<usize> {}
}