Fallible allocation support for Rust's Vec

Overview

Fallible allocation functions for Vec

Fallible allocation functions for the Rust standard library's alloc::vec::Vec type.

These functions are designed to be usable with #![no_std], #[cfg(no_global_oom_handling)] (see rust-lang/rust#84266) enabled and Allocators (see https://github.com/rust-lang/wg-allocators).

By default this crate requires the nightly compiler, but the stable compiler can be used if all features are disabled (i.e., specifying default-features = false for the dependency).

Usage

The recommended way to add these functions to Vec is by adding a use declaration for the FallibleVec trait: use fallible_vec::FallibleVec:

use fallible_vec::{FallibleVec, try_vec};

let mut vec = try_vec![1, 2]?;
vec.try_push(3)?;
assert_eq!(vec, [1, 2, 3]);

Panic Safety

These methods are "panic safe", meaning that if a call to external code (e.g., an iterator's next() method or an implementation of Clone::clone()) panics, then these methods will leave the Vec in a consistent state:

  • len() will be less than or equal to capacity().
  • Items in 0..len() will only be items originally in the Vec or items being added to the Vec. It will never include uninitialized memory, duplicated items or dropped items.
  • Items originally (but no longer) in the Vec or being added to (but not yet in) the Vec may be leaked - any method that may leak items like this will have a note to specify its behavior.

The exact behavior of each method is specified in its documentation.

Code origin

Most of this code is forked from Rust's Standard Library. While we will attempt to keep the code and docs in sync, if you notice any issues please check if they have been fixed in the Standard Library first.

This API is incomplete

There are many more infallible functions on Vec which have not been ported yet. If there's a particular API that you're missing feel free to open a PR or file an Issue to get it added.

Why are these not already in the Standard Library?

There is a PR to add these and more to the Standard Library, followed by an RFC to discuss if it's a good idea or not to do so.

Why would I use this crate versus similar crates?

In general, fallible_vec is only useful in situations where #[cfg(no_global_oom_handling)] is required, or if using the Allocator API (functions ending in _in). Other crates use APIs that don't exist when #[cfg(no_global_oom_handling)] is enabled (like vec::push), whereas fallible_vec reimplements each function to avoid these APIs and builds with #[cfg(no_global_oom_handling)] in its CI.

fallible_vec focuses on vec alone, whereas other crates provide support for additional types (like Box and HashMap).

Comparing fallible_vec to fallible_collections:

fallible_vec v0.1.0 fallible_collections v0.4.7
Supports no_std X X
Supports #[cfg(no_global_oom_handling)] X
Requires nightly rust compiler by default X
Supports stable rust compiler X X
vec::try_append X
vec::try_extend X
vec::try_extend_from_slice X X
vec::try_insert X X
vec::try_push X X
vec::try_push_give_back X
vec::try_resize X X
vec::try_resize_with X X
vec::try_splice_in X
try_collect X X
try_collect_in X
try_from_iterator X
try_with_capacity X
try_with_capacity_in X
try_vec! X
try_vec_in! X
Box::* X
Arc::* X
Rc::* X
HashMap::* X
try_format! X

Building locally

The recommended way to build locally is to use the build.ps1 script: this will build the crate using all feature combinations, run tests, check formatting, run clippy and build with #[cfg(no_global_oom_handling)] enabled.

In order to run this script you'll need:

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

You might also like...
A TUI for your todos built in Rust with full CLI support.

todui TUI Features This app allows for almost anythig you would need when dealing with todos: Create, edit, and delete tasks Add links to tasks Add du

Holo is a suite of routing protocols designed to support high-scale and automation-driven networks.

Holo is a suite of routing protocols designed to support high-scale and automation-driven networks. For a description of what a routing protocol is, p

Automate your business flows, support, change tickets with Automatdeck
Automate your business flows, support, change tickets with Automatdeck

Automatdeck agent Website: https://automatdeck.com Documentation: https://doc.automatdeck.com Automatdeck agent is a simple lightweight IT automation

Use Thunk to build your Rust program that runs on old Windows platforms, support Windows XP and more!

Use Thunk to build your Rust program that runs on old platforms. Thunk uses VC-LTL5 and YY-Thunks to build programs that support old platforms. So, ho

Cross-platform pseudoterminal (PTY/ConPTY) implementation with async support

pseudoterminal The pseudoterminal crate is a versatile pseudoterminal (PTY) implementation designed for Rust, offering asynchronous capabilities. This

Touch support for SDFE(Wacca).

Toucca Touch support for WACCA(SDFE). English | 简体中文 Setup Assuming you already have the game process running and working keyboard input (Being able t

LaTeX support for Typst, powered by Rust and WASM.
LaTeX support for Typst, powered by Rust and WASM.

MiTeX LaTeX support for Typst, powered by Rust and WASM. MiTeX processes LaTeX code into an abstract syntax tree (AST). Then it transforms the AST int

Cross-platform casting SDK, support Android, Windows, Linux

mirror Cross-platform casting SDK, support Android, Windows, Linux Low-latency transport protocols use [SRT](https://github.com/Haivision/srt) Video:

Meta framework. Support for dynamic plug-ins and AOP

Kokoro Dynamic publish-subscribe pattern framework. Support for dynamic plug-ins and AOP Not yet stable, do not use in production !! 下面的内容有些老旧了,但是 exa

Comments
  • Windows 8.1 code

    Windows 8.1 code

    I need the source code of Windows 8.1 since I want to port the code to ARM64 since I would like to see Windows RT 8.1 in arm64

    so could you release me the code please?

    opened by vicenteicc2008 3
  • Comparison to the fallible_collections crate

    Comparison to the fallible_collections crate

    Hi,

    I've been loosely following the vec_fallible_allocation RFC and saw you closed the RFC and published this crate.

    I've previously encountered (but not used) the fallible_collections crate. Would it be possible to add a section to the Readme comparing this crate with the fallible_collections crate (well, probably only the Vec portion of it). The exposed API seems to be quite similar, so I'm wondering what the main differences are.

    opened by jschwe 1
  • Enable crate to be used with the stable compiler

    Enable crate to be used with the stable compiler

    • Make the _in functions optional based on the allocator_api feature.
    • Add the use_unstable_apis feature to disable other unstable API usage.
    • By default, the above features will be enabled.
    • Workaround the lack of a stable way to create TryReserveError using mem::transmute.
    • Workaround the lack of support for #[cfg] on generic parameters using macros.
    • Switched the CI build to use build.ps1.
    • Added tests for building with different feature combinations enabled, including with no features enabled without RUSTC_BOOTSTRAP set.
    opened by dpaoliello 0
  • Initial implementation of `FallibleVec`

    Initial implementation of `FallibleVec`

    • Initial implementation of FallibleVec, FallibleVecClonableElements, try_vec! and some new functions.
    • GitHub action for PR and building main.
    • GitHub action for publishing to crates.io when tagging.
    • Local build script.
    opened by dpaoliello 0
Releases(v0.3.0)
  • v0.3.0(Mar 15, 2023)

    What's Changed

    • Enable crate to be used with the stable compiler by @dpaoliello in https://github.com/microsoft/rust_fallible_vec/pull/10

    Full Changelog: https://github.com/microsoft/rust_fallible_vec/compare/v0.2.0...v0.3.0

    Source code(tar.gz)
    Source code(zip)
  • v0.2.0(Mar 14, 2023)

    What's Changed

    • Documentation cleanup by @calebcartwright in https://github.com/microsoft/rust_fallible_vec/pull/5
    • Added comparison table to fallible_collections by @dpaoliello in https://github.com/microsoft/rust_fallible_vec/pull/7
    • Move try_collect into its own trait by @dpaoliello in https://github.com/microsoft/rust_fallible_vec/pull/8

    New Contributors

    • @calebcartwright made their first contribution in https://github.com/microsoft/rust_fallible_vec/pull/5

    Full Changelog: https://github.com/microsoft/rust_fallible_vec/compare/v0.1.0...v0.2.0

    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Mar 9, 2023)

Owner
Microsoft
Open source projects and samples from Microsoft
Microsoft
Padding/aligning values without heap allocation

zero-copy-pads Padding/aligning values without heap allocation. Cargo Features std (default feature): Disable #![no_std]. Enable features that require

Khải 5 Nov 29, 2021
🖥 A feature rich terminal UI file transfer and explorer with support for SCP/SFTP/FTP/S3

?? A feature rich terminal UI file transfer and explorer with support for SCP/SFTP/FTP/S3

Christian Visintin 574 Jan 5, 2023
Detects whether a terminal supports color, and gives details about that support

Detects whether a terminal supports color, and gives details about that support. It takes into account the NO_COLOR environment variable. This crate i

Kat Marchán 30 Dec 29, 2022
Rust crate to enable ANSI escape code support on Windows.

enable-ansi-support: Enable ANSI escape code support on Windows 10 About This crate provides one function, enable_ansi_support, which allows ANSI esca

Rain 9 Nov 18, 2022
A minimal readline with multiline and async support

RustyLine Async A minimal readline with multiline and async support. Inspired by rustyline , async-readline & termion-async-input.

Zyansheep 16 Dec 15, 2022
Support `use` in procmacros hygienically

Use statements in quote! Description Macro to simplify using Types in the quote! macro. Usage The quote_use! macro can be used just like quote!, but w

Roland Fredenhagen 2 Apr 1, 2022
DeFiChain octopus is a codename research & development for DFIP 2111-B: VOC: Ethereum Virtual Machine (EVM) Support.

DeFiCh/octopus DeFiChain octopus is a codename research & development for DFIP 2111-B: VOC: Ethereum Virtual Machine (EVM) Support . Proposed as a DFI

DeFi Meta Chain 6 Apr 18, 2022
Generate a vanity address (`juno1wynd...`) to show your support for WYND DAO

WYND Generator When you generate a new mnemonic, it is very random (must be to be secure), and you cannot predict the address you will get. However, i

null 9 Dec 8, 2022
🏭 Convert Markdown documents into themed HTML pages with support for code syntax highlighting, LaTeX and Mermaid diagrams.

Marky Markdown Magician ?? Features Hot reload previewing ?? Conversion to HTML / PDF ?? Themes! ✨ Extensions - Math, diagrams, syntax-highlighting ??

Vadim 12 Feb 19, 2023
Translation support for mdbook. The plugins here give you a structured way to maintain a translated book.

Gettext Translation Support for mdbook The plugins here makes it easy to translate documentation written in mdbook into multiple languages. Support fo

Google 19 Apr 5, 2023