From 9845a94d1ce505fb4b5ea1764d15767d1e2d212d Mon Sep 17 00:00:00 2001 From: legendofmiracles Date: Tue, 6 Apr 2021 07:46:16 +0200 Subject: [PATCH] media-converter: Make Rust more idiomatic --- media-converter/src/audioconv.rs | 22 +++++++++++----------- media-converter/src/fossilize.rs | 4 ++-- media-converter/src/videoconv.rs | 9 +++------ 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/media-converter/src/audioconv.rs b/media-converter/src/audioconv.rs index e845eb2b..28904a95 100644 --- a/media-converter/src/audioconv.rs +++ b/media-converter/src/audioconv.rs @@ -217,7 +217,7 @@ struct NeedTranscodeHead { impl NeedTranscodeHead { fn new_from_caps(caps: &gst::CapsRef) -> Result { - let s = caps.get_structure(0).ok_or(gst_loggable_error!(CAT, "Caps have no WMA data!"))?; + let s = caps.get_structure(0).ok_or_else(|| gst_loggable_error!(CAT, "Caps have no WMA data!"))?; let wmaversion = s.get_some::("wmaversion").map_err(|_| gst_loggable_error!(CAT, "Caps have no wmaversion field"))?; let bitrate = s.get_some::("bitrate").map_err(|_| gst_loggable_error!(CAT, "Caps have no bitrate field"))?; @@ -227,7 +227,7 @@ impl NeedTranscodeHead { let depth = s.get_some::("depth").map_err(|_| gst_loggable_error!(CAT, "Caps have no depth field"))?; let codec_data_buf = s.get::("codec_data") .map_err(|_| gst_loggable_error!(CAT, "Caps have no codec_data field"))? - .ok_or(gst_loggable_error!(CAT, "Caps have NULL codec_data field"))?; + .ok_or_else(|| gst_loggable_error!(CAT, "Caps have NULL codec_data field"))?; let mapped = codec_data_buf.into_mapped_buffer_readable().unwrap(); let mut codec_data = Vec::new(); @@ -311,7 +311,7 @@ impl StreamState { Ok(LoopState::Looping) }else{ - if self.loop_buffers.len() > 0 { + if !self.loop_buffers.is_empty() { /* partial loop, track them and then continue */ self.buffers.append(&mut self.loop_buffers); } @@ -324,7 +324,7 @@ impl StreamState { } fn write_to_foz(&self) -> Result<(), gst::LoggableError> { - if self.needs_dump && self.buffers.len() > 0 { + if self.needs_dump && !self.buffers.is_empty() { let mut db = (*DUMP_FOZDB).lock().unwrap(); let db = match &mut *db { Some(d) => d, @@ -337,13 +337,13 @@ impl StreamState { /* are there any recorded streams of which this stream is a subset? */ let stream_ids = db.iter_tag(AUDIOCONV_FOZ_TAG_STREAM).cloned().collect::>(); - found = stream_ids.iter().find(|stream_id| { + found = stream_ids.iter().any(|stream_id| { let mut offs = 0; for cur_buf_id in self.buffers.iter() { let mut buf = [0u8; 16]; - let res = db.read_entry(AUDIOCONV_FOZ_TAG_STREAM, **stream_id, offs, &mut buf, fossilize::CRCCheck::WithCRC); + let res = db.read_entry(AUDIOCONV_FOZ_TAG_STREAM, *stream_id, offs, &mut buf, fossilize::CRCCheck::WithCRC); let buffer_id = match res { Err(_) => { return false; } @@ -362,9 +362,9 @@ impl StreamState { offs += 16; } - gst_trace!(CAT, "stream id {} is a subset of {}, so not recording stream", self.cur_hash, **stream_id); + gst_trace!(CAT, "stream id {} is a subset of {}, so not recording stream", self.cur_hash, *stream_id); return true; - }).is_some(); + }); } if !found { @@ -510,7 +510,7 @@ impl AudioConvState { if let Ok(transcoded_size) = read_fozdb.entry_size(AUDIOCONV_FOZ_TAG_PTNADATA, hash) { /* success */ let mut buf = vec![0u8; transcoded_size].into_boxed_slice(); - if let Ok(_) = read_fozdb.read_entry(AUDIOCONV_FOZ_TAG_PTNADATA, hash, 0, &mut buf, fossilize::CRCCheck::WithoutCRC) { + if read_fozdb.read_entry(AUDIOCONV_FOZ_TAG_PTNADATA, hash, 0, &mut buf, fossilize::CRCCheck::WithoutCRC).is_ok() { return Ok((buf, 0.0)); } } @@ -518,7 +518,7 @@ impl AudioConvState { if let Ok(transcoded_size) = read_fozdb.entry_size(AUDIOCONV_FOZ_TAG_PTNADATA, loop_hash) { /* success */ let mut buf = vec![0u8; transcoded_size].into_boxed_slice(); - if let Ok(_) = read_fozdb.read_entry(AUDIOCONV_FOZ_TAG_PTNADATA, loop_hash, 0, &mut buf, fossilize::CRCCheck::WithoutCRC) { + if read_fozdb.read_entry(AUDIOCONV_FOZ_TAG_PTNADATA, loop_hash, 0, &mut buf, fossilize::CRCCheck::WithoutCRC).is_ok() { return Ok((buf, 0.0)); } } @@ -528,7 +528,7 @@ impl AudioConvState { /* if we can't, return the blank file */ self.stream_state.needs_dump = true; - let buf = Box::new(include_bytes!("../blank.ptna").clone()); + let buf = Box::new(*include_bytes!("../blank.ptna")); /* calculate average expected length of this buffer */ let codec_data = self.codec_data.as_ref().unwrap(); diff --git a/media-converter/src/fossilize.rs b/media-converter/src/fossilize.rs index 4eafb423..d9f10421 100644 --- a/media-converter/src/fossilize.rs +++ b/media-converter/src/fossilize.rs @@ -234,7 +234,7 @@ impl StreamArchive { self.write_pos = self.file.seek(io::SeekFrom::Start(0))?; if self.file.metadata().unwrap().len() > 0 { - let mut magic_and_version = [0 as u8; MAGIC_LEN_BYTES]; + let mut magic_and_version = [0_u8; MAGIC_LEN_BYTES]; self.file.read_exact(&mut magic_and_version)?; let version = magic_and_version[15]; @@ -334,7 +334,7 @@ impl StreamArchive { let to_copy = std::cmp::min(entry.payload_info.full_size as usize - offset as usize, buf.len()); self.file.read_exact(&mut buf[0..to_copy]) - .map_err(|e| Error::IOError(e))?; + .map_err(Error::IOError)?; if entry.payload_info.crc != 0 { if let CRCCheck::WithCRC = crc_opt { diff --git a/media-converter/src/videoconv.rs b/media-converter/src/videoconv.rs index b00d6d6d..5d09bec7 100644 --- a/media-converter/src/videoconv.rs +++ b/media-converter/src/videoconv.rs @@ -431,12 +431,12 @@ impl ElementImpl for VideoConv { } struct StreamSerializer<'a> { - stream: &'a Vec, + stream: &'a [u128], cur_idx: usize, } impl<'a> StreamSerializer<'a> { - fn new(stream: &'a Vec) -> Self { + fn new(stream: &'a [u128]) -> Self { StreamSerializer { stream, cur_idx: 0, @@ -540,9 +540,6 @@ impl VideoConv { let mut query = gst::query::Duration::new(gst::Format::Bytes); if self.sinkpad.peer_query(&mut query) { - /* XXX: what? */ - let res = query.get_result(); - drop(res); state.upstream_duration = match query.get_result() { gst::format::GenericFormattedValue::Bytes(b) => *b, @@ -592,7 +589,7 @@ impl VideoConv { None => { return false; } }; - if let None = state.upstream_duration { + if state.upstream_duration.is_none() { self.query_upstream_duration(&mut state); }