-
Notifications
You must be signed in to change notification settings - Fork 253
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
fix: extract #[callback_vec] vec type for abi #863
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,36 @@ pub(crate) fn extract_ok_type(ty: &Type) -> Option<&Type> { | |
_ => None, | ||
} | ||
} | ||
|
||
/// Checks whether the given path is literally "Vec". | ||
/// Note that it won't match a fully qualified name `std::vec::Vec` or a type alias like | ||
/// `type MyVec = Vec<String>`. | ||
fn path_is_vec(path: &Path) -> bool { | ||
path.leading_colon.is_none() | ||
&& path.segments.len() == 1 | ||
&& path.segments.iter().next().unwrap().ident == "Vec" | ||
} | ||
|
||
/// Extracts the inner generic type from a `Vec<_>` type. | ||
/// | ||
/// For example, given `Vec<String>` this function will return `String`. | ||
#[allow(dead_code)] | ||
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. this should just be marked as only non-wasm32 config rather than allowing dead code, to make sure if this goes unused in the future it will be warned to remove. 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. Dead code warning is happening because it is only used in modules enabled by If you think it still should be under abi feature I am happy to do so. 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. yeah just under the abi feature is better, my bad on the assumption it was based on target. Better this way because if it's needed otherwise, can just change the config then. Harder to remember to remove the function if unused, or remove the dead_code annotation if it does become used. |
||
pub(crate) fn extract_vec_type(ty: &Type) -> Option<&Type> { | ||
match ty { | ||
Type::Path(type_path) if type_path.qself.is_none() && path_is_vec(&type_path.path) => { | ||
let type_params = &type_path.path.segments.first()?.arguments; | ||
let generic_arg = match type_params { | ||
// We are interested in the first (and only) angle-bracketed param: | ||
PathArguments::AngleBracketed(params) if params.args.len() == 1 => { | ||
Some(params.args.first()?) | ||
} | ||
_ => None, | ||
}?; | ||
match generic_arg { | ||
GenericArgument::Type(ty) => Some(ty), | ||
_ => None, | ||
} | ||
} | ||
_ => None, | ||
} | ||
} |
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.
Why is this necessary? Anything else other than a
Vec
would fail to compile by the macros, so why not allow aliases? Very niche case so fine to ignoreThere 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.
As I said in my other comment a verbose check just makes errors more transparent, but yeah this certainly comes with its own downsides. Happy to go either 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.
Yeah, we can leave for now. Easier to add support for aliases later than to take it away