From 58ecd0008feb98b35e0d015ec6293c1809bec3cf Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Sat, 8 Feb 2025 12:32:30 +0900 Subject: [PATCH] make use of GATs to make decoder trait implementation simpler. --- nokhwa-core/src/decoder.rs | 41 ++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/nokhwa-core/src/decoder.rs b/nokhwa-core/src/decoder.rs index b751c6c..0dd2606 100644 --- a/nokhwa-core/src/decoder.rs +++ b/nokhwa-core/src/decoder.rs @@ -1,18 +1,14 @@ use crate::{error::NokhwaError, frame_buffer::FrameBuffer, frame_format::FrameFormat}; use image::{ImageBuffer, Pixel}; -use std::{ - ops::{ControlFlow, Deref}, -}; +use std::ops::{ControlFlow, Deref}; /// Trait to define a struct that can decode a [`FrameBuffer`] -pub trait Decoder { +pub trait Decoder { /// Formats that the decoder can decode. const ALLOWED_FORMATS: &'static [FrameFormat]; - /// Output pixel type (e.g. [`Rgb`](image::Rgb)) - type OutputPixels: Pixel; /// Container type for the decoder. Will be used for ImageBuffer - type PixelContainer: Deref::OutputPixels as Pixel>::Subpixel]>; + type PixelContainer: Deref; fn check_format(buffer: &FrameBuffer) -> ControlFlow { if !Self::ALLOWED_FORMATS.contains(&buffer.source_frame_format()) { @@ -26,7 +22,7 @@ pub trait Decoder { fn decode( &mut self, buffer: &FrameBuffer, - ) -> Result, NokhwaError>; + ) -> Result, NokhwaError>; /// Decode to user-provided Buffer /// @@ -34,7 +30,7 @@ pub trait Decoder { fn decode_buffer( &mut self, buffer: &FrameBuffer, - output: &mut [<::OutputPixels as Pixel>::Subpixel], + output: &mut [OutputPixel::Subpixel], ) -> Result<(), NokhwaError>; /// Decoder Predicted Size @@ -46,8 +42,8 @@ pub trait Decoder { Some( res.x() as usize * res.y() as usize - * size_of::<<::OutputPixels as Pixel>::Subpixel>() - * <::OutputPixels as Pixel>::CHANNEL_COUNT as usize, + * size_of::() + * OutputPixel::CHANNEL_COUNT as usize, ) } } @@ -55,47 +51,48 @@ pub trait Decoder { /// Decoder that can be used statically (struct contains no state) /// /// This is useful for times that a simple function is all that is required. -pub trait StaticDecoder: Decoder { +pub trait StaticDecoder: Decoder { fn decode_static( buffer: &FrameBuffer, - ) -> Result, NokhwaError>; + ) -> Result, NokhwaError>; fn decode_static_to_buffer( buffer: &FrameBuffer, - output: &mut [<::OutputPixels as Pixel>::Subpixel], + output: &mut [OutputPixel::Subpixel], ) -> Result<(), NokhwaError>; } #[cfg(feature = "async")] #[cfg_attr(feature = "async", async_trait::async_trait)] -pub trait AsyncDecoder: Decoder { +pub trait AsyncDecoder: Decoder { /// Asynchronous decoder async fn decode_async( &mut self, buffer: &FrameBuffer, - ) -> Result, NokhwaError>; + ) -> Result, NokhwaError>; /// Asynchronous decoder to user buffer. async fn decode_buffer( &mut self, buffer: &FrameBuffer, - output: &mut [<::OutputPixels as Pixel>::Subpixel], + output: &mut [OutputPixel::Subpixel], ) -> Result<(), NokhwaError>; } #[cfg(feature = "async")] #[cfg_attr(feature = "async", async_trait::async_trait)] -pub trait AsyncStaticDecoder: Decoder { +pub trait AsyncStaticDecoder: + Decoder + AsyncDecoder +{ /// Asynchronous decoder async fn decode_static_async( buffer: &FrameBuffer, - ) -> Result, NokhwaError>; + ) -> Result, NokhwaError>; /// Asynchronous decoder to user buffer. - async fn decode_static_buffer( - &mut self, + async fn decode_static_buffer_async( buffer: &FrameBuffer, - output: &mut [<::OutputPixels as Pixel>::Subpixel], + output: &mut [OutputPixel::Subpixel], ) -> Result<(), NokhwaError>; }