Skip to content

Commit c078126

Browse files
committed
MP4: Make Ilst::remove return all removed atoms
1 parent abf6b40 commit c078126

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

src/mp4/ilst/mod.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ macro_rules! impl_accessor {
5050
}
5151

5252
fn [<remove_ $name>](&mut self) {
53-
self.remove(&$const)
53+
let _ = self.remove(&$const);
5454
}
5555
)+
5656
}
@@ -142,8 +142,29 @@ impl Ilst {
142142
}
143143

144144
/// Inserts an [`Atom`], replacing any atom with the same [`AtomIdent`]
145+
///
146+
/// # Examples
147+
///
148+
/// ```rust
149+
/// use lofty::mp4::{Atom, AtomData, AtomIdent, Ilst};
150+
/// use lofty::Accessor;
151+
///
152+
/// const TITLE_IDENTIFIER: AtomIdent = AtomIdent::Fourcc(*b"\xa9nam");
153+
///
154+
/// let mut ilst = Ilst::new();
155+
///
156+
/// ilst.set_title(String::from("FooBar"));
157+
/// assert_eq!(ilst.title().as_deref(), Some("FooBar"));
158+
///
159+
/// // Replace our old title
160+
/// ilst.replace_atom(Atom::new(
161+
/// TITLE_IDENTIFIER,
162+
/// AtomData::UTF8(String::from("BarFoo")),
163+
/// ));
164+
/// assert_eq!(ilst.title().as_deref(), Some("BarFoo"));
165+
/// ```
145166
pub fn replace_atom(&mut self, atom: Atom<'_>) {
146-
self.remove(&atom.ident);
167+
let _ = self.remove(&atom.ident);
147168
self.atoms.push(atom.into_owned());
148169
}
149170

@@ -165,16 +186,24 @@ impl Ilst {
165186
/// assert!(title.is_some());
166187
///
167188
/// // Remove the title
168-
/// ilst.remove(&TITLE_IDENTIFIER);
189+
/// let returned = ilst.remove(&TITLE_IDENTIFIER);
190+
/// assert_eq!(returned.count(), 1);
169191
///
170192
/// let title = ilst.get(&TITLE_IDENTIFIER);
171193
/// assert!(title.is_none());
172194
/// ```
173-
pub fn remove(&mut self, ident: &AtomIdent<'_>) {
174-
self.atoms
175-
.iter()
176-
.position(|a| &a.ident == ident)
177-
.map(|p| self.atoms.remove(p));
195+
pub fn remove(&mut self, ident: &AtomIdent<'_>) -> impl Iterator<Item = Atom<'static>> + '_ {
196+
// TODO: drain_filter
197+
let mut split_idx = 0_usize;
198+
199+
for read_idx in 0..self.atoms.len() {
200+
if &self.atoms[read_idx].ident == ident {
201+
self.atoms.swap(split_idx, read_idx);
202+
split_idx += 1;
203+
}
204+
}
205+
206+
self.atoms.drain(..split_idx)
178207
}
179208

180209
/// Retain atoms based on the predicate
@@ -316,7 +345,7 @@ impl Accessor for Ilst {
316345
}
317346

318347
fn remove_track(&mut self) {
319-
self.remove(&AtomIdent::Fourcc(*b"trkn"));
348+
let _ = self.remove(&AtomIdent::Fourcc(*b"trkn"));
320349
}
321350

322351
fn track_total(&self) -> Option<u32> {
@@ -333,7 +362,7 @@ impl Accessor for Ilst {
333362

334363
fn remove_track_total(&mut self) {
335364
let track_num = self.track();
336-
self.remove(&AtomIdent::Fourcc(*b"trkn"));
365+
let _ = self.remove(&AtomIdent::Fourcc(*b"trkn"));
337366

338367
if let Some(track_num) = track_num {
339368
let track_bytes = (track_num as u16).to_be_bytes();
@@ -356,7 +385,7 @@ impl Accessor for Ilst {
356385
}
357386

358387
fn remove_disk(&mut self) {
359-
self.remove(&AtomIdent::Fourcc(*b"disk"));
388+
let _ = self.remove(&AtomIdent::Fourcc(*b"disk"));
360389
}
361390

362391
fn disk_total(&self) -> Option<u32> {
@@ -373,7 +402,7 @@ impl Accessor for Ilst {
373402

374403
fn remove_disk_total(&mut self) {
375404
let disk_num = self.disk();
376-
self.remove(&AtomIdent::Fourcc(*b"disk"));
405+
let _ = self.remove(&AtomIdent::Fourcc(*b"disk"));
377406

378407
if let Some(disk_num) = disk_num {
379408
let disk_bytes = (disk_num as u16).to_be_bytes();
@@ -401,7 +430,7 @@ impl Accessor for Ilst {
401430
}
402431

403432
fn remove_year(&mut self) {
404-
self.remove(&AtomIdent::Fourcc(*b"Year"));
433+
let _ = self.remove(&AtomIdent::Fourcc(*b"Year"));
405434
}
406435
}
407436

0 commit comments

Comments
 (0)