Skip to main content

neo_types/
bytestring.rs

1use std::vec::Vec;
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// Neo N3 ByteString type
7#[derive(Debug, Clone, PartialEq, Eq, Default)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub struct NeoByteString {
10    data: Vec<u8>,
11}
12
13impl NeoByteString {
14    pub fn new(data: Vec<u8>) -> Self {
15        Self { data }
16    }
17
18    pub fn from_slice(slice: &[u8]) -> Self {
19        Self {
20            data: slice.to_vec(),
21        }
22    }
23
24    pub fn as_slice(&self) -> &[u8] {
25        &self.data
26    }
27
28    pub fn len(&self) -> usize {
29        self.data.len()
30    }
31
32    pub fn is_empty(&self) -> bool {
33        self.data.is_empty()
34    }
35
36    pub fn push(&mut self, byte: u8) {
37        self.data.push(byte);
38    }
39
40    pub fn extend_from_slice(&mut self, slice: &[u8]) {
41        self.data.extend_from_slice(slice);
42    }
43}
44
45impl From<Vec<u8>> for NeoByteString {
46    fn from(data: Vec<u8>) -> Self {
47        Self { data }
48    }
49}
50
51impl From<&[u8]> for NeoByteString {
52    fn from(slice: &[u8]) -> Self {
53        Self::from_slice(slice)
54    }
55}
56
57impl AsRef<[u8]> for NeoByteString {
58    fn as_ref(&self) -> &[u8] {
59        &self.data
60    }
61}
62
63impl Extend<u8> for NeoByteString {
64    fn extend<I: IntoIterator<Item = u8>>(&mut self, iter: I) {
65        self.data.extend(iter);
66    }
67}
68
69impl FromIterator<u8> for NeoByteString {
70    fn from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Self {
71        Self {
72            data: Vec::from_iter(iter),
73        }
74    }
75}