My personal site cherry.computer
htmx tailwind axum askama

refactor: pass scrobble as reference to ScrobblesTemplate

I was trying to reduce the number of clones before by moving into the
Vec fields of the Scrobble, but into_iter().next() still calls
ptr::read() under the hood which does a bitwise copy so you're still
beholden to the compiler as to whether it's optimised away or not and
you get more unreadable code.

cherry.computer 614fe8fb 45e23b96

verified
+5 -13
+4 -12
server/src/scrobble.rs
··· 49 49 } 50 50 51 51 impl ScrobblesTemplate { 52 - pub fn new(scrobble: Scrobble) -> ScrobblesTemplate { 53 - let latest_track = scrobble 54 - .recent_tracks 55 - .track 56 - .into_iter() 57 - .next() 58 - .expect("no tracks were returned"); 52 + pub fn new(scrobble: &Scrobble) -> ScrobblesTemplate { 53 + let latest_track = &scrobble.recent_tracks.track[0]; 59 54 let srcset = latest_track.image.get(0..3).map(|images| { 60 55 format!( 61 56 "{}, {} 2x, {} 3x", ··· 64 59 }); 65 60 let text_intro = if latest_track 66 61 .attributes 62 + .as_ref() 67 63 .is_some_and(|attr| attr.now_playing == "true") 68 64 { 69 65 "Now playing: " ··· 75 71 ScrobblesTemplate { 76 72 intro: text_intro, 77 73 now_playing, 78 - image: latest_track 79 - .image 80 - .into_iter() 81 - .next() 82 - .map(|image| image.text), 74 + image: latest_track.image.first().map(|image| image.text.clone()), 83 75 srcset, 84 76 } 85 77 }
+1 -1
server/src/scrobble_monitor.rs
··· 63 63 _ => { 64 64 tracing::debug!("fetching new scrobble data"); 65 65 let scrobble = self.fetch_scrobble().await?; 66 - let scrobble_partial = ScrobblesTemplate::new(scrobble); 66 + let scrobble_partial = ScrobblesTemplate::new(&scrobble); 67 67 *last_scrobble = Some(CachedScrobble { 68 68 data: scrobble_partial.clone(), 69 69 fetch_time: Instant::now(),