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

encode should return ? for types that don't implement Encode #11

Closed
SSheldon opened this issue Feb 13, 2015 · 2 comments
Closed

encode should return ? for types that don't implement Encode #11

SSheldon opened this issue Feb 13, 2015 · 2 comments

Comments

@SSheldon
Copy link
Owner

Implementing #10 would currently require that any type used in a method argument must implement Encode. This is difficult for cases like functions, whose ObjC type encoding is just '?' but it's hard to implement Encode for all fns (because there's fn() -> R, extern fn() -> R, unsafe extern fn() -> R, unsafe fn() -> R, fn(A) -> R, etc).

It'd be simplest if encode<fn()> == "?" without implementing Encode for fn(), and then if Encode isn't implemented when it should be, it would just be an error at runtime.

@SSheldon
Copy link
Owner Author

This could be possible with negative bounds:

trait EncodePrivate {
    fn private_code() -> &'static str;
}

impl EncodePrivate for T where T: Encode {
    fn private_code() -> &'static str { <T as Encode>::code() }
}

impl EncodePrivate for T where T: !Encode {
    fn private_code() -> &'static str { "?" }
}

fn encode<T>() -> &'static str {
    <T as EncodePrivate>::private_code()
}

Negative bounds are being discussed in rust-lang/rfcs#586.

@SSheldon
Copy link
Owner Author

Turns out encode for a function actually returns "^?", so this won't do any good. Closing.

madsmtm added a commit to madsmtm/rust-objc that referenced this issue Oct 1, 2021
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

1 participant