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

Allow as in match statements #1009

Closed
strega-nil opened this issue Mar 24, 2015 · 4 comments
Closed

Allow as in match statements #1009

strega-nil opened this issue Mar 24, 2015 · 4 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@strega-nil
Copy link

As in

let ptr = match mmap(...) {
    -1 as *mut c_void => panic!(),
    p => p,
}

This is very useful for FFI and #![no_std], I don't think it takes anything away, and it shouldn't be too difficult to add.

Possibilities right now include

let ptr = match mmap(...) as usize {
    -1 => panic!(),
    p => p as *mut c_void,
}

or

let ptr = match mmap(...) {
    p if p == -1 => panic!(),
    p => p,
}

Another possible idea, one that I doubt many people would like, is to allow *const and *mut pointers to be integrals. Then you could just do the C thing of

let ptr = match mmap(...) {
    -1 => panic!(), // -1 is a *mut c_void in this situation
    p => p,
}
@lilyball
Copy link
Contributor

Perhaps type ascription is a better choice here? That RFC actually says

An earlier version of this RFC covered type ascription in patterns too, that has been postponed

So there may already be existing discussion about this.

@kennytm
Copy link
Member

kennytm commented Mar 25, 2015

@kballard Type ascription asserts that the expression has the provided type, but it doesn't apply in this case: -1 is an integer, not a raw pointer. The casting is required.

That said, I think it is better to allow int ↔︎ raw pointer casting in CTFE to allow:

const INVALID_MMAP: *mut c_void = (-1usize) as *mut c_void;
match mmap(...) {
    INVALID_MMAP => ...
    p => ...
}

@lilyball
Copy link
Contributor

Oh you're right, for some reason I was thinking that integral literals could be used where pointers are expected, but no, you need the explicit as there.

That said, I think it is better to allow int ↔︎ raw pointer casting in CTFE

Huh, that seems a bit surprising that it doesn't work. Is there an issue that tracks this? If not, one should be filed.

@petrochenkov petrochenkov added the T-lang Relevant to the language team, which will review and decide on the RFC. label Jan 29, 2018
@scottmcm
Copy link
Member

scottmcm commented Nov 11, 2020

I think that with https://rust-lang.github.io/rfcs/2920-inline-const.html this will be doable as

let ptr = match mmap(...) {
    const { -1 as *mut c_void } => panic!(),
    p => p,
}

So I'll close this and people can follow rust-lang/rust#76001.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

5 participants