Add reserve ergonomic wrappers for split_at_mut and array_mut_ref#10
Add reserve ergonomic wrappers for split_at_mut and array_mut_ref#10burdges wants to merge 1 commit intodroundy:masterfrom
reserve ergonomic wrappers for split_at_mut and array_mut_ref#10Conversation
These are convenient when you want to build a struct that contains a variety of references into a slice.
|
On looking at this more closely, I have a different proposal that might serve the same need while being closer in API and naming to the rest of arrayref. What you are aiming for with reserve seems to be very close to array_refs, with the caveat that you want a slice as input. The main difference being that you accept a could be more cleanly written as something like Now I don't have time for the work to get this macro doing this, but I think it would be way nicer as an API, and would avoid the distinction between tail and head. It would also enable taking several arrays off the top without having several bounds checks, so there is a potential efficiency gain as well. This syntax would move arrayref a bit closer to a stable rust version of advanced slice patterns, which really ought to make arrayref obsolete once it stabilizes (assuming they do it right!). (former notes below) I would lean against exporting your functions that reserve slices, as the point of arrayref is to provide array references, and since the array references can always be used where a slice would be used. That would simplify the API which would seem a plus. I'd like to see some cleanup before pulling this: clearer docs, and removing the public functions in favor of the macros (which I believe can do anything the functions can do, with no overhead required). But I must say that I find the documentation somewhat confusing: on a first glance I can't see what these functions do. The description involving heap.split_at_mut is not so entirely clear to me, and I expect a more clear description could be given in plain words. If we don't export the functions, but only the macros, we would have two macros which would be used like: would take 5 elements out of the beginning of the slice, and make would do the same for the last 5 elements of the slice. I don't really care for the "reserve" name. This looks like a "popping" operation. |
|
I've no opinion on the name myself. It originated from ad hoc arena allocators and makes a mess of taking postfixes. Just I write things like I also like your idea of Yet, there are situations where this |
These are wrappers for combinations of
slice::split_atorslice::split_at_mutwitharray_refandarray_mut_refrespectively, that simplify handling a variety of references into a slice. In essence, they use a call tomem::replaceto keep the borrow checker happy while doing roughly this :These functions are handy if you need to borrow slices from binary data, put them into structs of mutable references, and manipulate them. I donno if they really belong in this crate, but it seemed like plausible place. There are only doc tests right now.
I donno if the name
reserveis really ideal either. We could rename these toprefix_*andpostfix_*or something. I do not use thereserve_tailforms anywhere yet myself, so I kept the name reserve.