Skip to content

Commit add12d7

Browse files
Improve docs
1 parent 0af5f17 commit add12d7

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/vec.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,20 @@ pub struct VecInner<B: ?Sized + VecDrop> {
6464
/// }
6565
/// assert_eq!(*vec, [7, 1, 2, 3]);
6666
/// ```
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+
/// ```
6776
pub type Vec<T, const N: usize> = VecInner<[MaybeUninit<T>; N]>;
77+
6878
/// A Vec with dynamic capacity
6979
///
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
7181
///
7282
/// Unlike [`Vec`](), `VecView` does not have an `N` const-generic parameter.
7383
/// 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> {
889899
}
890900

891901
/// 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+
/// ```
892911
pub const fn as_view(&self) -> &VecView<T> {
893912
self
894913
}
895914

896915
/// 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+
/// ```
897925
pub fn as_mut_view(&mut self) -> &mut VecView<T> {
898926
self
899927
}

0 commit comments

Comments
 (0)