vcombey / fallible_collections

impl fallible collections in rust, quite as describe in RFC 2116
Apache License 2.0
31 stars 14 forks source link

Add types which require fallible allocation #2

Closed baumanj closed 4 years ago

baumanj commented 4 years ago

This crate currently supports fallible allocation on stdlib collections by adding additional functions via new traits. For example, implementing the FallibleVec trait for std::vec::Vec adds methods like try_push, which return a Result indicating the potential for allocation failure (as opposed to Vec's push which returns nothing).

This allows the crate to be introduced easily in code already using stdlib containers, but it requires the consumer to determine whether or not fallibility is desired for each call, and to be aware of all the calls which potentially allocate. To support consumers which desire consistent use of fallible allocation, this crate can also provide distinct types (e.g., TryVec as an alternative to std::vec::Vec) which provide the same function names as the stdlib containers, but add Result return values to any functions which potentially allocate and do not implement the traits which may allocate with no mechanism to return failure such as Clone. (They may implement alternative traits such as TryClone if necessary.)

Some initial work along these lines has already been done in the mp4parse-rust crate, but it has value as a more general library. Adding it to this crate should facilitate code sharing and discoverability.