Struct midly::Arena

source ·
pub struct Arena { /* private fields */ }
Expand description

Helps overcome limitations of the lifetime system when constructing MIDI events and files.

Because many events contain references to data that outlives them, it can be hard to build a MIDI file programatically.

Consider the following code:

use midly::{TrackEvent, TrackEventKind, MetaMessage};

let mut track = Vec::new();
for i in 0..64 {
    let marker_name = format!("Marker {}", i);
    let marker_ref = marker_name.as_bytes();
    track.push(TrackEvent {
        delta: 0.into(),
        kind: TrackEventKind::Meta(MetaMessage::Marker(marker_ref)),
    });
}

Looks pretty good, but it fails to compile with error[E0597]: "marker_name" does not live long enough, with a rightful reason: marker_name is dropped before the next iteration of the for loop.

Instead, use the Arena type like the following code:

use midly::{TrackEvent, TrackEventKind, MetaMessage};

let arena = midly::Arena::new();
let mut track = Vec::new();
for i in 0..64 {
    let marker_name = format!("Marker {}", i);
    let marker_ref = arena.add(marker_name.as_bytes());
    track.push(TrackEvent {
        delta: 0.into(),
        kind: TrackEventKind::Meta(MetaMessage::Marker(marker_ref)),
    });
}

This type is only available with the alloc feature enabled.

Implementations§

Create a new empty arena.

Empty this arena, deallocating all added bytes.

This method is safe to call because it requires a mutable reference.

Get the amount of allocations in the arena.

Add a set of bytes to the arena, returning a longer-lived mutable reference to a copy of these same bytes.

Add a Vec<u8> to the arena, returning a long-lived mutable reference to its contents.

This method is very similar to add, but avoids an allocation and a copy.

Add a set of databytes to the arena, returning a longer-lived mutable reference to a copy of these same databytes.

Add a Vec<u7> to the arena, returning a long-lived mutable reference to its contents.

This method is very similar to add_u7, but avoids an allocation and a copy.

Trait Implementations§

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.