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

Include proto macros #13

Merged
merged 18 commits into from
Sep 30, 2019
Merged

Include proto macros #13

merged 18 commits into from
Sep 30, 2019

Conversation

JohnDoneth
Copy link
Contributor

Includes 3 new macros as mentioned in #1.

  • include_proto!
  • include_client!
  • include_server!

Example:

pub mod routeguide {
    include!(concat!(env!("OUT_DIR"), "/routeguide.rs"));
}

is now replaced by the following in the server example.

include_server!(routeguide);

Optionally an included module can be renamed via a second argument to any of the macros.

include_client!(helloworld, hello_world);

Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

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

This is looking really good. I'm still thinking about breaking out the server and client generation. I'm not totally sure what the pros and cons are. Left some comments inline.

.out_dir
.clone()
.unwrap_or_else(|| PathBuf::from(std::env::var("OUT_DIR").unwrap()));
if self.out_dir.is_none() {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I understand why this was changed?

Copy link
Contributor Author

@JohnDoneth JohnDoneth Sep 29, 2019

Choose a reason for hiding this comment

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

The builder is taken by self by the ServiceGenerator to access it's fields. I needed to access the out_dir field inside of the ServiceGenerator::finalize method but it was not being set on the builder itself. So I modified the code here to set the out_dir field on the builder.

Copy link
Member

Choose a reason for hiding this comment

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

Ah but why not use unwrap_or_else?

#[doc(hidden)]
#[macro_export]
macro_rules! include_helper {
($include: ident, $ending:tt) => {
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer if $include took a string/str instead since I feel that feels more natural to what it is actually doing. An ident makes me think of something that exists already in the code.

}
use tonic::include_client;

include_client!(helloworld, hello_world);
Copy link
Member

Choose a reason for hiding this comment

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

So I still kinda prefer

pub mod pb {
	include_client!("helloworld");
}

thoughts? This removes the need to support renaming since you can do it yourself and also can just include it directly without a module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! It seems more explicit that way, which is probably a good thing.

#[macro_export]
macro_rules! include_client {
($name: tt) => {
$crate::include_helper!($name, ".rs");
Copy link
Member

Choose a reason for hiding this comment

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

what happens if someone does

include_server!(..);
include_client!(..);

That wouldn't compile because you're redefining the proto each time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it would import the {package}.rs file twice and cause conflicts.

Copy link
Member

Choose a reason for hiding this comment

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

I wonder how we could avoid this, I feel like this will cause issues with people who are trying tonic out :(

Choose a reason for hiding this comment

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

I don't think there is a way to do it w/ two macros. We could possibly try something else?

include_proto!(helloworld);
include_proto!(helloworld as hello_world);
include_proto!(helloworld, features = [server]);

(just throwing ideas out, i don't know if they are good or not)

Copy link
Contributor Author

@JohnDoneth JohnDoneth Sep 30, 2019

Choose a reason for hiding this comment

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

@carllerche definitely helpful ideas! I think that the following design should fix the concerns raised. Where the features expression can be equal to client and/or server and is both by default if omitted.

pub mod hello_world {
     include_proto!("helloworld", features = [server]);
}

};
}

/// Include generated proto client items.
Copy link
Member

Choose a reason for hiding this comment

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

I say we drop include_server and include_client for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They should be removed already

@LucioFranco LucioFranco merged commit 3edaa42 into hyperium:master Sep 30, 2019
@LucioFranco LucioFranco deleted the include-proto-macro branch September 30, 2019 18:05
brentalanmiller pushed a commit to brentalanmiller/tonic that referenced this pull request Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants