@@ -528,6 +528,65 @@ impl<A: Array> TinyVec<A> {
528
528
TinyVec :: Heap ( Vec :: with_capacity ( cap) )
529
529
}
530
530
}
531
+
532
+ /// Converts a `TinyVec<[T; N]>` into a `Box<[T]>`.
533
+ ///
534
+ /// - For `TinyVec::Heap(Vec<T>)`, it takes the `Vec<T>` and converts it into
535
+ /// a `Box<[T]>` without heap reallocation.
536
+ /// - For `TinyVec::Inline(inner_data)`, it first converts the `inner_data` to
537
+ /// `Vec<T>`, then into a `Box<[T]>`. Requiring only a single heap
538
+ /// allocation.
539
+ ///
540
+ /// ## Example
541
+ ///
542
+ /// ```
543
+ /// use core::mem::size_of_val as mem_size_of;
544
+ /// use tinyvec::TinyVec;
545
+ ///
546
+ /// // Initialize TinyVec with 256 elements (exceeding inline capacity)
547
+ /// let v: TinyVec<[_; 128]> = (0u8..=255).collect();
548
+ ///
549
+ /// assert!(v.is_heap());
550
+ /// assert_eq!(mem_size_of(&v), 136); // mem size of TinyVec<[u8; N]>: N+8
551
+ /// assert_eq!(v.len(), 256);
552
+ ///
553
+ /// let boxed = v.into_boxed_slice();
554
+ /// assert_eq!(mem_size_of(&boxed), 16); // mem size of Box<[u8]>: 16 bytes (fat pointer)
555
+ /// assert_eq!(boxed.len(), 256);
556
+ /// ```
557
+ #[ inline]
558
+ #[ must_use]
559
+ pub fn into_boxed_slice ( self ) -> alloc:: boxed:: Box < [ A :: Item ] > {
560
+ self . into_vec ( ) . into_boxed_slice ( )
561
+ }
562
+
563
+ /// Converts a `TinyVec<[T; N]>` into a `Vec<T>`.
564
+ ///
565
+ /// `v.into_vec()` is equivalent to `Into::<Vec<_>>::into(v)`.
566
+ ///
567
+ /// - For `TinyVec::Inline(_)`, `.into_vec()` **does not** offer a performance
568
+ /// advantage over `.to_vec()`.
569
+ /// - For `TinyVec::Heap(vec_data)`, `.into_vec()` will take `vec_data`
570
+ /// without heap reallocation.
571
+ ///
572
+ /// ## Example
573
+ ///
574
+ /// ```
575
+ /// use tinyvec::TinyVec;
576
+ ///
577
+ /// let v = TinyVec::from([0u8; 8]);
578
+ /// let v2 = v.clone();
579
+ ///
580
+ /// let vec = v.into_vec();
581
+ /// let vec2: Vec<_> = v2.into();
582
+ ///
583
+ /// assert_eq!(vec, vec2);
584
+ /// ```
585
+ #[ inline]
586
+ #[ must_use]
587
+ pub fn into_vec ( self ) -> Vec < A :: Item > {
588
+ self . into ( )
589
+ }
531
590
}
532
591
533
592
impl < A : Array > TinyVec < A > {
@@ -1332,6 +1391,61 @@ impl<A: Array> FromIterator<A::Item> for TinyVec<A> {
1332
1391
}
1333
1392
}
1334
1393
1394
+ impl < A : Array > Into < Vec < A :: Item > > for TinyVec < A > {
1395
+ /// Converts a `TinyVec` into a `Vec`.
1396
+ ///
1397
+ /// ## Examples
1398
+ ///
1399
+ /// ### Inline to Vec
1400
+ ///
1401
+ /// For `TinyVec::Inline(_)`,
1402
+ /// `.into()` **does not** offer a performance advantage over `.to_vec()`.
1403
+ ///
1404
+ /// ```
1405
+ /// use core::mem::size_of_val as mem_size_of;
1406
+ /// use tinyvec::TinyVec;
1407
+ ///
1408
+ /// let v = TinyVec::from([0u8; 128]);
1409
+ /// assert_eq!(mem_size_of(&v), 136);
1410
+ ///
1411
+ /// let vec: Vec<_> = v.into();
1412
+ /// assert_eq!(mem_size_of(&vec), 24);
1413
+ /// ```
1414
+ ///
1415
+ /// ### Heap into Vec
1416
+ ///
1417
+ /// For `TinyVec::Heap(vec_data)`,
1418
+ /// `.into()` will take `vec_data` without heap reallocation.
1419
+ ///
1420
+ /// ```
1421
+ /// use core::{
1422
+ /// any::type_name_of_val as type_of, mem::size_of_val as mem_size_of,
1423
+ /// };
1424
+ /// use tinyvec::TinyVec;
1425
+ ///
1426
+ /// const fn from_heap<T: Default>(owned: Vec<T>) -> TinyVec<[T; 1]> {
1427
+ /// TinyVec::Heap(owned)
1428
+ /// }
1429
+ ///
1430
+ /// let v = from_heap(vec![0u8; 128]);
1431
+ /// assert_eq!(v.len(), 128);
1432
+ /// assert_eq!(mem_size_of(&v), 24);
1433
+ /// assert!(type_of(&v).ends_with("TinyVec<[u8; 1]>"));
1434
+ ///
1435
+ /// let vec: Vec<_> = v.into();
1436
+ /// assert_eq!(mem_size_of(&vec), 24);
1437
+ /// assert!(type_of(&vec).ends_with("Vec<u8>"));
1438
+ /// ```
1439
+ #[ inline]
1440
+ #[ must_use]
1441
+ fn into ( self ) -> Vec < A :: Item > {
1442
+ match self {
1443
+ Self :: Heap ( inner) => inner,
1444
+ Self :: Inline ( mut inner) => inner. drain_to_vec ( ) ,
1445
+ }
1446
+ }
1447
+ }
1448
+
1335
1449
/// Iterator for consuming an `TinyVec` and returning owned elements.
1336
1450
#[ cfg_attr( docsrs, doc( cfg( feature = "alloc" ) ) ) ]
1337
1451
pub enum TinyVecIterator < A : Array > {
0 commit comments