Skip to content

Commit d8e213f

Browse files
authored
Rename our OOM-handling String to TryString (#12720)
1 parent 7a4f53a commit d8e213f

8 files changed

Lines changed: 45 additions & 44 deletions

File tree

crates/core/src/alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use boxed::{
1414
new_boxed_slice_from_fallible_iter, new_boxed_slice_from_iter,
1515
new_boxed_slice_from_iter_with_len, new_uninit_boxed_slice,
1616
};
17-
pub use string::String;
17+
pub use string::TryString;
1818
pub use try_clone::TryClone;
1919
pub use try_collect::{TryCollect, TryExtend, TryFromIterator};
2020
pub use try_cow::{TryCow, TryToOwned};

crates/core/src/alloc/string.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@ use std_alloc::{alloc::Layout, boxed::Box, string as inner};
88
/// A newtype wrapper around [`std::string::String`] that only exposes
99
/// fallible-allocation methods.
1010
#[derive(Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
11-
pub struct String {
11+
pub struct TryString {
1212
inner: inner::String,
1313
}
1414

15-
impl TryClone for String {
15+
impl TryClone for TryString {
1616
fn try_clone(&self) -> Result<Self, OutOfMemory> {
1717
let mut s = Self::new();
1818
s.push_str(self)?;
1919
Ok(s)
2020
}
2121
}
2222

23-
impl fmt::Debug for String {
23+
impl fmt::Debug for TryString {
2424
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2525
fmt::Debug::fmt(&self.inner, f)
2626
}
2727
}
2828

29-
impl fmt::Display for String {
29+
impl fmt::Display for TryString {
3030
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3131
fmt::Display::fmt(&self.inner, f)
3232
}
3333
}
3434

35-
impl ops::Deref for String {
35+
impl ops::Deref for TryString {
3636
type Target = str;
3737

3838
#[inline]
@@ -41,33 +41,33 @@ impl ops::Deref for String {
4141
}
4242
}
4343

44-
impl ops::DerefMut for String {
44+
impl ops::DerefMut for TryString {
4545
#[inline]
4646
fn deref_mut(&mut self) -> &mut Self::Target {
4747
&mut self.inner
4848
}
4949
}
5050

51-
impl AsRef<str> for String {
51+
impl AsRef<str> for TryString {
5252
fn as_ref(&self) -> &str {
5353
self
5454
}
5555
}
5656

57-
impl Borrow<str> for String {
57+
impl Borrow<str> for TryString {
5858
fn borrow(&self) -> &str {
5959
self
6060
}
6161
}
6262

63-
impl From<inner::String> for String {
63+
impl From<inner::String> for TryString {
6464
#[inline]
6565
fn from(inner: inner::String) -> Self {
6666
Self { inner }
6767
}
6868
}
6969

70-
impl serde::ser::Serialize for String {
70+
impl serde::ser::Serialize for TryString {
7171
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
7272
where
7373
S: serde::Serializer,
@@ -76,15 +76,15 @@ impl serde::ser::Serialize for String {
7676
}
7777
}
7878

79-
impl<'de> serde::de::Deserialize<'de> for String {
79+
impl<'de> serde::de::Deserialize<'de> for TryString {
8080
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
8181
where
8282
D: serde::Deserializer<'de>,
8383
{
8484
struct Visitor;
8585

8686
impl<'de> serde::de::Visitor<'de> for Visitor {
87-
type Value = String;
87+
type Value = TryString;
8888

8989
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
9090
f.write_str("a `wasmtime_core::alloc::String` str")
@@ -94,7 +94,7 @@ impl<'de> serde::de::Deserialize<'de> for String {
9494
where
9595
E: serde::de::Error,
9696
{
97-
let mut s = String::new();
97+
let mut s = TryString::new();
9898
s.reserve_exact(v.len()).map_err(|oom| E::custom(oom))?;
9999
s.push_str(v).expect("reserved capacity");
100100
Ok(s)
@@ -109,7 +109,7 @@ impl<'de> serde::de::Deserialize<'de> for String {
109109
}
110110
}
111111

112-
impl String {
112+
impl TryString {
113113
/// Same as [`std::string::String::new`].
114114
#[inline]
115115
pub fn new() -> Self {

crates/core/src/alloc/try_cow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ pub trait TryToOwned {
1313
}
1414

1515
impl TryToOwned for str {
16-
type Owned = String;
16+
type Owned = TryString;
1717

1818
fn try_to_owned(&self) -> Result<Self::Owned, OutOfMemory> {
19-
let mut s = String::new();
19+
let mut s = TryString::new();
2020
s.push_str(self)?;
2121
Ok(s)
2222
}

crates/environ/src/collections.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub use primary_map::PrimaryMap;
1717
pub use secondary_map::SecondaryMap;
1818
pub use wasmtime_core::{
1919
alloc::{
20-
String, TryClone, TryCollect, TryCow, TryExtend, TryFromIterator, TryNew, TryToOwned, Vec,
21-
try_new,
20+
TryClone, TryCollect, TryCow, TryExtend, TryFromIterator, TryNew, TryString, TryToOwned,
21+
Vec, try_new,
2222
},
2323
vec,
2424
};

crates/environ/src/prelude.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
//! and then `use crate::*` works as usual.
2121
2222
pub use crate::collections::{
23-
EntitySet, TryClone, TryCollect, TryExtend, TryFromIterator, TryNew, TryToOwned, try_new,
23+
EntitySet, TryClone, TryCollect, TryExtend, TryFromIterator, TryNew, TryString, TryToOwned,
24+
try_new,
2425
};
2526
pub use crate::error::{Context, Error, Result, bail, ensure, format_err};
2627
pub use alloc::borrow::ToOwned;

crates/environ/src/string_pool.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Simple string interning.
22
33
use crate::{
4-
collections::{HashMap, String, Vec},
4+
collections::{HashMap, Vec},
55
error::OutOfMemory,
66
prelude::*,
77
};
@@ -148,7 +148,7 @@ impl<'de> serde::de::Deserialize<'de> for StringPool {
148148
.map_err(|oom| A::Error::custom(oom))?;
149149
}
150150

151-
while let Some(s) = seq.next_element::<String>()? {
151+
while let Some(s) = seq.next_element::<TryString>()? {
152152
debug_assert_eq!(s.len(), s.capacity());
153153
let s = s.into_boxed_str().map_err(|oom| A::Error::custom(oom))?;
154154
if !pool.map.contains_key(&*s) {
@@ -179,7 +179,7 @@ impl StringPool {
179179
self.map.reserve(1)?;
180180
self.strings.reserve(1)?;
181181

182-
let mut owned = String::new();
182+
let mut owned = TryString::new();
183183
owned.reserve_exact(s.len())?;
184184
owned.push_str(s).expect("reserved capacity");
185185
let owned = owned

crates/fuzzing/tests/oom.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -323,67 +323,67 @@ fn vec_push() -> Result<()> {
323323
}
324324

325325
#[test]
326-
fn string_with_capacity() -> Result<()> {
326+
fn try_string_with_capacity() -> Result<()> {
327327
OomTest::new().test(|| {
328-
let _s = String::with_capacity(100)?;
328+
let _s = TryString::with_capacity(100)?;
329329
Ok(())
330330
})
331331
}
332332

333333
#[test]
334-
fn string_reserve() -> Result<()> {
334+
fn try_string_reserve() -> Result<()> {
335335
OomTest::new().test(|| {
336-
let mut s = String::new();
336+
let mut s = TryString::new();
337337
s.reserve(10)?;
338338
Ok(())
339339
})
340340
}
341341

342342
#[test]
343-
fn string_reserve_exact() -> Result<()> {
343+
fn try_string_reserve_exact() -> Result<()> {
344344
OomTest::new().test(|| {
345-
let mut s = String::new();
345+
let mut s = TryString::new();
346346
s.reserve_exact(3)?;
347347
Ok(())
348348
})
349349
}
350350

351351
#[test]
352-
fn string_push() -> Result<()> {
352+
fn try_string_push() -> Result<()> {
353353
OomTest::new().test(|| {
354-
let mut s = String::new();
354+
let mut s = TryString::new();
355355
s.push('c')?;
356356
Ok(())
357357
})
358358
}
359359

360360
#[test]
361-
fn string_push_str() -> Result<()> {
361+
fn try_string_push_str() -> Result<()> {
362362
OomTest::new().test(|| {
363-
let mut s = String::new();
363+
let mut s = TryString::new();
364364
s.push_str("hello")?;
365365
Ok(())
366366
})
367367
}
368368

369369
#[test]
370-
fn string_shrink_to_fit() -> Result<()> {
370+
fn try_string_shrink_to_fit() -> Result<()> {
371371
OomTest::new().test(|| {
372372
// len == cap == 0
373-
let mut s = String::new();
373+
let mut s = TryString::new();
374374
s.shrink_to_fit()?;
375375

376376
// len == 0 < cap
377-
let mut s = String::with_capacity(4)?;
377+
let mut s = TryString::with_capacity(4)?;
378378
s.shrink_to_fit()?;
379379

380380
// 0 < len < cap
381-
let mut s = String::with_capacity(4)?;
381+
let mut s = TryString::with_capacity(4)?;
382382
s.push('a')?;
383383
s.shrink_to_fit()?;
384384

385385
// 0 < len == cap
386-
let mut s = String::new();
386+
let mut s = TryString::new();
387387
s.reserve_exact(2)?;
388388
s.push('a')?;
389389
s.push('a')?;
@@ -394,23 +394,23 @@ fn string_shrink_to_fit() -> Result<()> {
394394
}
395395

396396
#[test]
397-
fn string_into_boxed_str() -> Result<()> {
397+
fn try_string_into_boxed_str() -> Result<()> {
398398
OomTest::new().test(|| {
399399
// len == cap == 0
400-
let s = String::new();
400+
let s = TryString::new();
401401
let _ = s.into_boxed_str()?;
402402

403403
// len == 0 < cap
404-
let s = String::with_capacity(4)?;
404+
let s = TryString::with_capacity(4)?;
405405
let _ = s.into_boxed_str()?;
406406

407407
// 0 < len < cap
408-
let mut s = String::with_capacity(4)?;
408+
let mut s = TryString::with_capacity(4)?;
409409
s.push('a')?;
410410
let _ = s.into_boxed_str()?;
411411

412412
// 0 < len == cap
413-
let mut s = String::new();
413+
let mut s = TryString::new();
414414
s.reserve_exact(2)?;
415415
s.push('a')?;
416416
s.push('a')?;

crates/wasmtime/src/engine/serialization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub fn detect_precompiled_file(path: impl AsRef<std::path::Path>) -> Result<Opti
179179

180180
#[derive(Serialize, Deserialize)]
181181
pub struct Metadata<'a> {
182-
target: collections::String,
182+
target: TryString,
183183
#[serde(borrow)]
184184
shared_flags: collections::Vec<(&'a str, FlagValue<'a>)>,
185185
#[serde(borrow)]

0 commit comments

Comments
 (0)