forked from vmware-archive/node-replicated-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallible_string.rs
More file actions
66 lines (57 loc) · 1.69 KB
/
Copy pathfallible_string.rs
File metadata and controls
66 lines (57 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! Implement Fallible String
//!
//! Thils file will ideally added to the fallible_collections library.
use alloc::collections::TryReserveError;
use alloc::string::String;
/// trait implementing all fallible methods on vec
pub(crate) trait FallibleString {
/// see with_capacity
fn try_with_capacity(capacity: usize) -> Result<String, TryReserveError>;
/// see push
fn try_push(&mut self, ch: char) -> Result<(), TryReserveError>;
/// see push_str
fn try_push_str(&mut self, string: &str) -> Result<(), TryReserveError>;
}
impl FallibleString for String {
#[inline]
fn try_with_capacity(capacity: usize) -> Result<String, TryReserveError> {
let mut s = String::new();
s.try_reserve(capacity)?;
Ok(s)
}
#[inline]
fn try_push(&mut self, ch: char) -> Result<(), TryReserveError> {
self.try_reserve(1)?;
self.push(ch);
Ok(())
}
#[inline]
fn try_push_str(&mut self, string: &str) -> Result<(), TryReserveError> {
self.try_reserve(string.len())?;
self.push_str(string);
Ok(())
}
}
/// TryString is a thin wrapper around String to provide support for fallible
/// allocation.
///
/// See the crate documentation for more.
#[derive(PartialEq)]
pub(crate) struct TryString {
inner: String,
}
impl From<TryString> for String {
fn from(s: TryString) -> String {
s.inner
}
}
impl core::convert::TryFrom<&str> for TryString {
type Error = TryReserveError;
#[inline]
fn try_from(value: &str) -> Result<Self, Self::Error> {
let mut inner = String::new();
inner.try_reserve(value.len())?;
inner.push_str(value);
Ok(TryString { inner })
}
}