soroban_env_common/
bytes.rs

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
use crate::{
    declare_tag_based_object_wrapper,
    xdr::{ScErrorCode, ScErrorType},
    Env, Error, TryFromVal, TryIntoVal, Val,
};

declare_tag_based_object_wrapper!(BytesObject);

impl<E: Env, const N: usize> TryFromVal<E, BytesObject> for [u8; N] {
    type Error = Error;

    fn try_from_val(env: &E, val: &BytesObject) -> Result<Self, Self::Error> {
        let len: u32 = env.bytes_len(*val).map_err(Into::into)?.into();
        let len = len as usize;
        if len != N {
            return Err(Error::from_type_and_code(
                ScErrorType::Value,
                ScErrorCode::UnexpectedSize,
            ));
        }
        let mut arr = [0u8; N];
        env.bytes_copy_to_slice(*val, Val::U32_ZERO, &mut arr)
            .map_err(Into::into)?;
        Ok(arr)
    }
}

impl<E: Env, const N: usize> TryFromVal<E, Val> for [u8; N] {
    type Error = Error;

    fn try_from_val(env: &E, val: &Val) -> Result<Self, Self::Error> {
        let bo: BytesObject = val.try_into()?;
        bo.try_into_val(env)
    }
}

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, BytesObject> for Vec<u8> {
    type Error = Error;

    fn try_from_val(env: &E, val: &BytesObject) -> Result<Self, Self::Error> {
        let len: u32 = env.bytes_len(*val).map_err(Into::into)?.into();
        let len = len as usize;
        let mut vec = vec![0u8; len];
        env.bytes_copy_to_slice(*val, Val::U32_ZERO, &mut vec)
            .map_err(Into::into)?;
        Ok(vec)
    }
}

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, Val> for Vec<u8> {
    type Error = Error;

    fn try_from_val(env: &E, val: &Val) -> Result<Self, Self::Error> {
        let bo: BytesObject = val.try_into()?;
        bo.try_into_val(env)
    }
}

impl<E: Env> TryFromVal<E, &[u8]> for BytesObject {
    type Error = Error;
    #[inline(always)]
    fn try_from_val(env: &E, v: &&[u8]) -> Result<BytesObject, Self::Error> {
        env.bytes_new_from_slice(v).map_err(Into::into)
    }
}

impl<E: Env> TryFromVal<E, &[u8]> for Val {
    type Error = Error;
    #[inline(always)]
    fn try_from_val(env: &E, v: &&[u8]) -> Result<Val, Self::Error> {
        Ok(BytesObject::try_from_val(env, v)?.into())
    }
}

impl<E: Env, const N: usize> TryFromVal<E, [u8; N]> for BytesObject {
    type Error = Error;

    fn try_from_val(env: &E, v: &[u8; N]) -> Result<Self, Self::Error> {
        v.as_slice().try_into_val(env)
    }
}

impl<E: Env, const N: usize> TryFromVal<E, [u8; N]> for Val {
    type Error = Error;

    fn try_from_val(env: &E, v: &[u8; N]) -> Result<Self, Self::Error> {
        v.as_slice().try_into_val(env)
    }
}

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, Vec<u8>> for BytesObject {
    type Error = Error;

    fn try_from_val(env: &E, v: &Vec<u8>) -> Result<Self, Self::Error> {
        v.as_slice().try_into_val(env)
    }
}

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, Vec<u8>> for Val {
    type Error = Error;

    fn try_from_val(env: &E, v: &Vec<u8>) -> Result<Self, Self::Error> {
        v.as_slice().try_into_val(env)
    }
}