Skip to main content

ion_rs/types/
lob.rs

1pub use crate::types::bytes::Bytes;
2
3/// An in-memory representation of an Ion blob.
4///
5/// ```rust
6/// use ion_rs::Blob;
7/// let ivm: &[u8] = &[0xEA_u8, 0x01, 0x00, 0xE0]; // Ion 1.0 version marker
8/// let blob: Blob = ivm.into();
9/// assert_eq!(&blob, ivm);
10/// assert_eq!(blob.as_slice().len(), 4);
11/// ```
12/// ```rust
13/// use ion_rs::Blob;
14/// let blob: Blob = "hello".into();
15/// assert_eq!(&blob, "hello".as_bytes());
16/// assert_eq!(blob.as_slice().len(), 5);
17/// ```
18///
19/// The inner [`Bytes`] is public to match `Value::Blob(Bytes)`, allowing
20/// direct construction and destructuring.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct Blob(pub Bytes);
23
24impl Blob {
25    pub fn as_slice(&self) -> &[u8] {
26        self.as_ref()
27    }
28}
29
30/// An in-memory representation of an Ion clob.
31///
32/// ```rust
33/// use ion_rs::Clob;
34/// let clob: Clob = "hello".into();
35/// assert_eq!(&clob, "hello".as_bytes());
36/// assert_eq!(clob.as_slice().len(), 5);
37/// ```
38///
39/// The inner [`Bytes`] is public to match `Value::Clob(Bytes)`, allowing
40/// direct construction and destructuring.
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct Clob(pub Bytes);
43
44impl Clob {
45    pub fn as_slice(&self) -> &[u8] {
46        self.as_ref()
47    }
48}
49
50impl AsRef<[u8]> for Blob {
51    fn as_ref(&self) -> &[u8] {
52        self.0.as_ref()
53    }
54}
55
56impl AsRef<[u8]> for Clob {
57    fn as_ref(&self) -> &[u8] {
58        self.0.as_ref()
59    }
60}
61
62impl PartialEq<[u8]> for Blob {
63    fn eq(&self, other: &[u8]) -> bool {
64        self.as_ref().eq(other)
65    }
66}
67
68impl PartialEq<[u8]> for Clob {
69    fn eq(&self, other: &[u8]) -> bool {
70        self.as_ref().eq(other)
71    }
72}
73
74impl From<Blob> for Bytes {
75    fn from(blob: Blob) -> Self {
76        blob.0
77    }
78}
79
80impl From<Clob> for Bytes {
81    fn from(clob: Clob) -> Self {
82        clob.0
83    }
84}
85
86impl From<Vec<u8>> for Blob {
87    fn from(data: Vec<u8>) -> Self {
88        let bytes: Bytes = data.into();
89        Blob(bytes)
90    }
91}
92
93impl From<Vec<u8>> for Clob {
94    fn from(data: Vec<u8>) -> Self {
95        let bytes: Bytes = data.into();
96        Clob(bytes)
97    }
98}
99
100impl From<&[u8]> for Blob {
101    fn from(data: &[u8]) -> Self {
102        Blob::from(data.to_vec())
103    }
104}
105
106impl From<&[u8]> for Clob {
107    fn from(data: &[u8]) -> Self {
108        Clob::from(data.to_vec())
109    }
110}
111
112impl From<&str> for Blob {
113    fn from(text: &str) -> Self {
114        text.as_bytes().into()
115    }
116}
117
118impl From<&str> for Clob {
119    fn from(text: &str) -> Self {
120        text.as_bytes().into()
121    }
122}