pippin icon indicating copy to clipboard operation
pippin copied to clipboard

Casts between usize and u64

Open dhardy opened this issue 9 years ago • 2 comments

When converting between an index or length in-memory and a value saved in a file, is the cast safe?

Should we enforce usize == u64 or usize >= u64? Would the program work in 32-bit mode?

  • [ ] Code tag #0015

dhardy avatar Feb 23 '16 21:02 dhardy

Suggestion:

  • only support usize >= 32 (add assert)
  • add asserts wherever converting between different sizes; for most usage limits of 32-bit usize will not be exceeded

Note: usize is used for the number of elements per partition, but we only support 2^24 elements. usize is used for element data length, but format is far from ideal if data segments are gigabytes long. usize is also used for the number of changes in a commit (depends on application, but probably < 100).

Note: some sizes are restricted to 32-bits in file format already; probably all uses of usize could be restricted to 32-bits.

dhardy avatar Mar 06 '16 13:03 dhardy

Explicit integer casts: This is precisely because of a point you note slightly further down - that narrowing casts can cause difficult-to-diagnose bugs. With type inference on top of that you'll get serious problems. A way to force the panicking semantics you want is to use some_val.try_into().unwrap(), which is noisier than some_val as _ but makes it explicit.

dhardy avatar Sep 19 '16 16:09 dhardy