soroban-sdk 23.4.0

Soroban SDK.
Documentation
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use core::{cmp::Ordering, convert::Infallible, fmt::Debug};

use super::{
    env::internal::{Env as _, EnvBase as _, StringObject},
    Bytes, ConversionError, Env, IntoVal, TryFromVal, TryIntoVal, Val,
};

use crate::unwrap::{UnwrapInfallible, UnwrapOptimized};
#[cfg(doc)]
use crate::{storage::Storage, Map, Vec};

#[cfg(not(target_family = "wasm"))]
use super::xdr::{ScString, ScVal};

/// String is a contiguous growable array type containing `u8`s.
///
/// The array is stored in the Host and available to the Guest through the
/// functions defined on String.
///
/// String values can be stored as [Storage], or in other types like [Vec],
/// [Map], etc.
///
/// ### Examples
///
/// String values can be created from slices:
/// ```
/// use soroban_sdk::{String, Env};
///
/// let env = Env::default();
/// let msg = "a message";
/// let s = String::from_str(&env, msg);
/// let mut out = [0u8; 9];
/// s.copy_into_slice(&mut out);
/// assert_eq!(msg.as_bytes(), out)
/// ```
#[derive(Clone)]
pub struct String {
    env: Env,
    obj: StringObject,
}

impl Debug for String {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        #[cfg(target_family = "wasm")]
        write!(f, "String(..)")?;
        #[cfg(not(target_family = "wasm"))]
        write!(f, "String({self})")?;
        Ok(())
    }
}

impl Eq for String {}

impl PartialEq for String {
    fn eq(&self, other: &Self) -> bool {
        self.partial_cmp(other) == Some(Ordering::Equal)
    }
}

impl PartialOrd for String {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(Ord::cmp(self, other))
    }
}

impl Ord for String {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        #[cfg(not(target_family = "wasm"))]
        if !self.env.is_same_env(&other.env) {
            return ScVal::from(self).cmp(&ScVal::from(other));
        }
        let v = self
            .env
            .obj_cmp(self.obj.to_val(), other.obj.to_val())
            .unwrap_infallible();
        v.cmp(&0)
    }
}

impl TryFromVal<Env, String> for String {
    type Error = ConversionError;

    fn try_from_val(_env: &Env, v: &String) -> Result<Self, Self::Error> {
        Ok(v.clone())
    }
}

impl TryFromVal<Env, StringObject> for String {
    type Error = Infallible;

    fn try_from_val(env: &Env, val: &StringObject) -> Result<Self, Self::Error> {
        Ok(unsafe { String::unchecked_new(env.clone(), *val) })
    }
}

impl TryFromVal<Env, Val> for String {
    type Error = ConversionError;

    fn try_from_val(env: &Env, val: &Val) -> Result<Self, Self::Error> {
        Ok(StringObject::try_from_val(env, val)?
            .try_into_val(env)
            .unwrap_infallible())
    }
}

impl TryFromVal<Env, String> for Val {
    type Error = ConversionError;

    fn try_from_val(_env: &Env, v: &String) -> Result<Self, Self::Error> {
        Ok(v.to_val())
    }
}

impl TryFromVal<Env, &String> for Val {
    type Error = ConversionError;

    fn try_from_val(_env: &Env, v: &&String) -> Result<Self, Self::Error> {
        Ok(v.to_val())
    }
}

impl From<String> for Val {
    #[inline(always)]
    fn from(v: String) -> Self {
        v.obj.into()
    }
}

impl From<String> for StringObject {
    #[inline(always)]
    fn from(v: String) -> Self {
        v.obj
    }
}

impl From<&String> for StringObject {
    #[inline(always)]
    fn from(v: &String) -> Self {
        v.obj
    }
}

impl From<&String> for String {
    #[inline(always)]
    fn from(v: &String) -> Self {
        v.clone()
    }
}

impl From<&String> for Bytes {
    fn from(v: &String) -> Self {
        Env::string_to_bytes(&v.env, v.obj.clone())
            .unwrap_infallible()
            .into_val(&v.env)
    }
}

impl From<String> for Bytes {
    fn from(v: String) -> Self {
        (&v).into()
    }
}

#[cfg(not(target_family = "wasm"))]
impl From<&String> for ScVal {
    fn from(v: &String) -> Self {
        // This conversion occurs only in test utilities, and theoretically all
        // values should convert to an ScVal because the Env won't let the host
        // type to exist otherwise, unwrapping. Even if there are edge cases
        // that don't, this is a trade off for a better test developer
        // experience.
        ScVal::try_from_val(&v.env, &v.obj.to_val()).unwrap()
    }
}

#[cfg(not(target_family = "wasm"))]
impl From<String> for ScVal {
    fn from(v: String) -> Self {
        (&v).into()
    }
}

#[cfg(not(target_family = "wasm"))]
impl TryFromVal<Env, ScVal> for String {
    type Error = ConversionError;
    fn try_from_val(env: &Env, val: &ScVal) -> Result<Self, Self::Error> {
        Ok(
            StringObject::try_from_val(env, &Val::try_from_val(env, val)?)?
                .try_into_val(env)
                .unwrap_infallible(),
        )
    }
}

impl TryFromVal<Env, &str> for String {
    type Error = ConversionError;

    fn try_from_val(env: &Env, v: &&str) -> Result<Self, Self::Error> {
        Ok(String::from_str(env, v))
    }
}

#[cfg(not(target_family = "wasm"))]
impl core::fmt::Display for String {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
        let sc_val: ScVal = self.try_into().unwrap();
        if let ScVal::String(ScString(s)) = sc_val {
            let utf8_s = s.to_utf8_string().unwrap();
            write!(f, "{utf8_s}")?;
        } else {
            panic!("value is not a string");
        }
        Ok(())
    }
}

impl String {
    #[inline(always)]
    pub(crate) unsafe fn unchecked_new(env: Env, obj: StringObject) -> Self {
        Self { env, obj }
    }

    #[inline(always)]
    pub fn env(&self) -> &Env {
        &self.env
    }

    pub fn as_val(&self) -> &Val {
        self.obj.as_val()
    }

    pub fn to_val(&self) -> Val {
        self.obj.to_val()
    }

    pub fn as_object(&self) -> &StringObject {
        &self.obj
    }

    pub fn to_object(&self) -> StringObject {
        self.obj
    }

    #[inline(always)]
    #[doc(hidden)]
    #[deprecated(note = "use from_str")]
    pub fn from_slice(env: &Env, slice: &str) -> String {
        Self::from_str(env, slice)
    }

    #[inline(always)]
    pub fn from_bytes(env: &Env, b: &[u8]) -> String {
        String {
            env: env.clone(),
            obj: env.string_new_from_slice(b).unwrap_optimized(),
        }
    }

    #[inline(always)]
    pub fn from_str(env: &Env, s: &str) -> String {
        String {
            env: env.clone(),
            obj: env.string_new_from_slice(s.as_bytes()).unwrap_optimized(),
        }
    }

    #[inline(always)]
    pub fn len(&self) -> u32 {
        self.env().string_len(self.obj).unwrap_infallible().into()
    }

    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Copy the bytes in [String] into the given slice.
    ///
    /// ### Panics
    ///
    /// If the output slice and string are of different lengths.
    #[inline(always)]
    pub fn copy_into_slice(&self, slice: &mut [u8]) {
        let env = self.env();
        if self.len() as usize != slice.len() {
            sdk_panic!("String::copy_into_slice with mismatched slice length")
        }
        env.string_copy_to_slice(self.to_object(), Val::U32_ZERO, slice)
            .unwrap_optimized();
    }

    /// Converts the contents of the String into a respective Bytes object.
    pub fn to_bytes(&self) -> Bytes {
        self.into()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::IntoVal;

    #[test]
    fn string_from_and_to_slices() {
        let env = Env::default();

        let msg = "a message";
        let s = String::from_str(&env, msg);
        let mut out = [0u8; 9];
        s.copy_into_slice(&mut out);
        assert_eq!(msg.as_bytes(), out)
    }

    #[test]
    fn string_from_and_to_bytes() {
        let env = Env::default();

        let msg = b"a message";
        let s = String::from_bytes(&env, msg);
        let mut out = [0u8; 9];
        s.copy_into_slice(&mut out);
        assert_eq!(msg, &out)
    }

    #[test]
    #[should_panic]
    fn string_to_short_slice() {
        let env = Env::default();
        let msg = "a message";
        let s = String::from_str(&env, msg);
        let mut out = [0u8; 8];
        s.copy_into_slice(&mut out);
    }

    #[test]
    #[should_panic]
    fn string_to_long_slice() {
        let env = Env::default();
        let msg = "a message";
        let s = String::from_str(&env, msg);
        let mut out = [0u8; 10];
        s.copy_into_slice(&mut out);
    }

    #[test]
    fn string_to_val() {
        let env = Env::default();

        let s = String::from_str(&env, "abcdef");
        let val: Val = s.clone().into_val(&env);
        let rt: String = val.into_val(&env);

        assert_eq!(s, rt);
    }

    #[test]
    fn ref_string_to_val() {
        let env = Env::default();

        let s = String::from_str(&env, "abcdef");
        let val: Val = (&s).into_val(&env);
        let rt: String = val.into_val(&env);

        assert_eq!(s, rt);
    }

    #[test]
    fn double_ref_string_to_val() {
        let env = Env::default();

        let s = String::from_str(&env, "abcdef");
        let val: Val = (&&s).into_val(&env);
        let rt: String = val.into_val(&env);

        assert_eq!(s, rt);
    }

    #[test]
    fn test_string_to_bytes() {
        let env = Env::default();
        let s = String::from_str(&env, "abcdef");
        let b: Bytes = s.clone().into();
        assert_eq!(b.len(), 6);
        let mut slice = [0u8; 6];
        b.copy_into_slice(&mut slice);
        assert_eq!(&slice, b"abcdef");
        let b2 = s.to_bytes();
        assert_eq!(b, b2);
    }

    #[test]
    fn test_string_accepts_any_bytes_even_invalid_utf8() {
        let env = Env::default();
        let input = b"a\xc3\x28d"; // \xc3 is invalid utf8
        let s = String::from_bytes(&env, &input[..]);
        let b = s.to_bytes().to_buffer::<4>();
        assert_eq!(b.as_slice(), input);
    }

    #[test]
    fn test_string_display_to_string() {
        let env = Env::default();
        let input = "abcdef";
        let s = String::from_str(&env, input);
        let rt = s.to_string();
        assert_eq!(input, &rt);
    }

    #[test]
    #[should_panic = "Utf8Error"]
    fn test_string_display_to_string_invalid_utf8() {
        let env = Env::default();
        let input = b"a\xc3\x28d"; // \xc3 is invalid utf8
        let s = String::from_bytes(&env, &input[..]);
        let _ = s.to_string();
    }
}