Skip to content
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: make to_compact borrow #9488

Merged
merged 13 commits into from
Jul 17, 2024
Merged

feat: make to_compact borrow #9488

merged 13 commits into from
Jul 17, 2024

Conversation

mattsse
Copy link
Collaborator

@mattsse mattsse commented Jul 13, 2024

closes #9487

needs more work on derive impls next

@mattsse mattsse added the C-enhancement New feature or request label Jul 13, 2024
@github-actions github-actions bot added the C-debt Refactor of code section that is hard to understand or maintain label Jul 13, 2024
Comment on lines 9 to 11
#[main_codec(no_arbitrary, no_serde)]
#[derive(Debug, Clone, PartialEq, Eq)]
struct GenesisAccountRef<'a> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bridge types get slightly more troublesome for types that do not support Copy (or we choose not to).

This also introduces lifetimes into the reth_codec macro which makes things slightly more complex just for bridge types.

Copy link
Collaborator

@joshieDo joshieDo Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess we could give Cow support to to_compact, but idk how complex that would be tbh

/// The account's private key. Should only be used for testing.
private_key: Option<&'a B256>,
}

#[main_codec]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
struct GenesisAccount {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from_compact is not supported by types with lifetimes/references, so we still need this bridge type when calling from_compact

}

fn from_compact(_: &[u8], _: usize) -> (Self, &[u8]) {
unimplemented!()
Copy link
Collaborator

@joshieDo joshieDo Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsupported for &T

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this blanket impl because this then can be called by any type.

there's an argument for splitting these functions into two traits, but not in this pr

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this then can be called by any type

I agree it would be cleaner to have two traits (it wasn't necessary until this PR imo) , but calling this would have to be very intentional and weird &T::from_compact

but could be done as a follow up

crates/storage/db/src/tables/mod.rs Outdated Show resolved Hide resolved
crates/trie/common/src/hash_builder/value.rs Outdated Show resolved Hide resolved
crates/trie/common/src/nodes/branch.rs Outdated Show resolved Hide resolved
@joshieDo joshieDo changed the title wip:feat: make to_compact borrow feat: make to_compact borrow Jul 16, 2024
@joshieDo joshieDo marked this pull request as ready for review July 16, 2024 18:16
@joshieDo
Copy link
Collaborator

rfr, but no merge

Copy link
Collaborator Author

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't review my own pr hehe

overall I think this is an improvement, but this really shows that the compact trait has a lot of shortcomings
I'd like to see some settings documented on the proc macro
and have a few questions

and would be nice if the vec specialization would also work for slices

pub fn #test() {
#fuzz(#ident::default())
let fn_from_compact = if has_lifetime {
quote! { unimplemented!("from_compact not supported with ref structs") }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Comment on lines 15 to 16
let topics = self.topics().iter().collect::<Vec<_>>();
topics.specialized_to_compact(&mut buffer);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this work for &[] as well?

}

fn from_compact(_: &[u8], _: usize) -> (Self, &[u8]) {
unimplemented!()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this blanket impl because this then can be called by any type.

there's an argument for splitting these functions into two traits, but not in this pr

Comment on lines 31 to 33
version.as_bytes().to_vec().to_compact(buf);
git_sha.as_bytes().to_vec().to_compact(buf);
build_timestamp.as_bytes().to_vec().to_compact(buf)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be could if this would work on slices

crates/storage/db-api/src/models/mod.rs Outdated Show resolved Hide resolved
crates/storage/db/src/tables/mod.rs Outdated Show resolved Hide resolved
crates/trie/common/src/hash_builder/value.rs Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment on lines 31 to 33
version.as_bytes().to_vec().to_compact(buf);
git_sha.as_bytes().to_vec().to_compact(buf);
build_timestamp.as_bytes().to_vec().to_compact(buf)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the to_vec still required?

Copy link
Member

@Rjected Rjected left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, this makes sense to me, let's make an issue for splitting the trait

@@ -37,7 +37,7 @@ pub fn maybe_generate_tests(args: TokenStream, ast: &DeriveInput) -> TokenStream
let len = field.encode(&mut buf);
let mut b = &mut buf.as_slice();
let decoded = super::#type_ident::decode(b).unwrap();
assert_eq!(field, decoded);
assert_eq!(field, decoded, "maybe_generate_tests::rlp");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah nice

@joshieDo joshieDo added this pull request to the merge queue Jul 17, 2024
Merged via the queue into main with commit c3347f3 Jul 17, 2024
32 checks passed
@joshieDo joshieDo deleted the matt/make-to-compact-borrow branch July 17, 2024 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-debt Refactor of code section that is hard to understand or maintain C-enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make Compact::encode accept &self
3 participants