Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Couldn't you write that operator yourself?

    #[cfg(target_word_size="32")]
    struct ThisCastIsUnsafeFixIt;
    #[cfg(target_word_size="32")]
    const u64_to_usize: ThisCastIsUnsafeFixIt = ThisCastIsUnsafeFixIt;
    #[cfg(target_word_size="64")]
    fn u64_to_usize(x: u64) -> usize { x }
    
    ...
    
    u64_to_usize(123) // won't compile on 32-bit
If "u64_to_usize" is too long of a name for you, then you should be able to do the same thing with a trait instead.


> Couldn't you write that operator yourself?

I guess I could (thanks for the tip - I didn't know about this cfg(), I've been trying with traits and sizeof::<>).

But I'd prefer it to be in the language:

• lossless integer type conversion seems like a very basic problem to me, that shouldn't need programmers to fix it themselves in a custom way in every crate.

• even if I fix it in my code, I'm still worried about other people's code, because I assume that they also develop on x64 and unintentionally write casts that are subtly broken on smaller architectures.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: