pcwalton / rust-media

A free, comprehensive, and portable video/audio streaming library for Rust
Apache License 2.0
787 stars 57 forks source link

WIP Refactor Track to have a cleaner type-safe API #15

Closed Gankra closed 9 years ago

Gankra commented 9 years ago

This is built on top of #14, with the newest commit being the new change.

This merges the Track::track_type and Track::as_video_track functionality into a single method that returns a tagged union. The end result is that clients have to write less boiler-plate, and the API is more type-safe.

There were some speed-bumps in doing this, though. It turns out that the separation of functionality was necessary in the old design to ensure that all the necessary temporaries were alive. However after observing the current concrete implementations, it appeared that this wasn't necessary. Every single concrete implementation was basically cloning self into a type with identical repr.

As such Track::track_type has been changed to take Box<Self>. This has two benefits: it moves self into the function, removing any need for the resultant cast to have a "reference" to the parent; and it allows the allocation from the original to be reused. In fact in principle all of the impls could basically be replaced with a match where every branch transmutes the Box.

However to accomplish this Track, AudioTrack, and VideoTrack needed to acquire a lifetime parameter. This is basically fine, since they're always intended to be behind a Box anyway.

As a consequence, the impl TrackExt is busted because it wants to peek into what type each track is temporarily, but this is not a consuming operation.

Also mixed in are some random cleanups.

pcwalton commented 9 years ago

Looks OK to me generally, just a couple of minor nits