Conversation
|
I'm thinking about what the correct API for this is. What I'm trying to keep in mind is that the non-emptiness of the collection is what we're always trying to maintain. With that in mind, I think I'd prefer something like: /// Removes the element found at `index`.
///
/// If the index exists, then the element is returned, alongside the other elements as a `Vec<T>`.
///
/// If the index is out of bounds then the `NonEmpty<T>` is returned as-is.
pub fn remove(self, index: usize) -> Result<(T, Vec<T>), NonEmpty<T>> {
/* index shenanigans */
}Note that we cannot guarantee that we have a non-empty collection as soon as we remove, so the caller can call For pub fn swap_remove(self, index: usize) -> Result<NonEmpty<T>, NonEmpty<T>> {
/* index shenanigans */
}Noting here that we can actually return a How do you feel about this? Would you be up for trying out a version of the PR that has this API? |
|
My intention for the currently implemented I personally don't like having to take the NonEmpty struct by value in these methods, and prefer Vec's API of taking a mutable reference. Any reason to not go with the current approach where we just return an error in the case where there's only one element or if index is out of bounds? |
Adds the following methods
checked_remove: checks if the element at a certain index can be removed from the vector, then removes it, shifting all elements after it to the left. If it is not possible to remove, returns aRemoveErrorremove: calls checked_remove, then unwrapschecked_swap_remove: checks if the element at a certain index can be removed from the vector, then removes it, replacing it with the last element. If it is not possible to remove, returns aRemoveErrorswap_remove: calls checked_swap_remove, then unwrapsAll of the above methods return the removed element (if any).
Also add a
RemoveErrorenum, with two variants:LengthOne: to avoid removing from a NonEmpty of length 1IndexOutOfBounds: for index larger than or equal to the length of the NonEmpty