|
| 1 | ++++ |
| 2 | +title = "Heapless v0.9.1 has been released!" |
| 3 | +date = 2025-08-20 |
| 4 | +draft = false |
| 5 | +in_search_index = true |
| 6 | +template = "page.html" |
| 7 | ++++ |
| 8 | + |
| 9 | +# Heapless `0.9.1` released |
| 10 | + |
| 11 | +Almost 2 years after the last release, the [heapless](https://github.com/rust-embedded/heapless). The first attempt at a `0.9.0` release was yanked, due to including more breaking changes than intended. This has been fixed, and `0.9.1` has been released today. |
| 12 | + |
| 13 | +Compared to `0.8.0`, the `0.9.1` release contains a bunch of small everyday improvements and bugfixes. Most users of the library should be able to adapt with minimal changes. For more information, you can check out [the changelog](https://github.com/rust-embedded/heapless/blob/main/CHANGELOG.md). Here are some of the major changes that can improve your usage of the library. |
| 14 | + |
| 15 | +<!-- more --> |
| 16 | + |
| 17 | +# The `View` types |
| 18 | + |
| 19 | +One of the main constraints when working with `heapless` types is that they all have a `const generic`. In a lot of situations, these can be removed thanks to the `View` types. |
| 20 | + |
| 21 | +A lot of embedded firmware will allocated a couple of buffers and pass them around to save on memory. |
| 22 | +To make it easy to change the size of the buffers, a lot of functions will carry along these `const generics`: |
| 23 | + |
| 24 | +```rust |
| 25 | +use heapless::Vec; |
| 26 | +struct App{ |
| 27 | + … |
| 28 | +} |
| 29 | + |
| 30 | +impl App { |
| 31 | + pub fn handle_request<const N: usize, const M: usize>(input: &mut Vec<u8, N>, output: &mut Vec<u8, M>) -> Result<(), Error> { |
| 32 | + … |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +The new `View` variants of the types will enable you to remove the `const generics` while still keeping the same functionality: |
| 38 | + |
| 39 | +```rust |
| 40 | +use heapless::VecView; |
| 41 | +struct App{ |
| 42 | + … |
| 43 | +} |
| 44 | + |
| 45 | +impl App { |
| 46 | + pub fn handle_request(input: &mut VecView<u8>, output: &mut VecView<u8>) -> Result<(), Error> { |
| 47 | + … |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Callsites of the `handle_request` will essentially be able to stay the same, the function will continue to accept `Vec<u8, N>`. So what's the difference between `VecView` and `Vec`? |
| 53 | +There are almost none, both are aliases of the same underlying type `VecInner`. The only limitation that `VecView` has compared to `Vec` is that `VecView` is `!Sized`. This means that you cannot perform anything that would require the compiler to know the size of the `VecView` at compile-time. In practice, you will always need to manipulate `VecView` through pointer indirection (generally a reference). This means you can't just create a `VecView` out of thin air, the `VecView` is a runtime "View" of an existing `Vec`. |
| 54 | + |
| 55 | +So how can we obtain a `VecView` ? It's pretty simple: `Vec` can be *coerced* into a `VecView`. [Coercion](https://doc.rust-lang.org/reference/type-coercions.html) (in this case [`Unsized` coercion](https://doc.rust-lang.org/reference/type-coercions.html#r-coerce.unsize)), is a way the compiler can transform one type into another implicitely. In this case, the compiler is capable of converting pointers to a `Vec` (`&Vec<T>`, `&mut Vec<T>`, `Box<T>` etc...) to pointers to a `VecView`, so you can use a reference to a `Vec` when a reference to a `VecView` is exepected: |
| 56 | + |
| 57 | +```rust |
| 58 | +use heapless::{VecView, Vec}; |
| 59 | +struct App{ |
| 60 | + … |
| 61 | +} |
| 62 | + |
| 63 | +impl App { |
| 64 | + pub fn handle_request(input: &mut VecView<u8>, output: &mut Vec<u8>) -> Result<(), Error> { |
| 65 | + … |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +let mut request: Vec<u8; 256> = Vec::new(); |
| 70 | +let mut reply: Vec<u8; 256> = Vec::new(); |
| 71 | + |
| 72 | +app.handle_request(&mut request, &mut reply).unwrap(); |
| 73 | +``` |
| 74 | + |
| 75 | +If you prefer things to be explicit, the `View` variants of types (`Vec` is not the only datastructure having `View` variants) can be obtained through `vec.as_view()` or through `vec.as_mut_view()`. |
| 76 | + |
| 77 | +The pointer to the `VecView` is the size of 2 `usize`: one for the address of the underlying `Vec`, and one for the capacity of the underlying `Vec`. This is exactly like slices. `VecView<T>` is to `Vec<T, N>` what a slice `[T]` is to an array `[T; N]`. |
| 78 | +Unless you need to store data on the stack, most often you will pass around `&mut [T]` rather than `&mut [T; N]`, because it's simpler. The same applies to `VecView`. Wherever you use `&mut Vec<T, N>`, you can instead use `&mut VecView<T>`. |
| 79 | + |
| 80 | +The `View` types are not available just for `Vec`. There are `View` versions of a lot of heapless types. |
| 81 | + |
| 82 | +## Benefits of the view types |
| 83 | + |
| 84 | +The benefits are multiple: |
| 85 | + |
| 86 | +### Better compatibility with `dyn Traits` |
| 87 | + |
| 88 | +If a trait has a function that takes a generic, it is not `dyn` compatible. By removing the const generic, the `View` types can make `dyn Trait` can pass around data structures without having to hard-code a single size of buffer in the trait definition. |
| 89 | + |
| 90 | +### Better ergonomics |
| 91 | + |
| 92 | +The View types can remove a ton of excess noise from the generics. |
| 93 | + |
| 94 | +### Better binary size and compile times |
| 95 | + |
| 96 | +When you use const-generics, the compiler needs to compile a new version of the function for each value of the const-generic. |
| 97 | +Removing the const generic means cutting down on duplicated function that are all almost the same, which improves both compile time and the size of the resulting binary. |
| 98 | + |
| 99 | +# The `LenType` optimization |
| 100 | + |
| 101 | +Most often, buffers in embedded applications will not contain a huge number of items. |
| 102 | +However, until `0.9.1` their capacity was almost always stored as a `usize`, which can often encode much more values than necessary. |
| 103 | + |
| 104 | +In 0.9.1, most data structures now have a new optional generic parameter called `LenT`. This type accepts `u8`, `u16`, `u32`, and `usize`, and defaults to `usize` to keep uses of the library simple. |
| 105 | + |
| 106 | +If you are seriously constrained by memory, a `Vec<T, 28>` can become a `Vec<T, 28, u8>`, saving up to 7 bytes per `Vec`. This is not much, but in very small microcontrollers it can make the difference between a program that uses all the memory available and one that just fits. |
| 107 | + |
| 108 | +This release was made possible by [@sgued] and [@zeenix] joining the embedded working group as part of the libs team. |
0 commit comments