From aa8df7d0d24ee12ea280dd7c85de90d3b228c681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Fri, 26 Jun 2020 22:00:41 +0100 Subject: [PATCH] DRY nb_streams --- src/format/context/common.rs | 52 +++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/format/context/common.rs b/src/format/context/common.rs index e441681..448462a 100644 --- a/src/format/context/common.rs +++ b/src/format/context/common.rs @@ -1,3 +1,4 @@ +use std::fmt; use std::mem; use std::ptr; use std::rc::Rc; @@ -36,12 +37,17 @@ impl Context { } impl Context { + #[inline] + fn nb_streams(&self) -> u32 { + unsafe { (*self.as_ptr()).nb_streams } + } + pub fn stream<'a, 'b>(&'a self, index: usize) -> Option> where 'a: 'b, { unsafe { - if index >= (*self.as_ptr()).nb_streams as usize { + if index >= self.nb_streams() as usize { None } else { Some(Stream::wrap(self, index)) @@ -54,7 +60,7 @@ impl Context { 'a: 'b, { unsafe { - if index >= (*self.as_ptr()).nb_streams as usize { + if index >= self.nb_streams() as usize { None } else { Some(StreamMut::wrap(self, index)) @@ -78,6 +84,7 @@ impl Context { unsafe { (*self.as_ptr()).duration } } + #[inline] pub fn nb_chapters(&self) -> u32 { unsafe { (*self.as_ptr()).nb_chapters } } @@ -87,7 +94,7 @@ impl Context { 'a: 'b, { unsafe { - if index >= (*self.as_ptr()).nb_chapters as usize { + if index >= self.nb_chapters() as usize { None } else { Some(Chapter::wrap(self, index)) @@ -100,7 +107,7 @@ impl Context { 'a: 'b, { unsafe { - if index >= (*self.as_ptr()).nb_chapters as usize { + if index >= self.nb_chapters() as usize { None } else { Some(ChapterMut::wrap(self, index)) @@ -222,7 +229,7 @@ impl<'a> Iterator for StreamIter<'a> { fn next(&mut self) -> Option<::Item> { unsafe { - if self.current >= (*self.context.as_ptr()).nb_streams { + if self.current >= self.context.nb_streams() { return None; } @@ -233,14 +240,12 @@ impl<'a> Iterator for StreamIter<'a> { } fn size_hint(&self) -> (usize, Option) { - unsafe { - let length = (*self.context.as_ptr()).nb_streams as usize; + let length = self.context.nb_streams() as usize; - ( - length - self.current as usize, - Some(length - self.current as usize), - ) - } + ( + length - self.current as usize, + Some(length - self.current as usize), + ) } } @@ -264,13 +269,12 @@ impl<'a> Iterator for StreamIterMut<'a> { type Item = StreamMut<'a>; fn next(&mut self) -> Option<::Item> { + if self.current >= self.context.nb_streams() { + return None; + } + self.current += 1; + unsafe { - if self.current >= (*self.context.as_ptr()).nb_streams { - return None; - } - - self.current += 1; - Some(StreamMut::wrap( mem::transmute_copy(&self.context), (self.current - 1) as usize, @@ -279,14 +283,12 @@ impl<'a> Iterator for StreamIterMut<'a> { } fn size_hint(&self) -> (usize, Option) { - unsafe { - let length = (*self.context.as_ptr()).nb_streams as usize; + let length = self.context.nb_streams() as usize; - ( - length - self.current as usize, - Some(length - self.current as usize), - ) - } + ( + length - self.current as usize, + Some(length - self.current as usize), + ) } }