wasmlib/wasmtypes/
scbytes.rs

1// Copyright 2020 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use std::convert::TryInto;
5
6use crate::*;
7
8// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\
9
10pub fn bytes_decode(dec: &mut WasmDecoder) -> Vec<u8> {
11    dec.bytes().to_vec()
12}
13
14pub fn bytes_encode(enc: &mut WasmEncoder, value: &[u8]) {
15    enc.bytes(value);
16}
17
18pub fn bytes_from_bytes(buf: &[u8]) -> Vec<u8> {
19    buf.to_vec()
20}
21
22pub fn bytes_to_bytes(value: &[u8]) -> Vec<u8> {
23    value.to_vec()
24}
25
26pub fn bytes_from_string(value: &str) -> Vec<u8> {
27    hex_decode(value)
28}
29
30pub fn bytes_to_string(value: &[u8]) -> String {
31    hex_encode(value)
32}
33
34// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\
35
36pub struct ScImmutableBytes {
37    proxy: Proxy,
38}
39
40impl ScImmutableBytes {
41    pub fn new(proxy: Proxy) -> ScImmutableBytes {
42        ScImmutableBytes { proxy }
43    }
44
45    pub fn exists(&self) -> bool {
46        self.proxy.exists()
47    }
48
49    pub fn to_string(&self) -> String {
50        bytes_to_string(&self.value())
51    }
52
53    pub fn value(&self) -> Vec<u8> {
54        bytes_from_bytes(&self.proxy.get())
55    }
56}
57
58// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\
59
60// value proxy for mutable ScBytes in host container
61pub struct ScMutableBytes {
62    proxy: Proxy,
63}
64
65impl ScMutableBytes {
66    pub fn new(proxy: Proxy) -> ScMutableBytes {
67        ScMutableBytes { proxy }
68    }
69
70    pub fn delete(&self) {
71        self.proxy.delete();
72    }
73
74    pub fn exists(&self) -> bool {
75        self.proxy.exists()
76    }
77
78    pub fn set_value(&self, value: &[u8]) {
79        self.proxy.set(&bytes_to_bytes(value));
80    }
81
82    pub fn to_string(&self) -> String {
83        bytes_to_string(&self.value())
84    }
85
86    pub fn value(&self) -> Vec<u8> {
87        bytes_from_bytes(&self.proxy.get())
88    }
89}