@@ -64,10 +64,20 @@ pub struct VecInner<B: ?Sized + VecDrop> {
64
64
/// }
65
65
/// assert_eq!(*vec, [7, 1, 2, 3]);
66
66
/// ```
67
+ ///
68
+ /// In some cases, the const-generic might be cumbersome. `Vec` can coerce into a [`VecView`][] to remove the need for the const-generic:
69
+ ///
70
+ /// ```rust
71
+ /// use heapless::{Vec, VecView};
72
+ ///
73
+ /// let vec: Vec<u8, 10> = Vec::from_slice(&[1, 2, 3, 4]).unwrap();
74
+ /// let view: &VecView<_> = &vec;
75
+ /// ```
67
76
pub type Vec < T , const N : usize > = VecInner < [ MaybeUninit < T > ; N ] > ;
77
+
68
78
/// A Vec with dynamic capacity
69
79
///
70
- /// [`Vec`]() coerces to `VecView`. `VecView` is !Sized, meaning that it can only ever be used through pointer
80
+ /// [`Vec`]() coerces to `VecView`. `VecView` is ` !Sized` , meaning that it can only ever be used through pointer
71
81
///
72
82
/// Unlike [`Vec`](), `VecView` does not have an `N` const-generic parameter.
73
83
/// This has the ergonomic advantages of making it possible to use functions without needing to know at
@@ -889,11 +899,29 @@ impl<T, const N: usize> Vec<T, N> {
889
899
}
890
900
891
901
/// Get a reference to the Vec, erasing the `N` const-generic
902
+ ///
903
+ /// This can also be used through type coerction, since `Vec<T, N>` implements `Unsize<VecView<T>>`:
904
+ ///
905
+ /// ```rust
906
+ /// use heapless::{Vec, VecView};
907
+ ///
908
+ /// let vec: Vec<u8, 10> = Vec::from_slice(&[1, 2, 3, 4]).unwrap();
909
+ /// let view: &VecView<_> = &vec;
910
+ /// ```
892
911
pub const fn as_view ( & self ) -> & VecView < T > {
893
912
self
894
913
}
895
914
896
915
/// Get a `mut` reference to the Vec, erasing the `N` const-generic
916
+ ///
917
+ /// This can also be used through type coerction, since `Vec<T, N>` implements `Unsize<VecView<T>>`:
918
+ ///
919
+ /// ```rust
920
+ /// use heapless::{Vec, VecView};
921
+ ///
922
+ /// let mut vec: Vec<u8, 10> = Vec::from_slice(&[1, 2, 3, 4]).unwrap();
923
+ /// let view: &mut VecView<_> = &mut vec;
924
+ /// ```
897
925
pub fn as_mut_view ( & mut self ) -> & mut VecView < T > {
898
926
self
899
927
}
0 commit comments