mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2024-11-10 06:34:18 +00:00
impl downcasts
just accidentally discovered that I could write impl From<Box<dyn xxx>>
This commit is contained in:
parent
84e15b3172
commit
0418d98955
6 changed files with 17 additions and 9 deletions
|
@ -10,6 +10,8 @@
|
|||
|
||||
This crate aims to provide a unified trait for parsers and writers of different audio file formats. This means that you can parse tags in mp3, flac, and m4a files with a single function: `Tag::default().read_from_path()` and get fields by directly calling `.album()`, `.artist()` on its result. Without this crate, you would otherwise need to learn different APIs in **id3**, **mp4ameta** etc. in order to parse metadata in different file formats.
|
||||
|
||||
I'm relatively new to Rust and programming in general, and this is my first attempt to make a non-trivial crate. If you see anything that you think isn't right, just say it!
|
||||
|
||||
## Examples
|
||||
|
||||
Examples can be found in the [manual](https://tianyishi2001.github.io/audiotags).
|
||||
|
|
BIN
assets/a.mp3
BIN
assets/a.mp3
Binary file not shown.
|
@ -1,3 +1,10 @@
|
|||
#[macro_export]
|
||||
macro_rules! downcast {
|
||||
($tag:expr, $type:ty) => {
|
||||
$tag.into_any().downcast_ref::<$type>().unwrap().into()
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_audiotag_config {
|
||||
($tag:ident) => {
|
||||
|
@ -58,5 +65,11 @@ macro_rules! impl_tag {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<dyn AudioTag>> for $inner {
|
||||
fn from(inp: Box<dyn AudioTag>) -> Self {
|
||||
downcast!(inp, $tag)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ fn main() {
|
|||
.expect("Fail to read!");
|
||||
assert_eq!(id3tag_reload.title(), Some("title from metaflac::Tag"));
|
||||
|
||||
let mut id3tag_inner: id3::Tag = downcast!(id3tag_reload, Id3v2Tag);
|
||||
let mut id3tag_inner: id3::Tag = id3tag_reload.into();
|
||||
let timestamp = id3::Timestamp {
|
||||
year: 2013,
|
||||
month: Some(2u8),
|
||||
|
|
|
@ -144,10 +144,3 @@ impl Tag {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! downcast {
|
||||
($tag:expr, $type:ty) => {
|
||||
$tag.into_any().downcast_ref::<$type>().unwrap().into()
|
||||
};
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ fn test_inner() {
|
|||
.expect("Fail to read!");
|
||||
assert_eq!(id3tag_reload.title(), Some("title from metaflac::Tag"));
|
||||
|
||||
let mut id3tag_inner: id3::Tag = downcast!(id3tag_reload, Id3v2Tag);
|
||||
let mut id3tag_inner: id3::Tag = id3tag_reload.into();
|
||||
let timestamp = id3::Timestamp {
|
||||
year: 2013,
|
||||
month: Some(2u8),
|
||||
|
|
Loading…
Reference in a new issue