-
Notifications
You must be signed in to change notification settings - Fork 31.2k
Turbopack: bincode: Add traits for types that require TurboBincodeEncoder or TurboBincodeDecoder
#86633
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
Turbopack: bincode: Add traits for types that require TurboBincodeEncoder or TurboBincodeDecoder
#86633
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| use std::{any::type_name, mem::transmute}; | ||
|
|
||
| pub use bincode; | ||
| use bincode::{ | ||
| de::Decoder, | ||
| enc::Encoder, | ||
| error::{DecodeError, EncodeError}, | ||
| }; | ||
|
|
||
| use crate::{TurboBincodeDecode, TurboBincodeDecoder, TurboBincodeEncode, TurboBincodeEncoder}; | ||
|
|
||
|
Comment on lines
+1
to
+11
Contributor
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. The View Details📝 Patch Detailsdiff --git a/turbopack/crates/turbo-bincode/src/macro_helpers.rs b/turbopack/crates/turbo-bincode/src/macro_helpers.rs
index ad0c624f7e..3286fa030a 100644
--- a/turbopack/crates/turbo-bincode/src/macro_helpers.rs
+++ b/turbopack/crates/turbo-bincode/src/macro_helpers.rs
@@ -6,6 +6,7 @@ use bincode::{
enc::Encoder,
error::{DecodeError, EncodeError},
};
+use unty;
use crate::{TurboBincodeDecode, TurboBincodeDecoder, TurboBincodeEncode, TurboBincodeEncoder};
AnalysisMissing
|
||
| #[track_caller] | ||
| pub fn encode_for_turbo_bincode_encode_impl<'a, T: TurboBincodeEncode, E: Encoder>( | ||
| value: &T, | ||
| encoder: &'a mut E, | ||
| ) -> Result<(), EncodeError> { | ||
| let encoder = if unty::type_equal::<E, TurboBincodeEncoder>() { | ||
| // SAFETY: Transmute is safe because `&mut E` is `&mut TurboBincodeEncoder`: | ||
| // - `unty::type_equal::<E, TurboBincodeEncoder>()` does not check lifetimes, but does check | ||
| // the type and layout, so we know those are correct. | ||
| // - The transmuted encoder cannot escape this function, and we know that the lifetime of | ||
| // `'a` is at least as long as the function. | ||
| // - Lifetimes don't change layout. This is not strictly guaranteed, but if this assumption | ||
| // is broken, we'd have a different type id (type ids are derived from layout | ||
| // information), `type_equal` would return `false`, and we'd panic instead of violating | ||
| // memory safety. | ||
| // - Two mutable references have the same layout and alignment when they reference exactly | ||
| // the same type. | ||
| // - The explicit lifetime ('a) avoids creating an implitly unbounded lifetime. | ||
| unsafe { transmute::<&'a mut E, &'a mut TurboBincodeEncoder>(encoder) } | ||
| } else { | ||
| unreachable!( | ||
| "{} implements TurboBincodeEncode, but was called with a {} encoder implementation", | ||
| type_name::<T>(), | ||
| type_name::<E>(), | ||
| ) | ||
| }; | ||
| TurboBincodeEncode::encode(value, encoder) | ||
| } | ||
|
|
||
| #[track_caller] | ||
| pub fn decode_for_turbo_bincode_decode_impl< | ||
| 'a, | ||
| Context, | ||
| T: TurboBincodeDecode<Context>, | ||
| D: Decoder<Context = Context>, | ||
| >( | ||
| decoder: &'a mut D, | ||
| ) -> Result<T, DecodeError> { | ||
| let decoder = if unty::type_equal::<D, TurboBincodeDecoder>() { | ||
| // SAFETY: See notes on the `Encode::encode` implementation on | ||
| // `encode_for_turbo_bincode_encode_impl`. | ||
| unsafe { transmute::<&'a mut D, &'a mut TurboBincodeDecoder<'a>>(decoder) } | ||
| } else { | ||
| unreachable!( | ||
| "{} implements TurboBincodeDecode, but was called with a {} decoder implementation", | ||
| type_name::<T>(), | ||
| type_name::<D>(), | ||
| ) | ||
| }; | ||
| TurboBincodeDecode::decode(decoder) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.