fix clippy warnings
[nihav.git] / nihav-codec-support / src / codecs / mod.rs
index 6589e477e4e3eaab4f57d9a0a9498c06a2ed39df..63810adca15bdcca5f70e871628ddcf5cebf0c84 100644 (file)
@@ -1,6 +1,6 @@
 //! Decoder support functions and definitions.
 use std::fmt;
-use std::ops::{Add, AddAssign, Sub, SubAssign};
+use std::ops::{Add, AddAssign, Sub, SubAssign, Neg};
 
 pub use nihav_core::frame::*;
 use std::mem;
@@ -11,25 +11,26 @@ use std::mem;
 ///
 /// # Examples
 ///
-/// ````norun
+/// ```ignore
 /// let mut frame = if is_intra_frame {
 ///         allocate_video_frame()
 ///     } else {
 ///         let ret = shuffler.clone_ref();
 ///         if ret.is_none() {
-///             return Err(DecodingError::MissingReference);
+///             return Err(DecoderError::MissingReference);
 ///         }
 ///         ret.unwrap()
 ///     };
 /// // output data into the frame
 /// shuffler.add_frame(frame.clone()); // tells frame manager to use the frame as the next reference
-/// ````
+/// ```
 #[allow(dead_code)]
-pub struct HAMShuffler {
-    lastframe: Option<NAVideoBufferRef<u8>>,
+#[derive(Default)]
+pub struct HAMShuffler<T: Copy> {
+    lastframe: Option<NAVideoBufferRef<T>>,
 }
 
-impl HAMShuffler {
+impl<T: Copy> HAMShuffler<T> {
     /// Constructs a new instance of frame manager.
     #[allow(dead_code)]
     pub fn new() -> Self { HAMShuffler { lastframe: None } }
@@ -38,12 +39,12 @@ impl HAMShuffler {
     pub fn clear(&mut self) { self.lastframe = None; }
     /// Sets a new frame reference.
     #[allow(dead_code)]
-    pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
+    pub fn add_frame(&mut self, buf: NAVideoBufferRef<T>) {
         self.lastframe = Some(buf);
     }
     /// Provides a copy of the reference frame if present or `None` if it is not.
     #[allow(dead_code)]
-    pub fn clone_ref(&mut self) -> Option<NAVideoBufferRef<u8>> {
+    pub fn clone_ref(&mut self) -> Option<NAVideoBufferRef<T>> {
         if let Some(ref mut frm) = self.lastframe {
             let newfrm = frm.copy_buffer();
             *frm = newfrm.clone().into_ref();
@@ -54,37 +55,31 @@ impl HAMShuffler {
     }
     /// Returns the original saved reference frame or `None` if it is not present.
     #[allow(dead_code)]
-    pub fn get_output_frame(&mut self) -> Option<NAVideoBufferRef<u8>> {
-        match self.lastframe {
-            Some(ref frm) => Some(frm.clone()),
-            None => None,
-        }
+    pub fn get_output_frame(&mut self) -> Option<NAVideoBufferRef<T>> {
+        self.lastframe.as_ref().cloned()
     }
 }
 
-impl Default for HAMShuffler {
-    fn default() -> Self { Self { lastframe: None } }
-}
-
 /// Frame manager for codecs with intra and inter frames.
 ///
 /// This frame manager simplifies frame management for the case when codec decodes new frame using previous frame as source of some data.
 ///
 /// # Examples
 ///
-/// ````norun
+/// ```ignore
 /// let mut frame = allocate_video_frame();
 /// if is_inter_frame {
 ///     let ret = shuffler.get_ref();
 ///     if ret.is_none() {
-///         return Err(DecodingError::MissingReference);
+///         return Err(DecoderError::MissingReference);
 ///     }
 ///     let ref_frame = ret.unwrap();
 ///     // keep decoding using data from ref_frame
 /// }
 /// shuffler.add_frame(frame.clone()); // tells frame manager to use the frame as the next reference
-/// ````
+/// ```
 #[allow(dead_code)]
+#[derive(Default)]
 pub struct IPShuffler {
     lastframe: Option<NAVideoBufferRef<u8>>,
 }
@@ -104,25 +99,17 @@ impl IPShuffler {
     /// Returns the original saved reference frame or `None` if it is not present.
     #[allow(dead_code)]
     pub fn get_ref(&mut self) -> Option<NAVideoBufferRef<u8>> {
-        if let Some(ref frm) = self.lastframe {
-            Some(frm.clone())
-        } else {
-            None
-        }
+        self.lastframe.as_ref().cloned()
     }
 }
 
-impl Default for IPShuffler {
-    fn default() -> Self { Self { lastframe: None } }
-}
-
 /// Frame manager for codecs with I-, P- and B-frames.
 ///
 /// This frame manager simplifies frame management for the case when codec uses I/P/B frame scheme.
 ///
 /// # Examples
 ///
-/// ````norun
+/// ```ignore
 /// let mut frame = allocate_video_frame();
 /// for mb in all_macroblocks {
 ///     // decode macroblock type
@@ -144,8 +131,9 @@ impl Default for IPShuffler {
 /// if is_intra_frame || is_p_frame {
 ///     shuffler.add_frame(frame.clone()); // tells frame manager to use the frame as the next reference
 /// }
-/// ````
+/// ```
 #[allow(dead_code)]
+#[derive(Default)]
 pub struct IPBShuffler {
     lastframe: Option<NAVideoBufferRef<u8>>,
     nextframe: Option<NAVideoBufferRef<u8>>,
@@ -167,45 +155,25 @@ impl IPBShuffler {
     /// Returns the previous reference frame or `None` if it is not present.
     #[allow(dead_code)]
     pub fn get_lastref(&mut self) -> Option<NAVideoBufferRef<u8>> {
-        if let Some(ref frm) = self.lastframe {
-            Some(frm.clone())
-        } else {
-            None
-        }
+        self.lastframe.as_ref().cloned()
     }
     /// Returns second last reference frame or `None` if it is not present.
     #[allow(dead_code)]
     pub fn get_nextref(&mut self) -> Option<NAVideoBufferRef<u8>> {
-        if let Some(ref frm) = self.nextframe {
-            Some(frm.clone())
-        } else {
-            None
-        }
+        self.nextframe.as_ref().cloned()
     }
     /// Returns the temporally following reference for B-frame or `None` if it is not present.
     #[allow(dead_code)]
     pub fn get_b_fwdref(&mut self) -> Option<NAVideoBufferRef<u8>> {
-        if let Some(ref frm) = self.nextframe {
-            Some(frm.clone())
-        } else {
-            None
-        }
+        self.nextframe.as_ref().cloned()
     }
     /// Returns the temporally preceeding reference for B-frame or `None` if it is not present.
     #[allow(dead_code)]
     pub fn get_b_bwdref(&mut self) -> Option<NAVideoBufferRef<u8>> {
-        if let Some(ref frm) = self.lastframe {
-            Some(frm.clone())
-        } else {
-            None
-        }
+        self.lastframe.as_ref().cloned()
     }
 }
 
-impl Default for IPBShuffler {
-    fn default() -> Self { Self { lastframe: None, nextframe: None } }
-}
-
 /// Motion vector data type.
 ///
 /// # Examples
@@ -228,6 +196,7 @@ pub struct MV {
 
 #[allow(clippy::many_single_char_names)]
 #[allow(clippy::collapsible_if)]
+#[allow(clippy::collapsible_else_if)]
 impl MV {
     /// Creates a new motion vector instance.
     pub fn new(x: i16, y: i16) -> Self { MV{ x, y } }
@@ -288,6 +257,13 @@ impl SubAssign for MV {
     fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; }
 }
 
+impl Neg for MV {
+    type Output = MV;
+    fn neg(self) -> Self::Output {
+        MV { x: -self.x, y: -self.y }
+    }
+}
+
 impl fmt::Display for MV {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         write!(f, "{},{}", self.x, self.y)