-
Notifications
You must be signed in to change notification settings - Fork 112
Add transmute_ref!
macro
#183
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
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 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 |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
[package] | ||
edition = "2021" | ||
name = "zerocopy" | ||
version = "0.7.7" | ||
version = "0.7.8" | ||
authors = ["Joshua Liebow-Feeser <[email protected]>"] | ||
description = "Utilities for zero-copy parsing and serialization" | ||
license = "BSD-2-Clause" | ||
|
@@ -41,7 +41,7 @@ simd-nightly = ["simd"] | |
__internal_use_only_features_that_work_on_stable = ["alloc", "derive", "simd"] | ||
|
||
[dependencies] | ||
zerocopy-derive = { version = "=0.7.7", path = "zerocopy-derive", optional = true } | ||
zerocopy-derive = { version = "=0.7.8", path = "zerocopy-derive", optional = true } | ||
|
||
[dependencies.byteorder] | ||
version = "1.3" | ||
|
@@ -52,7 +52,7 @@ optional = true | |
# zerocopy-derive remain equal, even if the 'derive' feature isn't used. | ||
# See: https://github.com/matklad/macro-dep-test | ||
[target.'cfg(any())'.dependencies] | ||
zerocopy-derive = { version = "=0.7.7", path = "zerocopy-derive" } | ||
zerocopy-derive = { version = "=0.7.8", path = "zerocopy-derive" } | ||
|
||
[dev-dependencies] | ||
assert_matches = "1.5" | ||
|
@@ -67,4 +67,4 @@ testutil = { path = "testutil" } | |
# CI test failures. | ||
trybuild = { version = "=1.0.85", features = ["diff"] } | ||
# In tests, unlike in production, zerocopy-derive is not optional | ||
zerocopy-derive = { version = "=0.7.7", path = "zerocopy-derive" } | ||
zerocopy-derive = { version = "=0.7.8", path = "zerocopy-derive" } |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
../ui-nightly/transmute-ref-alignment-increase.rs |
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.
There's one "trick" with traits and associated constants you can do:
Then,
noop
as-is is zero-cost*So, you can modify
noop
to contain your generic code.noop
is compiled to just a singleret
. Callingnoop
produces acall
instruction, unless it is inlined. https://godbolt.org/z/YnaMvxYqhSorry if this isn't useful/already known. Also, not sure if this approach has some other limitations. Sharing just in case, because I do not see this technique used often :)
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 is a fantastic idea, thanks! We'll definitely consider it. Just want to confirm first that the check is guaranteed to catch any unsoundness: rust-lang/unsafe-code-guidelines#409
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.
Here's another thread about it: rust-lang/rust#112090
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.
Okay, based on the discussion there, it seems that there is such a guarantee, but we'd need to be careful about how we structure things to make sure that we actually write code that benefits from that guarantee.
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.
These threads are quite interesting and insightful, thanks!
I got a little more curious, and decided to try to implement something that matches
std::mem::transmute
as close as possible. It seems it works, but I'm not sure if I haven't missed anything.I wish we had
const fn
in traits, thentransmute
/transmute_ref
could've beenconst
as well :)edit: The original code was a bit too strict w.r.t.
transmute
, updated.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.
Yeah something like this would be awesome! I'm not gonna get a chance to work on this much for the next few weeks, but feel free to leave more comments here, and I'll get to them when I'm working on this again!
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.
Note that rust-lang/rust#112301 will likely make this technique a bit annoying for users (although the error messages are way better than the existing technique, and the implementation is way simpler, so IMO it's still worth it on balance).
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've made a branch experimenting with this approach: #190
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.
Exciting news! With
inline_const
being stabilized, the following check will soon be possible on stable compiler:Link to nightly playground:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=24d5ca56b2834fdfde8ec7681359e729
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.
Unfortunately our MSRV is currently 1.56, so we won't be able to use that for a while, but I'm sure we'll make use of it once our MSRV is high enough.