| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 | #![allow(clippy::ptr_eq)]
|
| 7 |
|
| 8 | use super::{
|
| 9 | allocator::{KVmalloc, Kmalloc, Vmalloc},
|
| 10 | layout::ArrayLayout,
|
| 11 | AllocError, Allocator, Box, Flags,
|
| 12 | };
|
| 13 | use core::{
|
| 14 | fmt,
|
| 15 | marker::PhantomData,
|
| 16 | mem::{ManuallyDrop, MaybeUninit},
|
| 17 | ops::Deref,
|
| 18 | ops::DerefMut,
|
| 19 | ops::Index,
|
| 20 | ops::IndexMut,
|
| 21 | ptr,
|
| 22 | ptr::NonNull,
|
| 23 | slice,
|
| 24 | slice::SliceIndex,
|
| 25 | };
|
| 26 |
|
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | #[macro_export]
|
| 49 | macro_rules! kvec {
|
| 50 | () => (
|
| 51 | $crate::alloc::KVec::new()
|
| 52 | );
|
| 53 | ($elem:expr; $n:expr) => (
|
| 54 | $crate::alloc::KVec::from_elem($elem, $n, GFP_KERNEL)
|
| 55 | );
|
| 56 | ($($x:expr),+ $(,)?) => (
|
| 57 | match $crate::alloc::KBox::new_uninit(GFP_KERNEL) {
|
| 58 | Ok(b) => Ok($crate::alloc::KVec::from($crate::alloc::KBox::write(b, [$($x),+]))),
|
| 59 | Err(e) => Err(e),
|
| 60 | }
|
| 61 | );
|
| 62 | }
|
| 63 |
|
| 64 | /// The kernel's [`Vec`] type.
|
| 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 | pub struct Vec<T, A: Allocator> {
|
| 99 | ptr: NonNull<T>,
|
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | layout: ArrayLayout<T>,
|
| 105 | len: usize,
|
| 106 | _p: PhantomData<A>,
|
| 107 | }
|
| 108 |
|
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | pub type KVec<T> = Vec<T, Kmalloc>;
|
| 121 |
|
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | pub type VVec<T> = Vec<T, Vmalloc>;
|
| 134 |
|
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | pub type KVVec<T> = Vec<T, KVmalloc>;
|
| 147 |
|
| 148 | |
| 149 | unsafe impl<T, A> Send for Vec<T, A>
|
| 150 | where
|
| 151 | T: Send,
|
| 152 | A: Allocator,
|
| 153 | {
|
| 154 | }
|
| 155 |
|
| 156 | |
| 157 | unsafe impl<T, A> Sync for Vec<T, A>
|
| 158 | where
|
| 159 | T: Sync,
|
| 160 | A: Allocator,
|
| 161 | {
|
| 162 | }
|
| 163 |
|
| 164 | impl<T, A> Vec<T, A>
|
| 165 | where
|
| 166 | A: Allocator,
|
| 167 | {
|
| 168 | #[inline]
|
| 169 | const fn is_zst() -> bool {
|
| 170 | core::mem::size_of::<T>() == 0
|
| 171 | }
|
| 172 |
|
| 173 | |
| 174 | |
| 175 | pub fn capacity(&self) -> usize {
|
| 176 | if const { Self::is_zst() } {
|
| 177 | usize::MAX
|
| 178 | } else {
|
| 179 | self.layout.len()
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | |
| 184 | #[inline]
|
| 185 | pub fn len(&self) -> usize {
|
| 186 | self.len
|
| 187 | }
|
| 188 |
|
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | #[inline]
|
| 197 | pub unsafe fn set_len(&mut self, new_len: usize) {
|
| 198 | debug_assert!(new_len <= self.capacity());
|
| 199 | self.len = new_len;
|
| 200 | }
|
| 201 |
|
| 202 | |
| 203 | #[inline]
|
| 204 | pub fn as_slice(&self) -> &[T] {
|
| 205 | self
|
| 206 | }
|
| 207 |
|
| 208 | |
| 209 | #[inline]
|
| 210 | pub fn as_mut_slice(&mut self) -> &mut [T] {
|
| 211 | self
|
| 212 | }
|
| 213 |
|
| 214 | |
| 215 | |
| 216 | #[inline]
|
| 217 | pub fn as_mut_ptr(&mut self) -> *mut T {
|
| 218 | self.ptr.as_ptr()
|
| 219 | }
|
| 220 |
|
| 221 | |
| 222 | |
| 223 | #[inline]
|
| 224 | pub fn as_ptr(&self) -> *const T {
|
| 225 | self.ptr.as_ptr()
|
| 226 | }
|
| 227 |
|
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | #[inline]
|
| 240 | pub fn is_empty(&self) -> bool {
|
| 241 | self.len() == 0
|
| 242 | }
|
| 243 |
|
| 244 | |
| 245 | |
| 246 | |
| 247 | #[inline]
|
| 248 | pub const fn new() -> Self {
|
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | Self {
|
| 255 | ptr: NonNull::dangling(),
|
| 256 | layout: ArrayLayout::empty(),
|
| 257 | len: 0,
|
| 258 | _p: PhantomData::<A>,
|
| 259 | }
|
| 260 | }
|
| 261 |
|
| 262 | |
| 263 | pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
|
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | let ptr = unsafe { self.as_mut_ptr().add(self.len) } as *mut MaybeUninit<T>;
|
| 269 |
|
| 270 | |
| 271 | |
| 272 | unsafe { slice::from_raw_parts_mut(ptr, self.capacity() - self.len) }
|
| 273 | }
|
| 274 |
|
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {
|
| 289 | self.reserve(1, flags)?;
|
| 290 |
|
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | let ptr = unsafe { self.as_mut_ptr().add(self.len) };
|
| 296 |
|
| 297 | |
| 298 | |
| 299 | unsafe { core::ptr::write(ptr, v) };
|
| 300 |
|
| 301 | |
| 302 | |
| 303 | |
| 304 | unsafe { self.set_len(self.len() + 1) };
|
| 305 | Ok(())
|
| 306 | }
|
| 307 |
|
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | pub fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError> {
|
| 319 | let mut v = Vec::new();
|
| 320 |
|
| 321 | v.reserve(capacity, flags)?;
|
| 322 |
|
| 323 | Ok(v)
|
| 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 | pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
|
| 368 | let layout = if Self::is_zst() {
|
| 369 | ArrayLayout::empty()
|
| 370 | } else {
|
| 371 | |
| 372 | |
| 373 | unsafe { ArrayLayout::new_unchecked(capacity) }
|
| 374 | };
|
| 375 |
|
| 376 | |
| 377 | |
| 378 | Self {
|
| 379 | |
| 380 | |
| 381 | ptr: unsafe { NonNull::new_unchecked(ptr) },
|
| 382 | layout,
|
| 383 | len: length,
|
| 384 | _p: PhantomData::<A>,
|
| 385 | }
|
| 386 | }
|
| 387 |
|
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | pub fn into_raw_parts(self) -> (*mut T, usize, usize) {
|
| 394 | let mut me = ManuallyDrop::new(self);
|
| 395 | let len = me.len();
|
| 396 | let capacity = me.capacity();
|
| 397 | let ptr = me.as_mut_ptr();
|
| 398 | (ptr, len, capacity)
|
| 399 | }
|
| 400 |
|
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | pub fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError> {
|
| 420 | let len = self.len();
|
| 421 | let cap = self.capacity();
|
| 422 |
|
| 423 | if cap - len >= additional {
|
| 424 | return Ok(());
|
| 425 | }
|
| 426 |
|
| 427 | if Self::is_zst() {
|
| 428 | |
| 429 | return Err(AllocError);
|
| 430 | }
|
| 431 |
|
| 432 | |
| 433 | |
| 434 | let new_cap = core::cmp::max(cap * 2, len.checked_add(additional).ok_or(AllocError)?);
|
| 435 | let layout = ArrayLayout::new(new_cap).map_err(|_| AllocError)?;
|
| 436 |
|
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | let ptr = unsafe {
|
| 442 | A::realloc(
|
| 443 | Some(self.ptr.cast()),
|
| 444 | layout.into(),
|
| 445 | self.layout.into(),
|
| 446 | flags,
|
| 447 | )?
|
| 448 | };
|
| 449 |
|
| 450 | |
| 451 | |
| 452 | |
| 453 | self.ptr = ptr.cast();
|
| 454 | self.layout = layout;
|
| 455 |
|
| 456 | Ok(())
|
| 457 | }
|
| 458 | }
|
| 459 |
|
| 460 | impl<T: Clone, A: Allocator> Vec<T, A> {
|
| 461 | |
| 462 | pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), AllocError> {
|
| 463 | if n == 0 {
|
| 464 | return Ok(());
|
| 465 | }
|
| 466 |
|
| 467 | self.reserve(n, flags)?;
|
| 468 |
|
| 469 | let spare = self.spare_capacity_mut();
|
| 470 |
|
| 471 | for item in spare.iter_mut().take(n - 1) {
|
| 472 | item.write(value.clone());
|
| 473 | }
|
| 474 |
|
| 475 | |
| 476 | spare[n - 1].write(value);
|
| 477 |
|
| 478 | |
| 479 | |
| 480 | |
| 481 | unsafe { self.set_len(self.len() + n) };
|
| 482 |
|
| 483 | Ok(())
|
| 484 | }
|
| 485 |
|
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | pub fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), AllocError> {
|
| 502 | self.reserve(other.len(), flags)?;
|
| 503 | for (slot, item) in core::iter::zip(self.spare_capacity_mut(), other) {
|
| 504 | slot.write(item.clone());
|
| 505 | }
|
| 506 |
|
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | unsafe { self.set_len(self.len() + other.len()) };
|
| 513 | Ok(())
|
| 514 | }
|
| 515 |
|
| 516 | |
| 517 | pub fn from_elem(value: T, n: usize, flags: Flags) -> Result<Self, AllocError> {
|
| 518 | let mut v = Self::with_capacity(n, flags)?;
|
| 519 |
|
| 520 | v.extend_with(n, value, flags)?;
|
| 521 |
|
| 522 | Ok(v)
|
| 523 | }
|
| 524 | }
|
| 525 |
|
| 526 | impl<T, A> Drop for Vec<T, A>
|
| 527 | where
|
| 528 | A: Allocator,
|
| 529 | {
|
| 530 | fn drop(&mut self) {
|
| 531 | |
| 532 | unsafe {
|
| 533 | ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut(
|
| 534 | self.as_mut_ptr(),
|
| 535 | self.len,
|
| 536 | ))
|
| 537 | };
|
| 538 |
|
| 539 | |
| 540 | |
| 541 | |
| 542 | unsafe { A::free(self.ptr.cast(), self.layout.into()) };
|
| 543 | }
|
| 544 | }
|
| 545 |
|
| 546 | impl<T, A, const N: usize> From<Box<[T; N], A>> for Vec<T, A>
|
| 547 | where
|
| 548 | A: Allocator,
|
| 549 | {
|
| 550 | fn from(b: Box<[T; N], A>) -> Vec<T, A> {
|
| 551 | let len = b.len();
|
| 552 | let ptr = Box::into_raw(b);
|
| 553 |
|
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | unsafe { Vec::from_raw_parts(ptr as _, len, len) }
|
| 561 | }
|
| 562 | }
|
| 563 |
|
| 564 | impl<T> Default for KVec<T> {
|
| 565 | #[inline]
|
| 566 | fn default() -> Self {
|
| 567 | Self::new()
|
| 568 | }
|
| 569 | }
|
| 570 |
|
| 571 | impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> {
|
| 572 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 573 | fmt::Debug::fmt(&**self, f)
|
| 574 | }
|
| 575 | }
|
| 576 |
|
| 577 | impl<T, A> Deref for Vec<T, A>
|
| 578 | where
|
| 579 | A: Allocator,
|
| 580 | {
|
| 581 | type Target = [T];
|
| 582 |
|
| 583 | #[inline]
|
| 584 | fn deref(&self) -> &[T] {
|
| 585 | |
| 586 | |
| 587 | unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
|
| 588 | }
|
| 589 | }
|
| 590 |
|
| 591 | impl<T, A> DerefMut for Vec<T, A>
|
| 592 | where
|
| 593 | A: Allocator,
|
| 594 | {
|
| 595 | #[inline]
|
| 596 | fn deref_mut(&mut self) -> &mut [T] {
|
| 597 | |
| 598 | |
| 599 | unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
|
| 600 | }
|
| 601 | }
|
| 602 |
|
| 603 | impl<T: Eq, A> Eq for Vec<T, A> where A: Allocator {}
|
| 604 |
|
| 605 | impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A>
|
| 606 | where
|
| 607 | A: Allocator,
|
| 608 | {
|
| 609 | type Output = I::Output;
|
| 610 |
|
| 611 | #[inline]
|
| 612 | fn index(&self, index: I) -> &Self::Output {
|
| 613 | Index::index(&**self, index)
|
| 614 | }
|
| 615 | }
|
| 616 |
|
| 617 | impl<T, I: SliceIndex<[T]>, A> IndexMut<I> for Vec<T, A>
|
| 618 | where
|
| 619 | A: Allocator,
|
| 620 | {
|
| 621 | #[inline]
|
| 622 | fn index_mut(&mut self, index: I) -> &mut Self::Output {
|
| 623 | IndexMut::index_mut(&mut **self, index)
|
| 624 | }
|
| 625 | }
|
| 626 |
|
| 627 | macro_rules! impl_slice_eq {
|
| 628 | ($([$($vars:tt)*] $lhs:ty, $rhs:ty,)*) => {
|
| 629 | $(
|
| 630 | impl<T, U, $($vars)*> PartialEq<$rhs> for $lhs
|
| 631 | where
|
| 632 | T: PartialEq<U>,
|
| 633 | {
|
| 634 | #[inline]
|
| 635 | fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] }
|
| 636 | }
|
| 637 | )*
|
| 638 | }
|
| 639 | }
|
| 640 |
|
| 641 | impl_slice_eq! {
|
| 642 | [A1: Allocator, A2: Allocator] Vec<T, A1>, Vec<U, A2>,
|
| 643 | [A: Allocator] Vec<T, A>, &[U],
|
| 644 | [A: Allocator] Vec<T, A>, &mut [U],
|
| 645 | [A: Allocator] &[T], Vec<U, A>,
|
| 646 | [A: Allocator] &mut [T], Vec<U, A>,
|
| 647 | [A: Allocator] Vec<T, A>, [U],
|
| 648 | [A: Allocator] [T], Vec<U, A>,
|
| 649 | [A: Allocator, const N: usize] Vec<T, A>, [U; N],
|
| 650 | [A: Allocator, const N: usize] Vec<T, A>, &[U; N],
|
| 651 | }
|
| 652 |
|
| 653 | impl<'a, T, A> IntoIterator for &'a Vec<T, A>
|
| 654 | where
|
| 655 | A: Allocator,
|
| 656 | {
|
| 657 | type Item = &'a T;
|
| 658 | type IntoIter = slice::Iter<'a, T>;
|
| 659 |
|
| 660 | fn into_iter(self) -> Self::IntoIter {
|
| 661 | self.iter()
|
| 662 | }
|
| 663 | }
|
| 664 |
|
| 665 | impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A>
|
| 666 | where
|
| 667 | A: Allocator,
|
| 668 | {
|
| 669 | type Item = &'a mut T;
|
| 670 | type IntoIter = slice::IterMut<'a, T>;
|
| 671 |
|
| 672 | fn into_iter(self) -> Self::IntoIter {
|
| 673 | self.iter_mut()
|
| 674 | }
|
| 675 | }
|
| 676 |
|
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | pub struct IntoIter<T, A: Allocator> {
|
| 691 | ptr: *mut T,
|
| 692 | buf: NonNull<T>,
|
| 693 | len: usize,
|
| 694 | layout: ArrayLayout<T>,
|
| 695 | _p: PhantomData<A>,
|
| 696 | }
|
| 697 |
|
| 698 | impl<T, A> IntoIter<T, A>
|
| 699 | where
|
| 700 | A: Allocator,
|
| 701 | {
|
| 702 | fn into_raw_parts(self) -> (*mut T, NonNull<T>, usize, usize) {
|
| 703 | let me = ManuallyDrop::new(self);
|
| 704 | let ptr = me.ptr;
|
| 705 | let buf = me.buf;
|
| 706 | let len = me.len;
|
| 707 | let cap = me.layout.len();
|
| 708 | (ptr, buf, len, cap)
|
| 709 | }
|
| 710 |
|
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | pub fn collect(self, flags: Flags) -> Vec<T, A> {
|
| 747 | let old_layout = self.layout;
|
| 748 | let (mut ptr, buf, len, mut cap) = self.into_raw_parts();
|
| 749 | let has_advanced = ptr != buf.as_ptr();
|
| 750 |
|
| 751 | if has_advanced {
|
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | unsafe { ptr::copy(ptr, buf.as_ptr(), len) };
|
| 761 | ptr = buf.as_ptr();
|
| 762 |
|
| 763 | |
| 764 | let layout = unsafe { ArrayLayout::<T>::new_unchecked(len) };
|
| 765 |
|
| 766 | |
| 767 | |
| 768 | |
| 769 | ptr = match unsafe {
|
| 770 | A::realloc(Some(buf.cast()), layout.into(), old_layout.into(), flags)
|
| 771 | } {
|
| 772 | |
| 773 | |
| 774 | Err(_) => ptr,
|
| 775 | Ok(ptr) => {
|
| 776 | cap = len;
|
| 777 | ptr.as_ptr().cast()
|
| 778 | }
|
| 779 | };
|
| 780 | }
|
| 781 |
|
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | unsafe { Vec::from_raw_parts(ptr, len, cap) }
|
| 790 | }
|
| 791 | }
|
| 792 |
|
| 793 | impl<T, A> Iterator for IntoIter<T, A>
|
| 794 | where
|
| 795 | A: Allocator,
|
| 796 | {
|
| 797 | type Item = T;
|
| 798 |
|
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | fn next(&mut self) -> Option<T> {
|
| 813 | if self.len == 0 {
|
| 814 | return None;
|
| 815 | }
|
| 816 |
|
| 817 | let current = self.ptr;
|
| 818 |
|
| 819 | |
| 820 | |
| 821 | unsafe { self.ptr = self.ptr.add(1) };
|
| 822 |
|
| 823 | self.len -= 1;
|
| 824 |
|
| 825 | |
| 826 | Some(unsafe { current.read() })
|
| 827 | }
|
| 828 |
|
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | fn size_hint(&self) -> (usize, Option<usize>) {
|
| 848 | (self.len, Some(self.len))
|
| 849 | }
|
| 850 | }
|
| 851 |
|
| 852 | impl<T, A> Drop for IntoIter<T, A>
|
| 853 | where
|
| 854 | A: Allocator,
|
| 855 | {
|
| 856 | fn drop(&mut self) {
|
| 857 | |
| 858 | unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.ptr, self.len)) };
|
| 859 |
|
| 860 | |
| 861 | |
| 862 | |
| 863 | unsafe { A::free(self.buf.cast(), self.layout.into()) };
|
| 864 | }
|
| 865 | }
|
| 866 |
|
| 867 | impl<T, A> IntoIterator for Vec<T, A>
|
| 868 | where
|
| 869 | A: Allocator,
|
| 870 | {
|
| 871 | type Item = T;
|
| 872 | type IntoIter = IntoIter<T, A>;
|
| 873 |
|
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | #[inline]
|
| 903 | fn into_iter(self) -> Self::IntoIter {
|
| 904 | let buf = self.ptr;
|
| 905 | let layout = self.layout;
|
| 906 | let (ptr, len, _) = self.into_raw_parts();
|
| 907 |
|
| 908 | IntoIter {
|
| 909 | ptr,
|
| 910 | buf,
|
| 911 | len,
|
| 912 | layout,
|
| 913 | _p: PhantomData::<A>,
|
| 914 | }
|
| 915 | }
|
| 916 | }
|