capnproto-rust
capnproto-rust copied to clipboard
Non-panicking `capnp::struct_list::Reader::get`
Hello!
Currently capnp::struct_list::Reader::get can panic if the index is out of bounds because of an assertion at https://docs.capnproto-rust.org/src/capnp/struct_list.rs.html#84
This is rather unexpected as the documentation doesn't say so and the usual convention for get method of collections is to return an Option<T> for in invalid index/key/etc.
Would it make sense to (1) change the prototype of capnp::struct_list::Reader::get to return an option and maybe (2) implement the Index trait with the old panicking behavior as done for slices in std?
I am willing to provide the PR, I am just unsure of what non backward compatible change are deemed acceptable.
We can't use std::ops::Index, because its index() method returns a reference, which we generally can't do because we may need to swap bytes of the value to account for endianness (in the case of primitive_list).
We have an IndexMove trait for this purpose: https://github.com/capnproto/capnproto-rust/blob/3f31a36058d45e3812147c6de62d451b7bfd7973/capnp/src/traits.rs#L106-L108
There have been proposals for adding something like it upstream: https://github.com/rust-lang/rfcs/issues/997
We can't use
std::ops::Index, because itsindex()method returns a reference, which we generally can't do because we may need to swap bytes of the value to account for endianness (in the case ofprimitive_list).
Right, that's unfortunate. That being said, I am more interested in having a non-panicking version of get methods.