-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduces Bytes
, Blob
, and Clob
wrapper types
#506
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ There are a lot of trait impls here that feel repetitive. I'm confident that we can represent them more concisely if needed (perhaps via a macro or a |
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗺️ The
IonReader
trait's blob- and clob-related methods had to be updated to returnBlob
andClob
.Before:
After:
This change had to be made in each implementation of
IonReader
, of which there are several.