-
Notifications
You must be signed in to change notification settings - Fork 39
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: Use package.links
trick for Protobuf compilation
#32
Merged
brunoffranca
merged 13 commits into
main
from
aov-bft-373-use-packagelinks-trick-for-protobuf-compilation
Nov 13, 2023
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bcfd700
Sketch linking-based dependency info
slowli 175f935
Simplify `Manifest` serialization
slowli ec51fe6
Validate `links` values
slowli 30af9f4
Move `build` module to `protobuf` crate
slowli 27ebd71
Move `impl ReflectMessage` to `protobuf` crate
slowli b221b3a
Brush up comments and error messages
slowli 29d09aa
Remove `Descriptor` contents
slowli 97d57d2
Remove transitively accessible deps
slowli 72a3a10
Remove direct `protox_parse` dependency
slowli cd08b9c
Merge branch 'main' into aov-bft-373-use-packagelinks-trick-for-proto…
slowli 57a7132
Use `PathBuf` in `Manifest`
slowli 3321d41
Restore `prost-reflect` dependency in build crate
slowli f4b1e14
Update from upstream
slowli 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 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,85 @@ | ||
//! Build-related functionality. This should only be used by the code generated by the `protobuf_build` crate. | ||
|
||
pub use once_cell::sync::Lazy; | ||
pub use prost; | ||
use prost::Message as _; | ||
pub use prost_reflect; | ||
use prost_reflect::prost_types; | ||
use std::sync::RwLock; | ||
|
||
/// Global descriptor pool. | ||
static POOL: Lazy<RwLock<prost_reflect::DescriptorPool>> = Lazy::new(RwLock::default); | ||
|
||
/// Protobuf descriptor + info about the mapping to rust code. | ||
#[derive(Debug)] | ||
pub struct Descriptor(()); | ||
|
||
impl Descriptor { | ||
/// Constructs a descriptor and adds it to the global pool. | ||
pub fn new(_dependencies: &[&'static Self], descriptor_bytes: &[u8]) -> Self { | ||
let descriptor = prost_types::FileDescriptorSet::decode(descriptor_bytes).unwrap(); | ||
let pool = &mut POOL.write().unwrap(); | ||
|
||
// Dependencies are already loaded to the global pool on their initialization. | ||
// The fact that we have their refs is sufficient to prove that they are already in the global pool. | ||
Self::load(pool, descriptor).expect("failed loading descriptor into global pool"); | ||
Self(()) | ||
} | ||
|
||
/// Loads the descriptor to the pool, if not already loaded. | ||
fn load( | ||
pool: &mut prost_reflect::DescriptorPool, | ||
descriptor: prost_types::FileDescriptorSet, | ||
) -> anyhow::Result<()> { | ||
let pool_has_all_files = descriptor | ||
.file | ||
.iter() | ||
.all(|file| pool.get_file_by_name(file.name()).is_some()); | ||
if pool_has_all_files { | ||
return Ok(()); | ||
} | ||
pool.add_file_descriptor_set(descriptor)?; | ||
Ok(()) | ||
} | ||
|
||
/// Returns a descriptor by a fully qualified message name. | ||
pub fn get_message_by_name(&self, name: &str) -> Option<prost_reflect::MessageDescriptor> { | ||
POOL.read().unwrap().get_message_by_name(name) | ||
// ^ This works because this descriptor must have been loaded into the global pool | ||
// when the instance was constructed. | ||
} | ||
} | ||
|
||
/// Expands to a descriptor declaration. | ||
#[macro_export] | ||
macro_rules! declare_descriptor { | ||
($name:ident => $descriptor_path:expr, $($rust_deps:path),*) => { | ||
pub static $name: $crate::build::Lazy<$crate::build::Descriptor> = | ||
$crate::build::Lazy::new(|| { | ||
$crate::build::Descriptor::new( | ||
&[$({ use $rust_deps as dep; &dep::DESCRIPTOR }),*], | ||
::std::include_bytes!($descriptor_path), | ||
) | ||
}); | ||
} | ||
} | ||
|
||
/// Implements `ReflectMessage` for a type based on a provided `Descriptor`. | ||
#[macro_export] | ||
macro_rules! impl_reflect_message { | ||
($ty:ty, $descriptor:expr, $proto_name:expr) => { | ||
impl $crate::build::prost_reflect::ReflectMessage for $ty { | ||
fn descriptor(&self) -> $crate::build::prost_reflect::MessageDescriptor { | ||
static INIT: $crate::build::Lazy<$crate::build::prost_reflect::MessageDescriptor> = | ||
$crate::build::Lazy::new(|| { | ||
$crate::build::Descriptor::get_message_by_name($descriptor, $proto_name) | ||
.unwrap() | ||
}); | ||
INIT.clone() | ||
} | ||
} | ||
}; | ||
} | ||
|
||
pub use declare_descriptor; | ||
pub use impl_reflect_message; |
This file contains 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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
//! Code generated from protobuf schema files and | ||
//! utilities for serialization. | ||
|
||
#[doc(hidden)] // should only be used by code generated by `protobuf_build` crate | ||
pub mod build; | ||
pub mod proto; | ||
mod proto_fmt; | ||
mod std_conv; | ||
pub mod testonly; | ||
|
||
pub use proto_fmt::*; | ||
pub use zksync_protobuf_build as build; | ||
pub use self::proto_fmt::*; | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains 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 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.
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.
if unused, please remove
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.
Having dependency refs allows ensuring via type system that these dependencies are added in the global pool. Would it be better to force dependencies to get initialized via
Lazy::force()
, or in some other way?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.
Type system doesn't ensure any relation between the dependencies and the newly constructed descriptor, so this is as good as Lazy::force (or just dereferencing the lazy descriptors).