-
Notifications
You must be signed in to change notification settings - Fork 763
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
refactor pyproto internals #1270
Conversation
use std::fmt::Display; | ||
|
||
pub fn print_err(msg: String, t: TokenStream) { |
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.
Good catch 👍
Quite a nice refactoring, thanks. How about adding some constructors to |
BTW, I came up with a shorter fn modify_arg_ty(
sig: &mut syn::Signature,
idx: usize,
decl1: &syn::FnArg,
decl2: &syn::FnArg,
) -> syn::Result<()> {
let arg = sig.inputs[idx].clone();
match arg {
syn::FnArg::Typed(ref cap) if crate::utils::option_type_argument(&*cap.ty).is_some() => {
sig.inputs[idx] = fix_name(&cap.pat, &decl2.inputs[idx])?;
}
syn::FnArg::Typed(ref cap) => {
sig.inputs[idx] = fix_name(&cap.pat, &decl1.inputs[idx])?;
}
_ => return Err(syn::Error::new_spanned(arg, "not supported")),
}
Ok(())
} |
I thought and tried some constructors but then the declarations in Else, there's the builder pattern that can be a solution Which one do you like better ? |
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.
🚀 Thanks, this looks awesome to me! Lovely to see a great reduction in LOC and overall it's much easier to understand the affected files.
The .has_self()
builder method is really nice as it'll make it super easy to refactor the self-types later as discussed on the issue.
Regarding CHANGELOG entry, I don't think this affects users so imo it shouldn't have an entry.
pub const fn args(self, args: &'static [&'static str]) -> MethodProtoBuilder { | ||
let mut with_args = self; | ||
with_args.args = args; | ||
with_args | ||
} |
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.
I think you can probably take mut self
in each of these builder methods, e.g.
pub const fn args(self, args: &'static [&'static str]) -> MethodProtoBuilder { | |
let mut with_args = self; | |
with_args.args = args; | |
with_args | |
} | |
pub const fn args(mut self, args: &'static [&'static str]) -> MethodProtoBuilder { | |
self.args = args; | |
self | |
} |
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.
You're right, I didn't think mut self
was possible but the code is much nicer this way.
pyo3-derive-backend/src/pyproto.rs
Outdated
@@ -66,6 +66,7 @@ fn impl_proto_impl( | |||
for iimpl in impls.iter_mut() { | |||
if let syn::ImplItem::Method(ref mut met) = iimpl { | |||
// impl Py~Protocol<'p> { type = ... } | |||
// TODO: MethodProto.result = meth.sig.output |
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.
Not sure what this TODO is asking to happen?
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.
This TODO was forgotten there. It's removed.
Regarding MSRV failure - I think there's now maybe enough reasons for us to bump to rust 1.45, so I'm probably going to open a PR for that later. |
Looks great, thanks! If you squash and rebase on master I think you should find that MSRV CI starts working. I notice this PR is still in draft, but I think other than a rebase it's now hopefully ready to be merged? |
f88029d
to
9b9ca2c
Compare
Squashed, rebased, removed a Still some problems with MSRV and 1.45. The right commit made it to 1.46 |
Ugh. Based on this comment
I wonder if this works as a workaround: // TODO: workaround for no unsized casts in const fn on Rust 1.45 (stable in 1.46)
const EMPTY_ARGS: &[&str] = &[];
impl MethodProtoBuilder {
pub const fn new(name: &'static str, proto: &'static str) -> Self {
MethodProtoBuilder {
name,
proto,
args: EMPTY_ARGS,
with_self: false,
with_result: true,
}
}
} If you don't have a chance before I do, I might push a commit to this branch tonight to try that. |
... or I guess if that doesn't work, maybe could just always take |
Just a nit, but your impl MethodProtoBuilder {
const EMPTY_ARGS: &[&str] = &[];
...
} |
Why use |
e243528
to
dcad610
Compare
This workaround works !
I merged the |
👍 good spot
🎉 this refactoring is really great; it's reduced LOC by just over 50% 2 for this part of the codebase. Thanks so much for your work on this! |
This PR aims to refactor MethodProto.
make test
passed OKStill to do:
Free
methodsCloses: #1117