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

Missing Error impl for throw MyError fn #298

Closed
boozook opened this issue Nov 2, 2024 · 5 comments
Closed

Missing Error impl for throw MyError fn #298

boozook opened this issue Nov 2, 2024 · 5 comments

Comments

@boozook
Copy link

boozook commented Nov 2, 2024

The type returned by a function as second part of the Result must implement the Error protocol. For example:

struct MyErr;
fn err() -> Result<(), MyErr> { Ok(()) }
fn string() -> Result<(), String> { Ok(()) }

#[swift_bridge::bridge]
mod ffi {
	extern "Rust" {
		type MyErr;

		fn err() -> Result<(), MyErr>;
		fn string() -> Result<(), String>;
	}
}

Error: Thrown expression type 'RustString' does not conform to 'Error'
Produced Swift:
Screenshot 2024-11-02 at 12 48 45


Workaround is to add impl by hand:

extension MyErr: Error {}
extension RustString: Error {}

But this is not refactorible and could be great to have one of following options:

  • generate extension {Ty}: Error {} automatically
  • generate extension {Ty}: Error {} by rust attribute like #[swift_bridge(impl(Error))] type MyErr;
  • inject user's raw swift-source into generated output.

First and second are the best, imho.

@boozook boozook changed the title Missing Error impl for throw Error fn Missing Error impl for throw MyError fn Nov 2, 2024
@chinedufn
Copy link
Owner

Thanks for opening this issue.


Support for -> Resutlt<*, String> is implemented and will be available in the next release #296


Support for the Error protocol is tracked in #150 , so I'll close this in favor of that issue.
If you or any future person that sees this is interested in implementing support for the Error protocol I'd be happy to provide hand-held guidance and answer all questions.

@boozook
Copy link
Author

boozook commented Nov 2, 2024

@chinedufn Thanks! Impl for String is good but I need to know is that error recoverable or not, so I need a custom struct/enum.

@chinedufn
Copy link
Owner

Anytime.

#150 would solve your problem., or are you suggesting otherwise?

@boozook
Copy link
Author

boozook commented Nov 2, 2024

Partially. Could be great to have something like #[swift_bridge(Sendable, Error)] type Foo;.

@boozook
Copy link
Author

boozook commented Nov 4, 2024

#150 would solve your problem., or are you suggesting otherwise?

Looks like good enough, as I hope. Thanks! 😊

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

No branches or pull requests

2 participants