Commit d44c2937 authored by Sebastian Dröge's avatar Sebastian Dröge 🍵

Use TryFrom/TryInto traits where it makes sense instead of our previous ad-hoc solutions

parent fdb95f00
Pipeline #40096 passed with stages
in 20 minutes and 9 seconds
......@@ -17,6 +17,7 @@ use gst::prelude::*;
extern crate glib;
use std::convert::TryInto;
use std::env;
#[path = "../examples-common.rs"]
......@@ -60,7 +61,7 @@ fn example_main() {
//let pos = pipeline.query_position(gst::Format::Time).unwrap_or(-1);
//let dur = pipeline.query_duration(gst::Format::Time).unwrap_or(-1);
let pos = {
let pos: gst::ClockTime = {
// Create a new position query and send it to the pipeline.
// This will traverse all elements in the pipeline, until one feels
// capable of answering the query.
......@@ -71,10 +72,10 @@ fn example_main() {
None
}
}
.and_then(|pos| pos.try_into_time().ok())
.and_then(|pos| pos.try_into().ok())
.unwrap();
let dur = {
let dur: gst::ClockTime = {
// Create a new duration query and send it to the pipeline.
// This will traverse all elements in the pipeline, until one feels
// capable of answering the query.
......@@ -85,7 +86,7 @@ fn example_main() {
None
}
}
.and_then(|dur| dur.try_into_time().ok())
.and_then(|dur| dur.try_into().ok())
.unwrap();
println!("{} / {}", pos, dur);
......
......@@ -196,8 +196,10 @@ impl From<::VideoMultiviewFramePacking> for ::VideoMultiviewMode {
}
}
impl ::VideoMultiviewFramePacking {
pub fn try_from(
impl std::convert::TryFrom<::VideoMultiviewMode> for ::VideoMultiviewFramePacking {
type Error = ::VideoMultiviewMode;
fn try_from(
v: ::VideoMultiviewMode,
) -> Result<::VideoMultiviewFramePacking, ::VideoMultiviewMode> {
let v2 = from_glib(v.to_glib());
......
......@@ -16,6 +16,7 @@ use gst;
use gst::prelude::*;
use gst_video_sys;
use std::cmp;
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::mem;
use std::ptr;
......@@ -147,12 +148,16 @@ impl VideoTimeCode {
assert!(field_count <= 2);
self.0.field_count = field_count
}
}
impl TryFrom<VideoTimeCode> for ValidVideoTimeCode {
type Error = VideoTimeCode;
pub fn try_into(self) -> Result<ValidVideoTimeCode, VideoTimeCode> {
if self.is_valid() {
Ok(ValidVideoTimeCode(self.0))
fn try_from(v: VideoTimeCode) -> Result<ValidVideoTimeCode, VideoTimeCode> {
if v.is_valid() {
Ok(ValidVideoTimeCode(v.0))
} else {
Err(self)
Err(v)
}
}
}
......
This diff is collapsed.
......@@ -1277,6 +1277,7 @@ declare_concrete_query!(Other, T);
#[cfg(test)]
mod tests {
use super::*;
use std::convert::TryInto;
#[test]
fn test_writability() {
......@@ -1286,10 +1287,10 @@ mod tests {
match query.view_mut() {
QueryView::Position(ref mut p) => {
let pos = p.get_result();
assert_eq!(pos.try_into_time(), Ok(::CLOCK_TIME_NONE));
assert_eq!(pos.try_into(), Ok(::CLOCK_TIME_NONE));
p.set(3 * ::SECOND);
let pos = p.get_result();
assert_eq!(pos.try_into_time(), Ok(3 * ::SECOND));
assert_eq!(pos.try_into(), Ok(3 * ::SECOND));
}
_ => panic!("Wrong concrete Query in Query"),
}
......@@ -1299,7 +1300,7 @@ mod tests {
match query.view() {
QueryView::Position(ref p) => {
let pos = p.get_result();
assert_eq!(pos.try_into_time(), Ok(3 * ::SECOND));
assert_eq!(pos.try_into(), Ok(3 * ::SECOND));
unsafe {
assert!(!p.as_mut_ptr().is_null());
}
......@@ -1310,7 +1311,7 @@ mod tests {
let mut p = Query::new_position(::Format::Time);
let pos = p.get_result();
assert_eq!(pos.try_into_time(), Ok(::CLOCK_TIME_NONE));
assert_eq!(pos.try_into(), Ok(::CLOCK_TIME_NONE));
p.get_mut_structure().set("check_mut", &true);
......@@ -1349,7 +1350,7 @@ mod tests {
match query.view() {
QueryView::Duration(ref d) => {
let duration = d.get_result();
assert_eq!(duration.try_into_time(), Ok(2 * ::SECOND));
assert_eq!(duration.try_into(), Ok(2 * ::SECOND));
}
_ => (),
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment