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(core): support generics (especially Param) in #[command] #1622
Conversation
This is still marked as a draft because there is still 1 example command that doesn't work with type inference without changes. Ideally I want to support it, so I will look into ways to help inference it which may or may not involve small changes to command that doesn't work: tauri/examples/splashscreen/src-tauri/src/main.rs Lines 54 to 63 in a6baec5
It's not able to tell that the Note: that if you add a type that can successfully inference, then the error goes away e.g. I'm also curious if we could implement |
I solved the type inference by making a edit: It probably shouldn't be replaced, so the draft question is if we should keep the |
because we recommend `_: Window<P>` for type inference, the following function types are now supported: * Pat::Wild (arg: "_") * Pat::Struct (arg: final path segment) * Pat::TupleStruct (arg: final path segment)
The following command function arguments now work:
|
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 wanna kiss this code; can you just add the change file?
Also, we'll need to work on documentation (e.g. the |
What kind of change does this PR introduce? (check at least one)
Does this PR introduce a breaking change? (check one)
The PR fulfills these requirements:
fix: #xxx[,#xxx]
, where "xxx" is the issue number)If adding a new feature, the PR's description includes:
Other information:
In
#[command(with_window)]
commands, you could not specify a type forEvent
orLabel
, making any function that uses those associated types unusable from within that command function. Without those associated types specified, the developer will run into errors like"Expected type Params::Event but received struct String"
when using things likewindow.emit(...)
. Previously, the following code could not work:This is because the
import_csv_wrapper
function generated by#[command]
would always use<P: ::tauri::Params>
as the generic.With this first commit in this draft, the above code works because the following is now true:
Type::Path
(like a struct). (unlikely to change)Type::Path
must have exactly 1 generic parameter. (unlikely to change)impl Trait
ofimpl tauri::Params
. This will change before the PR is merged. This is a side effect of testing the core changes before I flesh out support for all ways of specifying generics. The following will be supported:fn my_command<P: Params<Event = String>>(window: Window<P>)
fn my_command<P>(window: Window<P>) where P: Params<Event = String>
fn my_command(window: Window<impl Params<Event = String>>)
Note: The code is pretty shaky and only accepts a happy path. This was done to test the proof of concept and to make sure it actually solves what it set out to do. At this point, I am relatively confident that this approach will work and the code will be refactored
Tasks to do before the PR is merged:
tauri-macros
crate to allow better readability with full error handlingstatic_assertions
andimpls
crate for compile-time detection of types/traits, so that we can spit out more specific errors if the wrong struct or trait is input intowindow: Window<P>