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