-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove Alloc::{alloc,dealloc}_array
and Alloc::{alloc,dealloc}_one
APIs
#18
Comments
I like the idea of leaning on |
The same applies to |
Yes, nice catch! EDIT: I've updated the issue to reflect this. We should also consider if we want to, at least for an MVP, and as a "design guideline", ban all generic methods in |
Alloc::{alloc,dealloc}_array
APIsAlloc::{alloc,dealloc}_array
and Alloc::{alloc,dealloc}_one
APIs
(This is the first I hear about deprecating |
What does MVP mean in this context? I guess you don't mean Most Valuable Player. |
I don't see why we should deprecate |
Minimum Viable Project. The smallest incremental library and language change that we could make to deliver the most value and that we could then continue to iterate on. As opposed to an RFC that attempts to cover all imaginable use cases, but due to its size becomes un-reviewable, un-mergeable, and is never shipped. |
Looking at the implementation of the methods that would be removed: https://github.com/rust-lang/rust/blob/1.34.1/src/libcore/alloc.rs#L1035-L1231 They call corresponding |
Note that the docs say: "For zero-sized So I don't think that, at least with the current API, we can clearly say that this would be a bug. If we were to ban zero-sized allocations (#16) then I'd suppose we would enforce that at the API boundary by using a Note also that with the current API and implementation, Maybe we could fix this by adding an associated const to |
As far as I can tell removing these methods precludes type aware allocators. Without type information the If generics in the API is the problem at least for my purposes replacements using alloc_typed(&mut self, layout: Layout, _: TypeId) -> Result<NonNull<u8>, AllocErr> {
alloc(self, layout)
} On Different note why avoid Generics? It's unclear to me why generics inherently limit |
@Avi-D-coder What you want is a very reasonable thing to want, but it is unclear to me that adding generic methods with the purpose of allocating arrays is the best way to support type aware allocators. Type aware allocators deserve a better solution than just hacking on the fact that the array methods are accidentally generic. |
GlobalAlloc methods just call a symbol in the binary. If the method is generic, no such symbol can easily exist. |
@gnzlbg What would you consider a better solution?
Linking is a useful property to preserve. In that case I agree with you that the generics be removed, but they should be atomically replaced with I would be willing to add a these methods to nightly to try and get a feel of if they work? I would argue they also be added to |
@Avi-D-coder I think such solutions are being discussed in #15 , maybe you can chime in with your use case there? |
You are right, I misread. I though that issue was about type-aware allocators (but it is about allocator-aware types). I don't think we currently have an issue tracking type-aware allocators. Maybe you could open one ? |
I like to remove Actually, they add nothing to
With the current API, For now we should focus on a MVP for If you have any concerns, please post them. @Amanieu I'd like to r? you when pushing the changes if that is ok? CC @gnzlbg @Ericson2314 @scottjmaddox @glandium @Wodann @Lokathor @Avi-D-coder |
I am no longer attempting to use the Alloc API heavily. One of the reasons is that the that the Anyhow I no longer have any objections to removing these methods. Though I believe we will come to regret bytes as a allocation primitive. |
I don't see any objection against an extension trait that provides default implementations for all of the above trait functions. |
I'm happy to have these methods removed.
Sure |
I'd propose that these go away from the trait but then they get restored as free functions (in whatever module) that do the necessary layout computation and allocation call for you, if they get restored at all. These don't make sense as trait methods or extension trait methods because individual allocators don't have any reason to override the default version of how you'd allocate/deallocate an array. Making the free functions let's people have a little help in their code without ever having to worry that a particular allocator did something weird. |
@Lokathor if I understand correctly, extension traits with blanket impls cannot be implemented for user types, due to the lack of specialization. |
Remove common usage pattern from `AllocRef` This removes the common usage patterns from `AllocRef`: - `alloc_one` - `dealloc_one` - `alloc_array` - `realloc_array` - `dealloc_array` Actually, they add nothing to `AllocRef` except a [convenience wrapper around `Layout` and other methods in this trait](https://doc.rust-lang.org/1.41.0/src/core/alloc.rs.html#1076-1240) but have a major flaw: The documentation of `AllocRefs` notes, that > some higher-level allocation methods (`alloc_one`, `alloc_array`) are well-defined on zero-sized types and can optionally support them: it is left up to the implementor whether to return `Err`, or to return `Ok` with some pointer. With the current API, `GlobalAlloc` does not have those methods, so they cannot be overridden for `liballoc::Global`, which means that even if the global allocator would support zero-sized allocations, `alloc_one`, `alloc_array`, and `realloc_array` for `liballoc::Global` will error, while calling `alloc` with a zeroed-size `Layout` could succeed. Even worse: allocating with `alloc` and deallocating with `dealloc_{one,array}` could end up with not calling `dealloc` at all! For the full discussion please see rust-lang/wg-allocators#18 r? @Amanieu
We should evaluate removing the
Alloc::alloc_array
andAlloc::dealloc_array
APIs.The main issue I have with these APIs is that they too easily allow creating zero-sized allocations if either T is a ZST, or the array length is zero. They also feel redundant and confusing for the
Alloc
trait, which is already quite complicated, e.g., Do I have to allocate / deallocate arrays with these? Can I mix match, e.g.,Alloc::alloc_array
withAlloc::dealloc
and vice-versa? etc..Ideally,
Alloc
andGlobalAlloc
would "converge" some day, e.g., such that you can just implementAlloc
for a#[global_allocator]
and that's it, instead of having to implement multiple traits. Having generic methods inAlloc
does make that a bit harder due to how#[global_allocator]
is implemented internally and the constraints on that.Layout
already has an unstableLayout::array<T>(n: usize)
method that can be used instead, and that feels like a much better place to put this functionality anyways. So just removing them, at least for the time being, sounds appealing to me. We could always add these later in a backwards compatible way anyways, but we don't have to have them now.EDIT: as @TimDiekmann mentions all of this applies to
alloc_one
/dealloc_one
as well.The text was updated successfully, but these errors were encountered: