-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable crate to be used with the stable compiler (#10)
- Loading branch information
1 parent
784001f
commit a99eacc
Showing
9 changed files
with
462 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
[package] | ||
name = "fallible_vec" | ||
description = "Fallible allocation functions for the Rust standard library's `Vec` type." | ||
version = "0.2.0" | ||
version = "0.3.0" | ||
edition = "2021" | ||
license-file = "LICENSE" | ||
repository = "https://github.com/microsoft/rust_fallible_vec" | ||
readme = "README.md" | ||
categories = ["embedded", "memory-management", "no-std"] | ||
keywords = ["vec", "fallible", "collections", "no_std"] | ||
|
||
[dependencies] | ||
static_assertions = "1.1" | ||
|
||
[features] | ||
default = ["allocator_api", "use_unstable_apis"] | ||
allocator_api = [] | ||
use_unstable_apis = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use core::alloc::Layout; | ||
|
||
#[allow(dead_code)] | ||
#[cfg(any(test, not(feature = "use_unstable_apis")))] | ||
mod internal { | ||
// Forked from the Rust Standard Library: library/alloc/src/collections/mod.rs | ||
use super::*; | ||
|
||
/// The error type for `try_reserve` methods. | ||
pub struct TryReserveError { | ||
pub kind: TryReserveErrorKind, | ||
} | ||
|
||
/// Details of the allocation that caused a `TryReserveError` | ||
pub enum TryReserveErrorKind { | ||
/// Error due to the computed capacity exceeding the collection's maximum | ||
/// (usually `isize::MAX` bytes). | ||
CapacityOverflow, | ||
|
||
/// The memory allocator returned an error | ||
AllocError { | ||
/// The layout of allocation request that failed | ||
layout: Layout, | ||
non_exhaustive: (), | ||
}, | ||
} | ||
|
||
pub fn build_error_from_layout(layout: Layout) -> alloc::collections::TryReserveError { | ||
static_assertions::assert_eq_size!( | ||
alloc::collections::TryReserveError, | ||
internal::TryReserveError | ||
); | ||
unsafe { | ||
core::mem::transmute(internal::TryReserveError { | ||
kind: internal::TryReserveErrorKind::AllocError { | ||
layout, | ||
non_exhaustive: (), | ||
}, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "use_unstable_apis")] | ||
fn build_error_from_layout(layout: Layout) -> alloc::collections::TryReserveError { | ||
alloc::collections::TryReserveErrorKind::AllocError { | ||
layout, | ||
non_exhaustive: (), | ||
} | ||
.into() | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn alloc_error(layout: Layout) -> alloc::collections::TryReserveError { | ||
#[cfg(feature = "use_unstable_apis")] | ||
{ | ||
build_error_from_layout(layout) | ||
} | ||
#[cfg(not(feature = "use_unstable_apis"))] | ||
{ | ||
internal::build_error_from_layout(layout) | ||
} | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "use_unstable_apis")] | ||
fn check_error_transmute() { | ||
let layout = core::alloc::Layout::new::<[i32; 42]>(); | ||
assert_eq!( | ||
build_error_from_layout(layout), | ||
internal::build_error_from_layout(layout) | ||
); | ||
} |
Oops, something went wrong.