From 8ed092c0c3f5d0c6eebb206f0e6ef4f343047dff Mon Sep 17 00:00:00 2001 From: meh Date: Sat, 29 Aug 2015 16:15:57 +0200 Subject: [PATCH] codec: use Option<_> in the getter API --- examples/codec-info.rs | 79 ++++++++++++++++++++++++++++++++++++------ src/codec/audio.rs | 72 ++++++++++++++++++++++---------------- src/codec/codec.rs | 17 +++++---- src/codec/video.rs | 48 ++++++++++++++----------- 4 files changed, 147 insertions(+), 69 deletions(-) diff --git a/examples/codec-info.rs b/examples/codec-info.rs index 68a63ef..eaaa813 100644 --- a/examples/codec-info.rs +++ b/examples/codec-info.rs @@ -17,16 +17,47 @@ fn main() { if let Some(profiles) = codec.profiles() { println!("\t profiles: {:?}", profiles.collect::>()); } + else { + println!("\t profiles: none"); + } if let Ok(video) = codec.video() { - println!("\t rates: {:?}", video.rates().collect::>()); - println!("\t formats: {:?}", video.formats().collect::>()); + if let Some(rates) = video.rates() { + println!("\t rates: {:?}", rates.collect::>()); + } + else { + println!("\t rates: any"); + } + + if let Some(formats) = video.formats() { + println!("\t formats: {:?}", formats.collect::>()); + } + else { + println!("\t formats: any"); + } } if let Ok(audio) = codec.audio() { - println!("\t rates: {:?}", audio.rates().collect::>()); - println!("\t formats: {:?}", audio.formats().collect::>()); - println!("\t channel_layouts: {:?}", audio.channel_layouts().collect::>()); + if let Some(rates) = audio.rates() { + println!("\t rates: {:?}", rates.collect::>()); + } + else { + println!("\t rates: any"); + } + + if let Some(formats) = audio.formats() { + println!("\t formats: {:?}", formats.collect::>()); + } + else { + println!("\t formats: any"); + } + + if let Some(layouts) = audio.channel_layouts() { + println!("\t channel_layouts: {:?}", layouts.collect::>()); + } + else { + println!("\t channel_layouts: any"); + } } println!("\t max_lowres: {:?}", codec.max_lowres()); @@ -46,14 +77,42 @@ fn main() { } if let Ok(video) = codec.video() { - println!("\t rates: {:?}", video.rates().collect::>()); - println!("\t formats: {:?}", video.formats().collect::>()); + if let Some(rates) = video.rates() { + println!("\t rates: {:?}", rates.collect::>()); + } + else { + println!("\t rates: any"); + } + + if let Some(formats) = video.formats() { + println!("\t formats: {:?}", formats.collect::>()); + } + else { + println!("\t formats: any"); + } } if let Ok(audio) = codec.audio() { - println!("\t rates: {:?}", audio.rates().collect::>()); - println!("\t formats: {:?}", audio.formats().collect::>()); - println!("\t channel_layouts: {:?}", audio.channel_layouts().collect::>()); + if let Some(rates) = audio.rates() { + println!("\t rates: {:?}", rates.collect::>()); + } + else { + println!("\t rates: any"); + } + + if let Some(formats) = audio.formats() { + println!("\t formats: {:?}", formats.collect::>()); + } + else { + println!("\t formats: any"); + } + + if let Some(layouts) = audio.channel_layouts() { + println!("\t channel_layouts: {:?}", layouts.collect::>()); + } + else { + println!("\t channel_layouts: any"); + } } println!("\t max_lowres: {:?}", codec.max_lowres()); diff --git a/src/codec/audio.rs b/src/codec/audio.rs index a3e4901..5c15c95 100644 --- a/src/codec/audio.rs +++ b/src/codec/audio.rs @@ -18,21 +18,36 @@ impl<'a> Audio<'a> { } impl<'a> Audio<'a> { - pub fn rates(&self) -> RateIter { + pub fn rates(&self) -> Option { unsafe { - RateIter::new((*self.codec.as_ptr()).supported_samplerates) + if (*self.as_ptr()).supported_samplerates.is_null() { + None + } + else { + Some(RateIter::new((*self.codec.as_ptr()).supported_samplerates)) + } } } - pub fn formats(&self) -> FormatIter { + pub fn formats(&self) -> Option { unsafe { - FormatIter::new((*self.codec.as_ptr()).sample_fmts) + if (*self.codec.as_ptr()).sample_fmts.is_null() { + None + } + else { + Some(FormatIter::new((*self.codec.as_ptr()).sample_fmts)) + } } } - pub fn channel_layouts(&self) -> ChannelLayoutIter { + pub fn channel_layouts(&self) -> Option { unsafe { - ChannelLayoutIter::new((*self.codec.as_ptr()).channel_layouts) + if (*self.codec.as_ptr()).channel_layouts.is_null() { + None + } + else { + Some(ChannelLayoutIter::new((*self.codec.as_ptr()).channel_layouts)) + } } } } @@ -62,15 +77,14 @@ impl<'a> Iterator for RateIter<'a> { fn next(&mut self) -> Option<::Item> { unsafe { - if !self.ptr.is_null() && (*self.ptr) != 0 { - let rate = *self.ptr; - self.ptr = self.ptr.offset(1); + if *self.ptr == 0 { + return None; + } - Some(rate) - } - else { - None - } + let rate = *self.ptr; + self.ptr = self.ptr.offset(1); + + Some(rate) } } } @@ -92,15 +106,14 @@ impl<'a> Iterator for FormatIter<'a> { fn next(&mut self) -> Option<::Item> { unsafe { - if !self.ptr.is_null() && (*self.ptr) != AVSampleFormat::AV_SAMPLE_FMT_NONE { - let format = (*self.ptr).into(); - self.ptr = self.ptr.offset(1); + if *self.ptr == AVSampleFormat::AV_SAMPLE_FMT_NONE { + return None; + } - Some(format) - } - else { - None - } + let format = (*self.ptr).into(); + self.ptr = self.ptr.offset(1); + + Some(format) } } } @@ -122,15 +135,14 @@ impl<'a> Iterator for ChannelLayoutIter<'a> { fn next(&mut self) -> Option<::Item> { unsafe { - if !self.ptr.is_null() && (*self.ptr) != 0 { - let chl = ChannelLayout::from_bits_truncate(*self.ptr); - self.ptr = self.ptr.offset(1); + if *self.ptr == 0 { + return None; + } - Some(chl) - } - else { - None - } + let layout = ChannelLayout::from_bits_truncate(*self.ptr); + self.ptr = self.ptr.offset(1); + + Some(layout) } } } diff --git a/src/codec/codec.rs b/src/codec/codec.rs index 2e28f1a..3fa2248 100644 --- a/src/codec/codec.rs +++ b/src/codec/codec.rs @@ -105,7 +105,7 @@ impl<'a> Codec<'a> { pub fn profiles(&self) -> Option { unsafe { if (*self.as_ptr()).profiles.is_null() { - return None; + None } else { Some(ProfileIter::new(self.id(), (*self.as_ptr()).profiles)) @@ -132,15 +132,14 @@ impl<'a> Iterator for ProfileIter<'a> { fn next(&mut self) -> Option<::Item> { unsafe { - if (*self.ptr).profile != FF_PROFILE_UNKNOWN && !(*self.ptr).name.is_null() { - let profile = Profile::from((self.id, (*self.ptr).profile)); - self.ptr = self.ptr.offset(1); + if (*self.ptr).profile == FF_PROFILE_UNKNOWN { + return None; + } - Some(profile) - } - else { - None - } + let profile = Profile::from((self.id, (*self.ptr).profile)); + self.ptr = self.ptr.offset(1); + + Some(profile) } } } diff --git a/src/codec/video.rs b/src/codec/video.rs index 6e484d4..ae05add 100644 --- a/src/codec/video.rs +++ b/src/codec/video.rs @@ -18,15 +18,25 @@ impl<'a> Video<'a> { } impl<'a> Video<'a> { - pub fn rates(&self) -> RateIter { + pub fn rates(&self) -> Option { unsafe { - RateIter::new((*self.codec.as_ptr()).supported_framerates) + if (*self.codec.as_ptr()).supported_framerates.is_null() { + None + } + else { + Some(RateIter::new((*self.codec.as_ptr()).supported_framerates)) + } } } - pub fn formats(&self) -> FormatIter { + pub fn formats(&self) -> Option { unsafe { - FormatIter::new((*self.codec.as_ptr()).pix_fmts) + if (*self.codec.as_ptr()).pix_fmts.is_null() { + None + } + else { + Some(FormatIter::new((*self.codec.as_ptr()).pix_fmts)) + } } } } @@ -56,15 +66,14 @@ impl<'a> Iterator for RateIter<'a> { fn next(&mut self) -> Option<::Item> { unsafe { - if !self.ptr.is_null() && (*self.ptr) != (AVRational { num: 0, den: 0 }) { - let rate = (*self.ptr).into(); - self.ptr = self.ptr.offset(1); + if (*self.ptr).num == 0 && (*self.ptr).den == 0 { + return None; + } - Some(rate) - } - else { - None - } + let rate = (*self.ptr).into(); + self.ptr = self.ptr.offset(1); + + Some(rate) } } } @@ -86,15 +95,14 @@ impl<'a> Iterator for FormatIter<'a> { fn next(&mut self) -> Option<::Item> { unsafe { - if !self.ptr.is_null() && (*self.ptr) != AVPixelFormat::AV_PIX_FMT_NONE { - let format = (*self.ptr).into(); - self.ptr = self.ptr.offset(1); + if *self.ptr == AVPixelFormat::AV_PIX_FMT_NONE { + return None; + } - Some(format) - } - else { - None - } + let format = (*self.ptr).into(); + self.ptr = self.ptr.offset(1); + + Some(format) } } }