Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Rename our OOM-handling `String` to `TryString` (#12720)

Nick Fitzgeraldd8e213f77d8a
diff --git a/crates/core/src/alloc.rs b/crates/core/src/alloc.rs
index ea570e73fd..63b70164a6 100644
--- a/crates/core/src/alloc.rs
+++ b/crates/core/src/alloc.rs
@@ -14,7 +14,7 @@ pub use boxed::{
new_boxed_slice_from_fallible_iter, new_boxed_slice_from_iter,
new_boxed_slice_from_iter_with_len, new_uninit_boxed_slice,
};
-pub use string::String;
+pub use string::TryString;
pub use try_clone::TryClone;
pub use try_collect::{TryCollect, TryExtend, TryFromIterator};
pub use try_cow::{TryCow, TryToOwned};
diff --git a/crates/core/src/alloc/string.rs b/crates/core/src/alloc/string.rs
index e312529ab2..018063f239 100644
--- a/crates/core/src/alloc/string.rs
+++ b/crates/core/src/alloc/string.rs
@@ -8,11 +8,11 @@ use std_alloc::{alloc::Layout, boxed::Box, string as inner};
/// A newtype wrapper around [`std::string::String`] that only exposes
/// fallible-allocation methods.
#[derive(Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
-pub struct String {
+pub struct TryString {
inner: inner::String,
}
-impl TryClone for String {
+impl TryClone for TryString {
fn try_clone(&self) -> Result<Self, OutOfMemory> {
let mut s = Self::new();
s.push_str(self)?;
@@ -20,19 +20,19 @@ impl TryClone for String {
}
}
-impl fmt::Debug for String {
+impl fmt::Debug for TryString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.inner, f)
}
}
-impl fmt::Display for String {
+impl fmt::Display for TryString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.inner, f)
}
}
-impl ops::Deref for String {
+impl ops::Deref for TryString {
type Target = str;
#[inline]
@@ -41,33 +41,33 @@ impl ops::Deref for String {
}
}
-impl ops::DerefMut for String {
+impl ops::DerefMut for TryString {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
-impl AsRef<str> for String {
+impl AsRef<str> for TryString {
fn as_ref(&self) -> &str {
self
}
}
-impl Borrow<str> for String {
+impl Borrow<str> for TryString {
fn borrow(&self) -> &str {
self
}
}
-impl From<inner::String> for String {
+impl From<inner::String> for TryString {
#[inline]
fn from(inner: inner::String) -> Self {
Self { inner }
}
}
-impl serde::ser::Serialize for String {
+impl serde::ser::Serialize for TryString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
@@ -76,7 +76,7 @@ impl serde::ser::Serialize for String {
}
}
-impl<'de> serde::de::Deserialize<'de> for String {
+impl<'de> serde::de::Deserialize<'de> for TryString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
@@ -84,7 +84,7 @@ impl<'de> serde::de::Deserialize<'de> for String {
struct Visitor;
impl<'de> serde::de::Visitor<'de> for Visitor {
- type Value = String;
+ type Value = TryString;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("a `wasmtime_core::alloc::String` str")
@@ -94,7 +94,7 @@ impl<'de> serde::de::Deserialize<'de> for String {
where
E: serde::de::Error,
{
- let mut s = String::new();
+ let mut s = TryString::new();
s.reserve_exact(v.len()).map_err(|oom| E::custom(oom))?;
s.push_str(v).expect("reserved capacity");
Ok(s)
@@ -109,7 +109,7 @@ impl<'de> serde::de::Deserialize<'de> for String {
}
}
-impl String {
+impl TryString {
/// Same as [`std::string::String::new`].
#[inline]
pub fn new() -> Self {
diff --git a/crates/core/src/alloc/try_cow.rs b/crates/core/src/alloc/try_cow.rs
index fcd64e1f64..6401203493 100644
--- a/crates/core/src/alloc/try_cow.rs
+++ b/crates/core/src/alloc/try_cow.rs
@@ -13,10 +13,10 @@ pub trait TryToOwned {
}
impl TryToOwned for str {
- type Owned = String;
+ type Owned = TryString;
fn try_to_owned(&self) -> Result<Self::Owned, OutOfMemory> {
- let mut s = String::new();
+ let mut s = TryString::new();
s.push_str(self)?;
Ok(s)
}
diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rs
index 6411723dd4..0c9f3ef076 100644
--- a/crates/environ/src/collections.rs
+++ b/crates/environ/src/collections.rs
@@ -17,8 +17,8 @@ pub use primary_map::PrimaryMap;
pub use secondary_map::SecondaryMap;
pub use wasmtime_core::{
alloc::{
- String, TryClone, TryCollect, TryCow, TryExtend, TryFromIterator, TryNew, TryToOwned, Vec,
- try_new,
+ TryClone, TryCollect, TryCow, TryExtend, TryFromIterator, TryNew, TryString, TryToOwned,
+ Vec, try_new,
},
vec,
};
diff --git a/crates/environ/src/prelude.rs b/crates/environ/src/prelude.rs
index b6d65e2d08..d49c74cd85 100644
--- a/crates/environ/src/prelude.rs
+++ b/crates/environ/src/prelude.rs
@@ -20,7 +20,8 @@
//! and then `use crate::*` works as usual.
pub use crate::collections::{
- EntitySet, TryClone, TryCollect, TryExtend, TryFromIterator, TryNew, TryToOwned, try_new,
+ EntitySet, TryClone, TryCollect, TryExtend, TryFromIterator, TryNew, TryString, TryToOwned,
+ try_new,
};
pub use crate::error::{Context, Error, Result, bail, ensure, format_err};
pub use alloc::borrow::ToOwned;
diff --git a/crates/environ/src/string_pool.rs b/crates/environ/src/string_pool.rs
index b70b5a3877..241de57986 100644
--- a/crates/environ/src/string_pool.rs
+++ b/crates/environ/src/string_pool.rs
@@ -1,7 +1,7 @@
//! Simple string interning.
use crate::{
- collections::{HashMap, String, Vec},
+ collections::{HashMap, Vec},
error::OutOfMemory,
prelude::*,
};
@@ -148,7 +148,7 @@ impl<'de> serde::de::Deserialize<'de> for StringPool {
.map_err(|oom| A::Error::custom(oom))?;
}
- while let Some(s) = seq.next_element::<String>()? {
+ while let Some(s) = seq.next_element::<TryString>()? {
debug_assert_eq!(s.len(), s.capacity());
let s = s.into_boxed_str().map_err(|oom| A::Error::custom(oom))?;
if !pool.map.contains_key(&*s) {
@@ -179,7 +179,7 @@ impl StringPool {
self.map.reserve(1)?;
self.strings.reserve(1)?;
- let mut owned = String::new();
+ let mut owned = TryString::new();
owned.reserve_exact(s.len())?;
owned.push_str(s).expect("reserved capacity");
let owned = owned
diff --git a/crates/fuzzing/tests/oom.rs b/crates/fuzzing/tests/oom.rs
index d1d5d1fa4a..8b9d95d5dd 100644
--- a/crates/fuzzing/tests/oom.rs
+++ b/crates/fuzzing/tests/oom.rs
@@ -323,67 +323,67 @@ fn vec_push() -> Result<()> {
}
#[test]
-fn string_with_capacity() -> Result<()> {
+fn try_string_with_capacity() -> Result<()> {
OomTest::new().test(|| {
- let _s = String::with_capacity(100)?;
+ let _s = TryString::with_capacity(100)?;
Ok(())
})
}
#[test]
-fn string_reserve() -> Result<()> {
+fn try_string_reserve() -> Result<()> {
OomTest::new().test(|| {
- let mut s = String::new();
+ let mut s = TryString::new();
s.reserve(10)?;
Ok(())
})
}
#[test]
-fn string_reserve_exact() -> Result<()> {
+fn try_string_reserve_exact() -> Result<()> {
OomTest::new().test(|| {
- let mut s = String::new();
+ let mut s = TryString::new();
s.reserve_exact(3)?;
Ok(())
})
}
#[test]
-fn string_push() -> Result<()> {
+fn try_string_push() -> Result<()> {
OomTest::new().test(|| {
- let mut s = String::new();
+ let mut s = TryString::new();
s.push('c')?;
Ok(())
})
}
#[test]
-fn string_push_str() -> Result<()> {
+fn try_string_push_str() -> Result<()> {
OomTest::new().test(|| {
- let mut s = String::new();
+ let mut s = TryString::new();
s.push_str("hello")?;
Ok(())
})
}
#[test]
-fn string_shrink_to_fit() -> Result<()> {
+fn try_string_shrink_to_fit() -> Result<()> {
OomTest::new().test(|| {
// len == cap == 0
- let mut s = String::new();
+ let mut s = TryString::new();
s.shrink_to_fit()?;
// len == 0 < cap
- let mut s = String::with_capacity(4)?;
+ let mut s = TryString::with_capacity(4)?;
s.shrink_to_fit()?;
// 0 < len < cap
- let mut s = String::with_capacity(4)?;
+ let mut s = TryString::with_capacity(4)?;
s.push('a')?;
s.shrink_to_fit()?;
// 0 < len == cap
- let mut s = String::new();
+ let mut s = TryString::new();
s.reserve_exact(2)?;
s.push('a')?;
s.push('a')?;
@@ -394,23 +394,23 @@ fn string_shrink_to_fit() -> Result<()> {
}
#[test]
-fn string_into_boxed_str() -> Result<()> {
+fn try_string_into_boxed_str() -> Result<()> {
OomTest::new().test(|| {
// len == cap == 0
- let s = String::new();
+ let s = TryString::new();
let _ = s.into_boxed_str()?;
// len == 0 < cap
- let s = String::with_capacity(4)?;
+ let s = TryString::with_capacity(4)?;
let _ = s.into_boxed_str()?;
// 0 < len < cap
- let mut s = String::with_capacity(4)?;
+ let mut s = TryString::with_capacity(4)?;
s.push('a')?;
let _ = s.into_boxed_str()?;
// 0 < len == cap
- let mut s = String::new();
+ let mut s = TryString::new();
s.reserve_exact(2)?;
s.push('a')?;
s.push('a')?;
diff --git a/crates/wasmtime/src/engine/serialization.rs b/crates/wasmtime/src/engine/serialization.rs
index 88ff39a939..eab3c8e617 100644
--- a/crates/wasmtime/src/engine/serialization.rs
+++ b/crates/wasmtime/src/engine/serialization.rs
@@ -179,7 +179,7 @@ pub fn detect_precompiled_file(path: impl AsRef<std::path::Path>) -> Result<Opti
#[derive(Serialize, Deserialize)]
pub struct Metadata<'a> {
- target: collections::String,
+ target: TryString,
#[serde(borrow)]
shared_flags: collections::Vec<(&'a str, FlagValue<'a>)>,
#[serde(borrow)]