ion_rs/types/
bytes.rs

1use crate::ion_data::{IonEq, IonOrd};
2use std::cmp::Ordering;
3
4/// An owned, immutable byte array.
5/// ```rust
6/// use ion_rs::element::Bytes;
7/// let ivm: &[u8] = &[0xEA_u8, 0x01, 0x00, 0xE0]; // Ion 1.0 version marker
8/// let bytes: Bytes = ivm.into();
9/// assert_eq!(&bytes, ivm);
10/// ```
11/// ```rust
12/// use ion_rs::element::Bytes;
13/// let bytes: Bytes = "hello".into();
14/// assert_eq!(&bytes, "hello".as_bytes());
15/// ```
16/// ```rust
17/// use ion_rs::element::Bytes;
18/// let bytes: Bytes = b"world".into();
19/// assert_eq!(&bytes, b"world".as_slice());
20/// ```
21#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd)]
22pub struct Bytes {
23    data: Vec<u8>,
24}
25
26impl IonEq for Bytes {
27    fn ion_eq(&self, other: &Self) -> bool {
28        self == other
29    }
30}
31
32impl IonOrd for Bytes {
33    fn ion_cmp(&self, other: &Self) -> Ordering {
34        self.cmp(other)
35    }
36}
37
38impl PartialEq<[u8]> for Bytes {
39    fn eq(&self, other: &[u8]) -> bool {
40        self.as_ref().eq(other)
41    }
42}
43
44impl From<Vec<u8>> for Bytes {
45    fn from(data: Vec<u8>) -> Self {
46        Bytes { data }
47    }
48}
49
50impl From<&[u8]> for Bytes {
51    fn from(data: &[u8]) -> Self {
52        Bytes {
53            data: data.to_vec(),
54        }
55    }
56}
57
58impl<const N: usize> From<&[u8; N]> for Bytes {
59    fn from(data: &[u8; N]) -> Self {
60        Bytes {
61            data: data.to_vec(),
62        }
63    }
64}
65
66impl From<&str> for Bytes {
67    fn from(text: &str) -> Self {
68        Bytes {
69            data: text.as_bytes().into(),
70        }
71    }
72}
73
74impl AsRef<[u8]> for Bytes {
75    fn as_ref(&self) -> &[u8] {
76        &self.data
77    }
78}