-
Notifications
You must be signed in to change notification settings - Fork 84
feat: implement AsRef<[u8]> for Atom #303
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
Open
dislogical
wants to merge
1
commit into
servo:main
Choose a base branch
from
dislogical:feat/asref-bytes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−52
Open
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 hidden or 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 hidden or 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 hidden or 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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -193,6 +193,33 @@ impl<Static: StaticAtomSet> Atom<Static> { | |||||||||||||
| Err(hash) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// Get a reference to the underlying str. | ||||||||||||||
| #[inline] | ||||||||||||||
| pub fn as_str(&self) -> &str { | ||||||||||||||
| unsafe { | ||||||||||||||
| match self.tag() { | ||||||||||||||
| DYNAMIC_TAG => { | ||||||||||||||
| let entry = self.unsafe_data.get() as *const Entry; | ||||||||||||||
| &(*entry).string | ||||||||||||||
| } | ||||||||||||||
| INLINE_TAG => { | ||||||||||||||
| let len = (self.unsafe_data() & LEN_MASK) >> LEN_OFFSET; | ||||||||||||||
| debug_assert!(len as usize <= MAX_INLINE_LEN); | ||||||||||||||
| let src = inline_atom_slice(&self.unsafe_data); | ||||||||||||||
| str::from_utf8_unchecked(src.get_unchecked(..(len as usize))) | ||||||||||||||
| } | ||||||||||||||
| STATIC_TAG => Static::get().atoms[self.static_index() as usize], | ||||||||||||||
| _ => debug_unreachable!(), | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// Get a reference to the bytes of the underlying str. | ||||||||||||||
| #[inline] | ||||||||||||||
| pub fn as_bytes(&self) -> &[u8] { | ||||||||||||||
| self.as_str().as_bytes() | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+220
to
+222
Member
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.
Suggested change
|
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| impl<Static: StaticAtomSet> Default for Atom<Static> { | ||||||||||||||
|
|
@@ -276,22 +303,7 @@ impl<Static: StaticAtomSet> ops::Deref for Atom<Static> { | |||||||||||||
|
|
||||||||||||||
| #[inline] | ||||||||||||||
| fn deref(&self) -> &str { | ||||||||||||||
| unsafe { | ||||||||||||||
| match self.tag() { | ||||||||||||||
| DYNAMIC_TAG => { | ||||||||||||||
| let entry = self.unsafe_data.get() as *const Entry; | ||||||||||||||
| &(*entry).string | ||||||||||||||
| } | ||||||||||||||
| INLINE_TAG => { | ||||||||||||||
| let len = (self.unsafe_data() & LEN_MASK) >> LEN_OFFSET; | ||||||||||||||
| debug_assert!(len as usize <= MAX_INLINE_LEN); | ||||||||||||||
| let src = inline_atom_slice(&self.unsafe_data); | ||||||||||||||
| str::from_utf8_unchecked(src.get_unchecked(..(len as usize))) | ||||||||||||||
| } | ||||||||||||||
| STATIC_TAG => Static::get().atoms[self.static_index() as usize], | ||||||||||||||
| _ => debug_unreachable!(), | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| self.as_str() | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -317,7 +329,7 @@ impl<Static: StaticAtomSet> PartialOrd for Atom<Static> { | |||||||||||||
| if self.unsafe_data == other.unsafe_data { | ||||||||||||||
| return Some(Equal); | ||||||||||||||
| } | ||||||||||||||
| self.as_ref().partial_cmp(other.as_ref()) | ||||||||||||||
| self.as_str().partial_cmp(other.as_ref()) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -327,7 +339,7 @@ impl<Static: StaticAtomSet> Ord for Atom<Static> { | |||||||||||||
| if self.unsafe_data == other.unsafe_data { | ||||||||||||||
| return Equal; | ||||||||||||||
| } | ||||||||||||||
| self.as_ref().cmp(other.as_ref()) | ||||||||||||||
| self.as_str().cmp(other.as_ref()) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -392,24 +404,24 @@ impl<Static: StaticAtomSet> Atom<Static> { | |||||||||||||
|
|
||||||||||||||
| #[inline(always)] | ||||||||||||||
| fn inline_atom_slice(x: &NonZeroU64) -> &[u8] { | ||||||||||||||
| let x: *const NonZeroU64 = x; | ||||||||||||||
| let mut data = x as *const u8; | ||||||||||||||
| // All except the lowest byte, which is first in little-endian, last in big-endian. | ||||||||||||||
| if cfg!(target_endian = "little") { | ||||||||||||||
| data = unsafe { data.offset(1) }; | ||||||||||||||
| } | ||||||||||||||
| let len = 7; | ||||||||||||||
| unsafe { slice::from_raw_parts(data, len) } | ||||||||||||||
| let x: *const NonZeroU64 = x; | ||||||||||||||
| let mut data = x as *const u8; | ||||||||||||||
| // All except the lowest byte, which is first in little-endian, last in big-endian. | ||||||||||||||
| if cfg!(target_endian = "little") { | ||||||||||||||
| data = unsafe { data.offset(1) }; | ||||||||||||||
| } | ||||||||||||||
| let len = 7; | ||||||||||||||
| unsafe { slice::from_raw_parts(data, len) } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[inline(always)] | ||||||||||||||
| fn inline_atom_slice_mut(x: &mut u64) -> &mut [u8] { | ||||||||||||||
| let x: *mut u64 = x; | ||||||||||||||
| let mut data = x as *mut u8; | ||||||||||||||
| // All except the lowest byte, which is first in little-endian, last in big-endian. | ||||||||||||||
| if cfg!(target_endian = "little") { | ||||||||||||||
| data = unsafe { data.offset(1) }; | ||||||||||||||
| } | ||||||||||||||
| let len = 7; | ||||||||||||||
| unsafe { slice::from_raw_parts_mut(data, len) } | ||||||||||||||
| fn inline_atom_slice_mut(x: &mut u64) -> &mut [u8] { | ||||||||||||||
| let x: *mut u64 = x; | ||||||||||||||
| let mut data = x as *mut u8; | ||||||||||||||
| // All except the lowest byte, which is first in little-endian, last in big-endian. | ||||||||||||||
| if cfg!(target_endian = "little") { | ||||||||||||||
| data = unsafe { data.offset(1) }; | ||||||||||||||
| } | ||||||||||||||
| let len = 7; | ||||||||||||||
| unsafe { slice::from_raw_parts_mut(data, len) } | ||||||||||||||
| } | ||||||||||||||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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 should all optimize away but I’d prefer going through fewer layers of abstraction. The actual logic is in the
Derefimpl so, here and in the newas_bytesmethod let’s use this: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.
Good catch! I'm actually just going to implement all of the logic in
as_strasas_bytes, and then the traits can call into these. I think that'll make the code much easier to follow.