1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-11-29 18:29:40 +02:00

Add page about tinyvec.

This commit is contained in:
Andrew Walbran 2023-03-20 17:26:22 +00:00
parent 8d1f903202
commit f293ede958
6 changed files with 59 additions and 1 deletions

7
Cargo.lock generated
View File

@ -2068,6 +2068,13 @@ dependencies = [
"tinyvec_macros",
]
[[package]]
name = "tinyvec-example"
version = "0.1.0"
dependencies = [
"tinyvec",
]
[[package]]
name = "tinyvec_macros"
version = "0.1.0"

View File

@ -3,5 +3,6 @@ members = [
"i18n-helpers",
"src/exercises",
"src/bare-metal/useful-crates/allocator-example",
"src/bare-metal/useful-crates/tinyvec-example",
"src/bare-metal/useful-crates/zerocopy-example",
]

View File

@ -271,7 +271,7 @@
- [zerocopy](bare-metal/useful-crates/zerocopy.md)
- [aarch64-paging](bare-metal/useful-crates/aarch64-paging.md)
- [buddy_system_allocator](bare-metal/useful-crates/buddy_system_allocator.md)
- [tinyvec]()
- [tinyvec](bare-metal/useful-crates/tinyvec.md)
- [spin and once_cell]()
- [Android]()
- [vmbase]()

View File

@ -0,0 +1,7 @@
[package]
name = "tinyvec-example"
version = "0.1.0"
edition = "2021"
[dependencies]
tinyvec = "1.6.0"

View File

@ -0,0 +1,25 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ANCHOR: main
use tinyvec::{array_vec, ArrayVec};
fn main() {
let mut numbers: ArrayVec<[u32; 5]> = array_vec!(42, 66);
println!("{numbers:?}");
numbers.push(7);
println!("{numbers:?}");
numbers.remove(1);
println!("{numbers:?}");
}

View File

@ -0,0 +1,18 @@
# `tinyvec`
Sometimes you want something which can be resized like a `Vec`, but without heap allocation.
[`tinyvec`][1] provides this: a vector backed by an array or slice, which could be statically
allocated or on the stack, which keeps track of how many elements are used and panics if you try to
use more than are allocated.
```rust,editable,compile_fail
{{#include tinyvec-example/src/main.rs:main}}
```
<details>
* `tinyvec` requires that the element type implement `Default` for initialisation.
</details>
[1]: https://crates.io/crates/tinyvec