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§
Source§impl Arena
impl Arena
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Empty this arena, deallocating all added bytes.
This method is safe to call because it requires a mutable reference.
Sourcepub fn add<'a, 'b>(&'a self, bytes: &'b [u8]) -> &'a mut [u8] ⓘ
pub fn add<'a, 'b>(&'a self, bytes: &'b [u8]) -> &'a mut [u8] ⓘ
Add a set of bytes to the arena, returning a longer-lived mutable reference to a copy of these same bytes.
Sourcepub fn add_vec<'a>(&'a self, bytes: Vec<u8>) -> &'a mut [u8] ⓘ
pub fn add_vec<'a>(&'a self, bytes: Vec<u8>) -> &'a mut [u8] ⓘ
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.
Sourcepub fn add_u7<'a, 'b>(&'a self, databytes: &'b [u7]) -> &'a mut [u7]
pub fn add_u7<'a, 'b>(&'a self, databytes: &'b [u7]) -> &'a mut [u7]
Add a set of databytes to the arena, returning a longer-lived mutable reference to a copy of these same databytes.
Sourcepub fn add_u7_vec<'a>(&'a self, databytes: Vec<u7>) -> &'a mut [u7]
pub fn add_u7_vec<'a>(&'a self, databytes: Vec<u7>) -> &'a mut [u7]
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§
Auto Trait Implementations§
impl !Freeze for Arena
impl !RefUnwindSafe for Arena
impl !Sync for Arena
impl Unpin for Arena
impl UnwindSafe for Arena
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more