Live data from Hacker News

Making Rust a Better Fit for Cheri and Other Platforms

tratt.net

1–10 of 42 posts

Re: Making Rust a Better Fit for Cheri and Other Platforms

#2
It would also be nice to be able to use unsigned types (like u8, u16 and u32) to index into slices and arrays up until usize (usually a u32 or u64). Using a usize often seems wasteful for indexing into small arrays and slices and casting makes the code look ugly (also dangerous because the effect of casting is checked by the developer, not the compiler). Admittedly, this would have the downside of having code that compiles on one architecture but not another so it’s probably not worth it.

Re: Making Rust a Better Fit for Cheri and Other Platforms

#3

It would also be nice to be able to use unsigned types (like u8, u16 and u32) to index into slices and arrays up until usize (usually a u32 or u64). Using a usize often seems wasteful for indexing into small arrays and slices and casting makes the code look ugly (also dangerous because the effect of casting is checked by the developer, not the compiler). Admittedly, this would have the downside of having code that co…

Rust supports u16 pointer. Suddenly you are casting u32 to u16. And fun begins.

Re: Making Rust a Better Fit for Cheri and Other Platforms

#4
post #3

It would also be nice to be able to use unsigned types (like u8, u16 and u32) to index into slices and arrays up until usize (usually a u32 or u64). Using a usize often seems wasteful for indexing into small arrays and slices and casting makes the code look ugly (also dangerous because the effect of casting is checked by the developer, not the compiler). Admittedly, this would have the downside of having code that co…

Rust supports u16 pointer. Suddenly you are casting u32 to u16. And fun begins.

Exactly this!

Re: Making Rust a Better Fit for Cheri and Other Platforms

#5

It would also be nice to be able to use unsigned types (like u8, u16 and u32) to index into slices and arrays up until usize (usually a u32 or u64). Using a usize often seems wasteful for indexing into small arrays and slices and casting makes the code look ugly (also dangerous because the effect of casting is checked by the developer, not the compiler). Admittedly, this would have the downside of having code that co…

IIRC integer literals are the blocking issue here. Bounds checking (and elision) happens anyway, but when `Index` is implemented for multiple integer types, `foo[0]` becomes ambiguous.

Re: Making Rust a Better Fit for Cheri and Other Platforms

#6

It would also be nice to be able to use unsigned types (like u8, u16 and u32) to index into slices and arrays up until usize (usually a u32 or u64). Using a usize often seems wasteful for indexing into small arrays and slices and casting makes the code look ugly (also dangerous because the effect of casting is checked by the developer, not the compiler). Admittedly, this would have the downside of having code that co…

What’s wrong with .try_into::().unwrap()?

Re: Making Rust a Better Fit for Cheri and Other Platforms

#7

It would also be nice to be able to use unsigned types (like u8, u16 and u32) to index into slices and arrays up until usize (usually a u32 or u64). Using a usize often seems wasteful for indexing into small arrays and slices and casting makes the code look ugly (also dangerous because the effect of casting is checked by the developer, not the compiler). Admittedly, this would have the downside of having code that co…

What’s wrong with .try_into:: ().unwrap()?

It fails at runtime and not at compile time and it only fails if the value is out of range, so you have to test with large enough values to trigger the assertion during testing.

We want to be able to index with integer types that are smaller than or has the same size as usize, but not with integer types that are larger than usize. And we want those checks at compile time.

Re: Making Rust a Better Fit for Cheri and Other Platforms

#8
Re the non-address bits of a pointer: there could be a method `shifted_by_address_width()` that just returns a same-size copy of the data (which remains unmodified) shifted over by the width of an address on the system. That way it's platform agnostic, it's still as wide as the address type, and on systems with no extra bits beyond the address, it's just zero.

Re: Making Rust a Better Fit for Cheri and Other Platforms

#9

It would also be nice to be able to use unsigned types (like u8, u16 and u32) to index into slices and arrays up until usize (usually a u32 or u64). Using a usize often seems wasteful for indexing into small arrays and slices and casting makes the code look ugly (also dangerous because the effect of casting is checked by the developer, not the compiler). Admittedly, this would have the downside of having code that co…

Code looks ugly should maybe not be a big concern in a language like Rust. It’s fundamentally ugly to begin with in many ways. And I think that’s fine because it is a language that is primarily about control.

One could argue that this is beautiful in its own way. There’s a clarity and power in being explicit while the constraints let you move on with confidence.

Re: Making Rust a Better Fit for Cheri and Other Platforms

#10

It would also be nice to be able to use unsigned types (like u8, u16 and u32) to index into slices and arrays up until usize (usually a u32 or u64). Using a usize often seems wasteful for indexing into small arrays and slices and casting makes the code look ugly (also dangerous because the effect of casting is checked by the developer, not the compiler). Admittedly, this would have the downside of having code that co…

Rust really doesn't do implicit numeric conversion, and I suspect it would be hard to retrofit. But you could write a function i(...) that promoted u8, etc, to usize:

    v[i(my_u8_index)]
This could be implemented as a compile-time check, with no runtime errors. You'd have to limit which types you support as indices if you want to be portable to platforms with tiny pointers, of course.

I guess something like this might be useful inside of specialized library code that worked with lots of small vectors.

But in general, Rust heavily favors explicit over implicit in many areas. If you want lots of automatic implicit behavior, something like Scala might be a better choice?

Post reply on HN