rust-lang / wg-allocators

Home of the Allocators working group: Paving a path for a standard set of allocator traits to be used in collections!
http://bit.ly/hello-wg-allocators
209 stars 9 forks source link

How to alloc_zeroed with new Allocator trait? #80

Closed berkus closed 3 years ago

berkus commented 3 years ago

I used to have this code which uses alloc_zeroed previously in AllocRef (implemented via this).

I've updated to the new alllocator interface (AllocRef -> Allocator), but now it fails to build and I'm not sure how can I replace it, because I don't have a GlobalAllocator and don't want to implement it for my BumpAllocator class.

Any advice?

TimDiekmann commented 3 years ago

You use use core::alloc::AllocRef; which is not available in the latest nightly anymore. Replace it with use core::alloc::Allocator and call it with dma.allocate_zeroed. The trait and the methods just got renamed recently.

berkus commented 3 years ago

Yes, that's what I'm saying - I renamed it to Allocator (I just wrote that in the second sentence!) and it started to fail compilation.

error[E0599]: no method named `alloc_zeroed` found for mutable reference `&mut BumpAllocator` in the current scope
   --> nucleus/src/platform/rpi3/mailbox.rs:364:21
    |
364 |                 dma.alloc_zeroed(
    |                     ^^^^^^^^^^^^ method not found in `&mut BumpAllocator`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
    = note: the following trait defines an item `alloc_zeroed`, perhaps you need to implement it:
            candidate #1: `GlobalAlloc`

I can't have GlobalAlloc because I'm in no_std no_main no_alloc land!

berkus commented 3 years ago

How do I reopen that, do I need to file a new ticket?

berkus commented 3 years ago

Even if i import the GlobalAlloc trait it's definitely not implemented anywhere, so there's no implementation of alloc_zeroed available.

error: unused import: `core::alloc::GlobalAlloc`
   --> nucleus/src/platform/rpi3/mailbox.rs:361:13
    |
361 |         use core::alloc::GlobalAlloc;
    |             ^^^^^^^^^^^^^^^^^^^^^^^^
    |
Amanieu commented 3 years ago

The method has been renamed to allocate_zeroed.

berkus commented 3 years ago

Thank you @Amanieu !

Unfortunately I couldn't find any mention of that in the tickets.

TimDiekmann commented 3 years ago

Replace it with use core::alloc::Allocator and call it with dma.allocate_zeroed. The trait and the methods just got renamed recently.

berkus commented 3 years ago

@TimDiekmann yeah, I'm dumb and can't read apparently, sorry about that.

It all compiled now, I can develop aarch64-unknown-none-softfloat from aarch64-apple-darwin now, hurray!

TimDiekmann commented 3 years ago

No problem, nice to hear everything works now! :slightly_smiling_face: