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
67
68
use crate::{ConversionError, Env, RawVal, TryIntoVal};
#[cfg(feature = "std")]
use stellar_xdr::ScObjectType;
#[cfg(feature = "std")]
use crate::{Object, RawValConvertible, TryFromVal};
#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, RawVal> for String {
type Error = ConversionError;
#[inline(always)]
fn try_from_val(env: &E, val: RawVal) -> Result<Self, Self::Error> {
let obj: Object = val.try_into_val(env)?;
if obj.is_obj_type(ScObjectType::Bytes) {
let len = unsafe { <u32 as RawValConvertible>::unchecked_from_val(env.bytes_len(obj)) };
let mut vec = std::vec![0; len as usize];
env.bytes_copy_to_slice(obj, RawVal::U32_ZERO, &mut vec)
.map_err(|_| ConversionError)?;
String::from_utf8(vec).map_err(|_| ConversionError)
} else {
Err(ConversionError)
}
}
}
#[cfg(feature = "std")]
impl<E: Env> TryIntoVal<E, String> for RawVal {
type Error = ConversionError;
#[inline(always)]
fn try_into_val(self, env: &E) -> Result<String, Self::Error> {
<_ as TryFromVal<E, RawVal>>::try_from_val(env, self)
}
}
impl<E: Env> TryIntoVal<E, RawVal> for &str {
type Error = ConversionError;
#[inline(always)]
fn try_into_val(self, env: &E) -> Result<RawVal, Self::Error> {
Ok(env
.bytes_new_from_slice(self.as_bytes())
.map_err(|_| ConversionError)?
.to_raw())
}
}
#[cfg(feature = "std")]
impl<E: Env> TryIntoVal<E, RawVal> for String {
type Error = ConversionError;
#[inline(always)]
fn try_into_val(self, env: &E) -> Result<RawVal, Self::Error> {
(&self).try_into_val(env)
}
}
#[cfg(feature = "std")]
impl<E: Env> TryIntoVal<E, RawVal> for &String {
type Error = ConversionError;
#[inline(always)]
fn try_into_val(self, env: &E) -> Result<RawVal, Self::Error> {
self.as_str().try_into_val(env)
}
}