Skip to content

Commit

Permalink
ndk/media_format: Implement Debug in terms of Display
Browse files Browse the repository at this point in the history
Currently `Debug` only prints a raw pointer which is rather useless to
look at.  In addition to the pointer, also print the `Display`
representation of `MediaFormat` which uses Android's `toString()`
function to create a human-readable string of the various fields set
inside of it.

On a side-note it is "great" to see that `toString()` is not currently
available as a user function via a lifetimed `CStr` as it is invalidated
in a nontrivial way (e.g. when `toString()` is called again, and
possibly also when `set_()` functions are called which are not currently
borrowing it mutably).
  • Loading branch information
MarijnS95 committed Nov 20, 2023
1 parent b7d83a8 commit 4218769
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ndk/src/media/media_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@ use crate::media_error::MediaError;
/// A native [`AMediaFormat *`]
///
/// [`AMediaFormat *`]: https://developer.android.com/ndk/reference/group/media#amediaformat
#[derive(Debug)]
pub struct MediaFormat {
inner: NonNull<ffi::AMediaFormat>,
}

impl fmt::Display for MediaFormat {
/// Human readable representation of the format.
#[doc(alias = "AMediaFormat_toString")]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let c_str = unsafe { CStr::from_ptr(ffi::AMediaFormat_toString(self.as_ptr())) };
f.write_str(c_str.to_str().unwrap())
}
}

impl fmt::Debug for MediaFormat {
#[doc(alias = "AMediaFormat_toString")]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MediaFormat({:?}: {})", self.inner, self)
}
}

impl Default for MediaFormat {
fn default() -> Self {
Self::new()
Expand Down

0 comments on commit 4218769

Please sign in to comment.