16-bit usize supported?

Since you cannot do this:

let x: u32 = 5; let y = usize::from(x);

I'm assuming usize isn't guaranteed to be at least 32 bits? Is Rust supported on any 16-bit platforms? The reason I ask is I am writing a no_std library and don't want to restrict the platform any more than necessary, but my library definitely requires (in a naive implementation) some arrays larger than a 16-bit index will support. What's the proper way of handling something like that (or at the very least indicating to a user of the library that it requires at least 32-bit indexing).

Also, if my library has some value which should be configurable by the library user, but in my library it must be a compile time constant, then what is the best way to expose that fact to users of my library?

Correct. There are AFAIK no existing ports of rust to 16 bit platforms but it can theoretically be only 16 bit. The suggested way to convert u32 to usize is to use try_from().unwrap(). On platforms with >=32 bit address space the spurious check should be optimized out. On (theoretical) 16 bit platforms it would just fail.

To be honest this is one of my rust pet peeves. I wish there was a way to mark a crate as 'needs 32-bit' and have usize::from(u32) just work.

There is one in-tree 16-bit target, msp430-none-elf.

7 Likes

Ok, so at least one. Now that I've thought about it, I think I can adjust things to work with 16-bit indices in that case.

Is documentation the only way to advertise a compile-time constant that I want the user to know they can, and maybe should, tweak?

Thanks everyone.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.