Add more IntoIterator implementations

This commit is contained in:
Uwe Klotz 2023-01-11 21:07:54 +01:00 committed by Alex
parent 0616da11fb
commit 3df98aff12
5 changed files with 59 additions and 25 deletions

View file

@ -99,11 +99,6 @@ impl ApeTag {
self.items.retain(|i| !i.key().eq_ignore_ascii_case(key));
}
/// Returns all of the tag's items
pub fn items(&self) -> &[ApeItem] {
&self.items
}
fn split_num_pair(&self, key: &str) -> (Option<u32>, Option<u32>) {
if let Some(ApeItem {
value: ItemValue::Text(ref text),
@ -118,6 +113,24 @@ impl ApeTag {
}
}
impl IntoIterator for ApeTag {
type Item = ApeItem;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.items.into_iter()
}
}
impl<'a> IntoIterator for &'a ApeTag {
type Item = &'a ApeItem;
type IntoIter = std::slice::Iter<'a, ApeItem>;
fn into_iter(self) -> Self::IntoIter {
self.items.iter()
}
}
impl Accessor for ApeTag {
impl_accessor!(
artist => "Artist";
@ -458,10 +471,10 @@ mod tests {
let header = read_ape_header(&mut reader, false).unwrap();
let parsed_tag = crate::ape::tag::read::read_ape_tag(&mut reader, header).unwrap();
assert_eq!(expected_tag.items().len(), parsed_tag.items().len());
assert_eq!(expected_tag.len(), parsed_tag.len());
for item in expected_tag.items() {
assert!(parsed_tag.items().contains(item));
for item in &expected_tag.items {
assert!(parsed_tag.items.contains(item));
}
}

View file

@ -94,13 +94,22 @@ pub struct ID3v2Tag {
impl IntoIterator for ID3v2Tag {
type Item = Frame<'static>;
type IntoIter = std::vec::IntoIter<Frame<'static>>;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.frames.into_iter()
}
}
impl<'a> IntoIterator for &'a ID3v2Tag {
type Item = &'a Frame<'static>;
type IntoIter = std::slice::Iter<'a, Frame<'static>>;
fn into_iter(self) -> Self::IntoIter {
self.frames.iter()
}
}
impl Default for ID3v2Tag {
fn default() -> Self {
Self {
@ -132,16 +141,6 @@ impl ID3v2Tag {
}
impl ID3v2Tag {
/// Returns an iterator over the tag's frames
pub fn iter(&self) -> impl Iterator<Item = &Frame<'static>> {
self.frames.iter()
}
/// Returns the number of frames in the tag
pub fn len(&self) -> usize {
self.frames.len()
}
/// Gets a [`Frame`] from an id
///
/// NOTE: This is *not* case-sensitive

View file

@ -81,11 +81,6 @@ impl RIFFInfoList {
.position(|(k, _)| k.eq_ignore_ascii_case(key))
.map(|p| self.items.remove(p));
}
/// Returns the tag's items in (key, value) pairs
pub fn items(&self) -> &[(String, String)] {
self.items.as_slice()
}
}
impl Accessor for RIFFInfoList {
@ -130,6 +125,24 @@ impl Accessor for RIFFInfoList {
}
}
impl IntoIterator for RIFFInfoList {
type Item = (String, String);
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.items.into_iter()
}
}
impl<'a> IntoIterator for &'a RIFFInfoList {
type Item = &'a (String, String);
type IntoIter = std::slice::Iter<'a, (String, String)>;
fn into_iter(self) -> Self::IntoIter {
self.items.iter()
}
}
impl TagExt for RIFFInfoList {
type Err = LoftyError;
type RefKey<'a> = &'a str;

View file

@ -219,7 +219,7 @@ impl<'a> IntoIterator for &'a Ilst {
impl IntoIterator for Ilst {
type Item = Atom<'static>;
type IntoIter = std::vec::IntoIter<Atom<'static>>;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.atoms.into_iter()

View file

@ -115,6 +115,15 @@ impl IntoIterator for Tag {
}
}
impl<'a> IntoIterator for &'a Tag {
type Item = &'a TagItem;
type IntoIter = std::slice::Iter<'a, TagItem>;
fn into_iter(self) -> Self::IntoIter {
self.items.iter()
}
}
impl Accessor for Tag {
impl_accessor!(
TrackArtist => artist,