Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/core/src/alloc/arc.rs
3073 views
1
use super::TryNew;
2
use crate::error::OutOfMemory;
3
use std_alloc::sync::Arc;
4
5
/// XXX: Stable Rust doesn't actually give us any method to build fallible
6
/// allocation for `Arc<T>`, so this is only actually fallible when using
7
/// nightly Rust and setting `RUSTFLAGS="--cfg arc_try_new"`.
8
impl<T> TryNew for Arc<T> {
9
type Value = T;
10
11
#[inline]
12
fn try_new(value: T) -> Result<Self, OutOfMemory>
13
where
14
Self: Sized,
15
{
16
#[cfg(arc_try_new)]
17
return Arc::try_new(value).map_err(|_| {
18
// We don't have access to the exact size of the inner `Arc`
19
// allocation, but (at least at one point) it was made up of a
20
// strong ref count, a weak ref count, and the inner value.
21
let bytes = core::mem::size_of::<(usize, usize, T)>();
22
OutOfMemory::new(bytes)
23
});
24
25
#[cfg(not(arc_try_new))]
26
return Ok(Arc::new(value));
27
}
28
}
29
30
#[cfg(test)]
31
mod test {
32
use super::{Arc, TryNew};
33
34
#[test]
35
fn try_new() {
36
<Arc<_> as TryNew>::try_new(4).unwrap();
37
}
38
}
39
40