-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR introduces new opaque wrappers for `Vec<u8>`, which was used throughout APIs to represent owned blobs and clobs. `Bytes` captures the core functionality of an owned byte array, but does not include Ion type information. `Blob` and `Clob` are both trivial tuple struct wrappers around `Bytes`, adding an Ion type to the data. The `Value::Blob` and `Value::Clob` enum variants (which already provide an Ion type) now wrap an instance of `Bytes`. This PR fixes #500.
- Loading branch information
Showing
14 changed files
with
246 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/// An owned, immutable byte array. | ||
/// ```rust | ||
/// use ion_rs::element::Bytes; | ||
/// let ivm: &[u8] = &[0xEA_u8, 0x01, 0x00, 0xE0]; // Ion 1.0 version marker | ||
/// let bytes: Bytes = ivm.into(); | ||
/// assert_eq!(&bytes, ivm); | ||
/// ``` | ||
/// ```rust | ||
/// use ion_rs::element::Bytes; | ||
/// let bytes: Bytes = "hello".into(); | ||
/// assert_eq!(&bytes, "hello".as_bytes()); | ||
/// ``` | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct Bytes { | ||
data: Vec<u8>, | ||
} | ||
|
||
impl PartialEq<[u8]> for Bytes { | ||
fn eq(&self, other: &[u8]) -> bool { | ||
self.as_ref().eq(other) | ||
} | ||
} | ||
|
||
impl From<Vec<u8>> for Bytes { | ||
fn from(data: Vec<u8>) -> Self { | ||
Bytes { data } | ||
} | ||
} | ||
|
||
impl From<&[u8]> for Bytes { | ||
fn from(data: &[u8]) -> Self { | ||
Bytes { | ||
data: data.to_vec(), | ||
} | ||
} | ||
} | ||
|
||
impl From<&str> for Bytes { | ||
fn from(text: &str) -> Self { | ||
Bytes { | ||
data: text.as_bytes().into(), | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for Bytes { | ||
fn as_ref(&self) -> &[u8] { | ||
&self.data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use crate::element::Bytes; | ||
|
||
/// An in-memory representation of an Ion blob. | ||
/// | ||
/// ```rust | ||
/// use ion_rs::element::Blob; | ||
/// let ivm: &[u8] = &[0xEA_u8, 0x01, 0x00, 0xE0]; // Ion 1.0 version marker | ||
/// let blob: Blob = ivm.into(); | ||
/// assert_eq!(&blob, ivm); | ||
/// assert_eq!(blob.as_slice().len(), 4); | ||
/// ``` | ||
/// ```rust | ||
/// use ion_rs::element::Blob; | ||
/// let blob: Blob = "hello".into(); | ||
/// assert_eq!(&blob, "hello".as_bytes()); | ||
/// assert_eq!(blob.as_slice().len(), 5); | ||
/// ``` | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct Blob(pub Bytes); | ||
|
||
impl Blob { | ||
pub fn as_slice(&self) -> &[u8] { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
/// An in-memory representation of an Ion clob. | ||
/// | ||
/// ```rust | ||
/// use ion_rs::element::Clob; | ||
/// let clob: Clob = "hello".into(); | ||
/// assert_eq!(&clob, "hello".as_bytes()); | ||
/// assert_eq!(clob.as_slice().len(), 5); | ||
/// ``` | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct Clob(pub Bytes); | ||
|
||
impl Clob { | ||
pub fn as_slice(&self) -> &[u8] { | ||
self.as_ref() | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for Blob { | ||
fn as_ref(&self) -> &[u8] { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for Clob { | ||
fn as_ref(&self) -> &[u8] { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl PartialEq<[u8]> for Blob { | ||
fn eq(&self, other: &[u8]) -> bool { | ||
self.as_ref().eq(other) | ||
} | ||
} | ||
|
||
impl PartialEq<[u8]> for Clob { | ||
fn eq(&self, other: &[u8]) -> bool { | ||
self.as_ref().eq(other) | ||
} | ||
} | ||
|
||
impl From<Blob> for Bytes { | ||
fn from(blob: Blob) -> Self { | ||
blob.0 | ||
} | ||
} | ||
|
||
impl From<Clob> for Bytes { | ||
fn from(clob: Clob) -> Self { | ||
clob.0 | ||
} | ||
} | ||
|
||
impl From<Vec<u8>> for Blob { | ||
fn from(data: Vec<u8>) -> Self { | ||
let bytes: Bytes = data.into(); | ||
Blob(bytes) | ||
} | ||
} | ||
|
||
impl From<Vec<u8>> for Clob { | ||
fn from(data: Vec<u8>) -> Self { | ||
let bytes: Bytes = data.into(); | ||
Clob(bytes) | ||
} | ||
} | ||
|
||
impl From<&[u8]> for Blob { | ||
fn from(data: &[u8]) -> Self { | ||
Blob::from(data.to_vec()) | ||
} | ||
} | ||
|
||
impl From<&[u8]> for Clob { | ||
fn from(data: &[u8]) -> Self { | ||
Clob::from(data.to_vec()) | ||
} | ||
} | ||
|
||
impl From<&str> for Blob { | ||
fn from(text: &str) -> Self { | ||
text.as_bytes().into() | ||
} | ||
} | ||
|
||
impl From<&str> for Clob { | ||
fn from(text: &str) -> Self { | ||
text.as_bytes().into() | ||
} | ||
} |
Oops, something went wrong.