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::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<Stream<'b>>
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<<Self as Iterator>::Item> {
unsafe {
if self.current >= (*self.context.as_ptr()).nb_streams {
if self.current >= self.context.nb_streams() {
return None;
}
@ -233,15 +240,13 @@ impl<'a> Iterator for StreamIter<'a> {
}
fn size_hint(&self) -> (usize, Option<usize>) {
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),
)
}
}
}
impl<'a> ExactSizeIterator for StreamIter<'a> {}
@ -264,13 +269,12 @@ impl<'a> Iterator for StreamIterMut<'a> {
type Item = StreamMut<'a>;
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
if self.current >= (*self.context.as_ptr()).nb_streams {
if self.current >= self.context.nb_streams() {
return None;
}
self.current += 1;
unsafe {
Some(StreamMut::wrap(
mem::transmute_copy(&self.context),
(self.current - 1) as usize,
@ -279,15 +283,13 @@ impl<'a> Iterator for StreamIterMut<'a> {
}
fn size_hint(&self) -> (usize, Option<usize>) {
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),
)
}
}
}
impl<'a> ExactSizeIterator for StreamIterMut<'a> {}