mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-17 14:06:21 +02:00
Add page about tinyvec.
This commit is contained in:
parent
8d1f903202
commit
f293ede958
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -2068,6 +2068,13 @@ dependencies = [
|
|||||||
"tinyvec_macros",
|
"tinyvec_macros",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tinyvec-example"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"tinyvec",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinyvec_macros"
|
name = "tinyvec_macros"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
@ -3,5 +3,6 @@ members = [
|
|||||||
"i18n-helpers",
|
"i18n-helpers",
|
||||||
"src/exercises",
|
"src/exercises",
|
||||||
"src/bare-metal/useful-crates/allocator-example",
|
"src/bare-metal/useful-crates/allocator-example",
|
||||||
|
"src/bare-metal/useful-crates/tinyvec-example",
|
||||||
"src/bare-metal/useful-crates/zerocopy-example",
|
"src/bare-metal/useful-crates/zerocopy-example",
|
||||||
]
|
]
|
||||||
|
@ -271,7 +271,7 @@
|
|||||||
- [zerocopy](bare-metal/useful-crates/zerocopy.md)
|
- [zerocopy](bare-metal/useful-crates/zerocopy.md)
|
||||||
- [aarch64-paging](bare-metal/useful-crates/aarch64-paging.md)
|
- [aarch64-paging](bare-metal/useful-crates/aarch64-paging.md)
|
||||||
- [buddy_system_allocator](bare-metal/useful-crates/buddy_system_allocator.md)
|
- [buddy_system_allocator](bare-metal/useful-crates/buddy_system_allocator.md)
|
||||||
- [tinyvec]()
|
- [tinyvec](bare-metal/useful-crates/tinyvec.md)
|
||||||
- [spin and once_cell]()
|
- [spin and once_cell]()
|
||||||
- [Android]()
|
- [Android]()
|
||||||
- [vmbase]()
|
- [vmbase]()
|
||||||
|
7
src/bare-metal/useful-crates/tinyvec-example/Cargo.toml
Normal file
7
src/bare-metal/useful-crates/tinyvec-example/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "tinyvec-example"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tinyvec = "1.6.0"
|
25
src/bare-metal/useful-crates/tinyvec-example/src/main.rs
Normal file
25
src/bare-metal/useful-crates/tinyvec-example/src/main.rs
Normal 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:?}");
|
||||||
|
}
|
18
src/bare-metal/useful-crates/tinyvec.md
Normal file
18
src/bare-metal/useful-crates/tinyvec.md
Normal 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
|
Loading…
x
Reference in New Issue
Block a user