-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: make to_compact borrow
#9488
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 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
82aa0ed
feat: make to_compact borrow
mattsse 57ce90e
compiles
joshieDo ad7039e
Merge remote-tracking branch 'origin/main' into matt/make-to-compact-…
joshieDo f50aef0
cargo docs fix
joshieDo 6f9c326
revert Cow
joshieDo 7b86d7b
Merge remote-tracking branch 'origin/main' into matt/make-to-compact-…
joshieDo 58a05df
merge fixes
joshieDo 6f35549
rm removed attr
joshieDo 4f44f70
clippy
joshieDo 03a6300
add impl compact for slices
joshieDo 9537b92
fmt
joshieDo a8d9eb0
revert lifetime change from macro
joshieDo 1038d8d
ClientVersion uses slice
joshieDo 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
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
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
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
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 |
|---|---|---|
|
|
@@ -4,7 +4,12 @@ use super::*; | |
| use convert_case::{Case, Casing}; | ||
|
|
||
| /// Generates code to implement the `Compact` trait for a data type. | ||
| pub fn generate_from_to(ident: &Ident, fields: &FieldList, is_zstd: bool) -> TokenStream2 { | ||
| pub fn generate_from_to( | ||
| ident: &Ident, | ||
| has_lifetime: bool, | ||
| fields: &FieldList, | ||
| is_zstd: bool, | ||
| ) -> TokenStream2 { | ||
| let flags = format_ident!("{ident}Flags"); | ||
|
|
||
| let to_compact = generate_to_compact(fields, ident, is_zstd); | ||
|
|
@@ -15,35 +20,66 @@ pub fn generate_from_to(ident: &Ident, fields: &FieldList, is_zstd: bool) -> Tok | |
| let fuzz = format_ident!("fuzz_test_{snake_case_ident}"); | ||
| let test = format_ident!("fuzz_{snake_case_ident}"); | ||
|
|
||
| // Build function | ||
| quote! { | ||
| let lifetime = if has_lifetime { | ||
| quote! { 'a } | ||
| } else { | ||
| quote! {} | ||
| }; | ||
|
|
||
| #[cfg(test)] | ||
| #[allow(dead_code)] | ||
| #[test_fuzz::test_fuzz] | ||
| fn #fuzz(obj: #ident) { | ||
| let mut buf = vec![]; | ||
| let len = obj.clone().to_compact(&mut buf); | ||
| let (same_obj, buf) = #ident::from_compact(buf.as_ref(), len); | ||
| assert_eq!(obj, same_obj); | ||
| let impl_compact = if has_lifetime { | ||
| quote! { | ||
| impl<#lifetime> Compact for #ident<#lifetime> | ||
| } | ||
| } else { | ||
| quote! { | ||
| impl Compact for #ident | ||
| } | ||
| }; | ||
|
|
||
| #[test] | ||
| pub fn #test() { | ||
| #fuzz(#ident::default()) | ||
| let fn_from_compact = if has_lifetime { | ||
| quote! { unimplemented!("from_compact not supported with ref structs") } | ||
|
Collaborator
Author
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. I think this a reasonable approach, we only need this for bridge types really |
||
| } else { | ||
| quote! { | ||
| let (flags, mut buf) = #flags::from(buf); | ||
| #from_compact | ||
| } | ||
| }; | ||
|
|
||
| let fuzz_tests = if has_lifetime { | ||
| quote! {} | ||
| } else { | ||
| quote! { | ||
| #[cfg(test)] | ||
| #[allow(dead_code)] | ||
| #[test_fuzz::test_fuzz] | ||
| fn #fuzz(obj: #ident) { | ||
| let mut buf = vec![]; | ||
| let len = obj.clone().to_compact(&mut buf); | ||
| let (same_obj, buf) = #ident::from_compact(buf.as_ref(), len); | ||
| assert_eq!(obj, same_obj); | ||
| } | ||
|
|
||
| #[test] | ||
| pub fn #test() { | ||
| #fuzz(#ident::default()) | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Build function | ||
| quote! { | ||
| #fuzz_tests | ||
|
|
||
| impl Compact for #ident { | ||
| fn to_compact<B>(self, buf: &mut B) -> usize where B: bytes::BufMut + AsMut<[u8]> { | ||
| #impl_compact { | ||
| fn to_compact<B>(&self, buf: &mut B) -> usize where B: bytes::BufMut + AsMut<[u8]> { | ||
| let mut flags = #flags::default(); | ||
| let mut total_length = 0; | ||
| #(#to_compact)* | ||
| total_length | ||
| } | ||
|
|
||
| fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) { | ||
| let (flags, mut buf) = #flags::from(buf); | ||
| #from_compact | ||
| #fn_from_compact | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
ah nice