DRY nb_streams

This commit is contained in:
Kornel Lesiński 2020-06-26 22:00:41 +01:00 committed by Zhiming Wang
parent 53570d13d7
commit aa8df7d0d2
No known key found for this signature in database
GPG Key ID: 5B58F95EC95965D8

View File

@ -1,3 +1,4 @@
use std::fmt;
use std::mem; use std::mem;
use std::ptr; use std::ptr;
use std::rc::Rc; use std::rc::Rc;
@ -36,12 +37,17 @@ impl Context {
} }
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<Stream<'b>> pub fn stream<'a, 'b>(&'a self, index: usize) -> Option<Stream<'b>>
where where
'a: 'b, 'a: 'b,
{ {
unsafe { unsafe {
if index >= (*self.as_ptr()).nb_streams as usize { if index >= self.nb_streams() as usize {
None None
} else { } else {
Some(Stream::wrap(self, index)) Some(Stream::wrap(self, index))
@ -54,7 +60,7 @@ impl Context {
'a: 'b, 'a: 'b,
{ {
unsafe { unsafe {
if index >= (*self.as_ptr()).nb_streams as usize { if index >= self.nb_streams() as usize {
None None
} else { } else {
Some(StreamMut::wrap(self, index)) Some(StreamMut::wrap(self, index))
@ -78,6 +84,7 @@ impl Context {
unsafe { (*self.as_ptr()).duration } unsafe { (*self.as_ptr()).duration }
} }
#[inline]
pub fn nb_chapters(&self) -> u32 { pub fn nb_chapters(&self) -> u32 {
unsafe { (*self.as_ptr()).nb_chapters } unsafe { (*self.as_ptr()).nb_chapters }
} }
@ -87,7 +94,7 @@ impl Context {
'a: 'b, 'a: 'b,
{ {
unsafe { unsafe {
if index >= (*self.as_ptr()).nb_chapters as usize { if index >= self.nb_chapters() as usize {
None None
} else { } else {
Some(Chapter::wrap(self, index)) Some(Chapter::wrap(self, index))
@ -100,7 +107,7 @@ impl Context {
'a: 'b, 'a: 'b,
{ {
unsafe { unsafe {
if index >= (*self.as_ptr()).nb_chapters as usize { if index >= self.nb_chapters() as usize {
None None
} else { } else {
Some(ChapterMut::wrap(self, index)) Some(ChapterMut::wrap(self, index))
@ -222,7 +229,7 @@ impl<'a> Iterator for StreamIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe { unsafe {
if self.current >= (*self.context.as_ptr()).nb_streams { if self.current >= self.context.nb_streams() {
return None; return None;
} }
@ -233,8 +240,7 @@ impl<'a> Iterator for StreamIter<'a> {
} }
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
unsafe { let length = self.context.nb_streams() as usize;
let length = (*self.context.as_ptr()).nb_streams as usize;
( (
length - self.current as usize, length - self.current as usize,
@ -242,7 +248,6 @@ impl<'a> Iterator for StreamIter<'a> {
) )
} }
} }
}
impl<'a> ExactSizeIterator for StreamIter<'a> {} impl<'a> ExactSizeIterator for StreamIter<'a> {}
@ -264,13 +269,12 @@ impl<'a> Iterator for StreamIterMut<'a> {
type Item = StreamMut<'a>; type Item = StreamMut<'a>;
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe { if self.current >= self.context.nb_streams() {
if self.current >= (*self.context.as_ptr()).nb_streams {
return None; return None;
} }
self.current += 1; self.current += 1;
unsafe {
Some(StreamMut::wrap( Some(StreamMut::wrap(
mem::transmute_copy(&self.context), mem::transmute_copy(&self.context),
(self.current - 1) as usize, (self.current - 1) as usize,
@ -279,8 +283,7 @@ impl<'a> Iterator for StreamIterMut<'a> {
} }
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
unsafe { let length = self.context.nb_streams() as usize;
let length = (*self.context.as_ptr()).nb_streams as usize;
( (
length - self.current as usize, length - self.current as usize,
@ -288,7 +291,6 @@ impl<'a> Iterator for StreamIterMut<'a> {
) )
} }
} }
}
impl<'a> ExactSizeIterator for StreamIterMut<'a> {} impl<'a> ExactSizeIterator for StreamIterMut<'a> {}