-
Notifications
You must be signed in to change notification settings - Fork 23
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
feat: add bytes_vec features #86
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use alloc::{borrow::ToOwned, vec::Vec}; | ||
use core::{ | ||
convert::From, | ||
ops::{Bound, Deref, RangeBounds}, | ||
}; | ||
|
||
#[derive(Debug, Default, Clone, PartialEq, Eq)] | ||
pub struct Bytes(Vec<u8>); | ||
|
||
impl From<Vec<u8>> for Bytes { | ||
fn from(value: Vec<u8>) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl From<&[u8]> for Bytes { | ||
fn from(value: &[u8]) -> Self { | ||
Self(value.to_owned()) | ||
} | ||
} | ||
|
||
impl From<Bytes> for Vec<u8> { | ||
fn from(value: Bytes) -> Self { | ||
value.0 | ||
} | ||
} | ||
|
||
impl Deref for Bytes { | ||
type Target = [u8]; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Bytes { | ||
pub fn from_static(bytes: &[u8]) -> Self { | ||
Self::from(bytes) | ||
} | ||
|
||
pub fn slice(&self, range: impl RangeBounds<usize>) -> Self { | ||
let len = self.len(); | ||
let begin = match range.start_bound() { | ||
Bound::Included(&n) => n, | ||
Bound::Excluded(&n) => n + 1, | ||
Bound::Unbounded => 0, | ||
}; | ||
let end = match range.end_bound() { | ||
Bound::Included(&n) => n + 1, | ||
Bound::Excluded(&n) => n, | ||
eval-exec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Bound::Unbounded => len, | ||
}; | ||
Self::from(&self.0[begin..end]) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I think it should be
n
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.
this file is just reverted from #75 , I didn't look into the detail, @yangby-cryptape could you help to check?
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.
I think the current code is mostly correct. But there's an edge case, when
n
isusize::MAX
. It might be better to pass the range toself.0[..].index
directly.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.
Sorry, I misunderstood. It's correct.
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.
Use SliceIndex for (Bound, Bound)
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.
But the SliceIndex trait is unsafe.
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.
It's safe to use. It the same as
self.0[range]
. (Actually,self.0[range]
is better.)