Add Success/Error variants of #[must_use] enums

And implement basically the Try trait for them. This will be replaced by
the Try trait once it is stable.

Fixes https://github.com/sdroege/gstreamer-rs/issues/44
This commit is contained in:
Sebastian Dröge 2017-11-05 19:58:44 +02:00
parent e8638c1630
commit 95204c2294
5 changed files with 230 additions and 23 deletions

View file

@ -73,7 +73,7 @@ fn main_loop() -> Result<(), utils::ExampleError> {
}
}
appsrc.end_of_stream();
let _ = appsrc.end_of_stream();
});
utils::set_state(&pipeline, gst::State::Playing)?;

208
gstreamer/src/enums.rs Normal file
View file

@ -0,0 +1,208 @@
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use StateChangeReturn;
use FlowReturn;
use PadLinkReturn;
use ClockReturn;
impl StateChangeReturn {
pub fn into_result(self) -> Result<StateChangeSuccess, StateChangeError> {
match self {
StateChangeReturn::Success => Ok(StateChangeSuccess::Success),
StateChangeReturn::Async => Ok(StateChangeSuccess::Async),
StateChangeReturn::NoPreroll => Ok(StateChangeSuccess::NoPreroll),
StateChangeReturn::Failure => Err(StateChangeError),
_ => Err(StateChangeError),
}
}
pub fn from_error(_: StateChangeError) -> Self {
StateChangeReturn::Failure
}
pub fn from_ok(v: StateChangeSuccess) -> Self {
match v {
StateChangeSuccess::Success => StateChangeReturn::Success,
StateChangeSuccess::Async => StateChangeReturn::Async,
StateChangeSuccess::NoPreroll => StateChangeReturn::NoPreroll,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum StateChangeSuccess {
Success,
Async,
NoPreroll,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[must_use]
pub struct StateChangeError;
impl FlowReturn {
pub fn into_result(self) -> Result<FlowSuccess, FlowError> {
match self {
FlowReturn::CustomSuccess2 => Ok(FlowSuccess::CustomSuccess2),
FlowReturn::CustomSuccess1 => Ok(FlowSuccess::CustomSuccess1),
FlowReturn::CustomSuccess => Ok(FlowSuccess::CustomSuccess),
FlowReturn::Ok => Ok(FlowSuccess::Ok),
FlowReturn::NotLinked => Err(FlowError::NotLinked),
FlowReturn::Flushing => Err(FlowError::Flushing),
FlowReturn::Eos => Err(FlowError::Eos),
FlowReturn::NotNegotiated => Err(FlowError::NotNegotiated),
FlowReturn::Error => Err(FlowError::Error),
FlowReturn::NotSupported => Err(FlowError::NotSupported),
FlowReturn::CustomError => Err(FlowError::CustomError),
FlowReturn::CustomError1 => Err(FlowError::CustomError1),
FlowReturn::CustomError2 => Err(FlowError::CustomError2),
_ => Err(FlowError::Error),
}
}
pub fn from_error(v: FlowError) -> Self {
match v {
FlowError::NotLinked => FlowReturn::NotLinked,
FlowError::Flushing => FlowReturn::Flushing,
FlowError::Eos => FlowReturn::Eos,
FlowError::NotNegotiated => FlowReturn::NotNegotiated,
FlowError::Error => FlowReturn::Error,
FlowError::NotSupported => FlowReturn::NotSupported,
FlowError::CustomError => FlowReturn::CustomError,
FlowError::CustomError1 => FlowReturn::CustomError1,
FlowError::CustomError2 => FlowReturn::CustomError2,
}
}
pub fn from_ok(v: FlowSuccess) -> Self {
match v {
FlowSuccess::CustomSuccess2 => FlowReturn::CustomSuccess2,
FlowSuccess::CustomSuccess1 => FlowReturn::CustomSuccess1,
FlowSuccess::CustomSuccess => FlowReturn::CustomSuccess,
FlowSuccess::Ok => FlowReturn::Ok,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum FlowSuccess {
CustomSuccess2,
CustomSuccess1,
CustomSuccess,
Ok,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[must_use]
pub enum FlowError {
NotLinked,
Flushing,
Eos,
NotNegotiated,
Error,
NotSupported,
CustomError,
CustomError1,
CustomError2,
}
impl PadLinkReturn {
pub fn into_result(self) -> Result<PadLinkSuccess, PadLinkError> {
match self {
PadLinkReturn::Ok => Ok(PadLinkSuccess),
PadLinkReturn::WrongHierarchy => Err(PadLinkError::WrongHierarchy),
PadLinkReturn::WasLinked => Err(PadLinkError::WasLinked),
PadLinkReturn::WrongDirection => Err(PadLinkError::WrongDirection),
PadLinkReturn::Noformat => Err(PadLinkError::Noformat),
PadLinkReturn::Nosched => Err(PadLinkError::Nosched),
PadLinkReturn::Refused => Err(PadLinkError::Refused),
_ => Err(PadLinkError::Refused),
}
}
pub fn from_error(v: PadLinkError) -> Self {
match v {
PadLinkError::WrongHierarchy => PadLinkReturn::WrongHierarchy,
PadLinkError::WasLinked => PadLinkReturn::WasLinked,
PadLinkError::WrongDirection => PadLinkReturn::WrongDirection,
PadLinkError::Noformat => PadLinkReturn::Noformat,
PadLinkError::Nosched => PadLinkReturn::Nosched,
PadLinkError::Refused => PadLinkReturn::Refused,
}
}
pub fn from_ok(_: PadLinkSuccess) -> Self {
PadLinkReturn::Ok
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct PadLinkSuccess;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[must_use]
pub enum PadLinkError {
WrongHierarchy,
WasLinked,
WrongDirection,
Noformat,
Nosched,
Refused,
}
impl ClockReturn {
pub fn into_result(self) -> Result<ClockSuccess, ClockError> {
match self {
ClockReturn::Ok => Ok(ClockSuccess::Ok),
ClockReturn::Done => Ok(ClockSuccess::Done),
ClockReturn::Early => Err(ClockError::Early),
ClockReturn::Unscheduled => Err(ClockError::Unscheduled),
ClockReturn::Busy => Err(ClockError::Busy),
ClockReturn::Badtime => Err(ClockError::Badtime),
ClockReturn::Error => Err(ClockError::Error),
ClockReturn::Unsupported => Err(ClockError::Unsupported),
_ => Err(ClockError::Error),
}
}
pub fn from_error(v: ClockError) -> Self {
match v {
ClockError::Early => ClockReturn::Early,
ClockError::Unscheduled => ClockReturn::Unscheduled,
ClockError::Busy => ClockReturn::Busy,
ClockError::Badtime => ClockReturn::Badtime,
ClockError::Error => ClockReturn::Error,
ClockError::Unsupported => ClockReturn::Unsupported,
}
}
pub fn from_ok(v: ClockSuccess) -> Self {
match v {
ClockSuccess::Ok => ClockReturn::Ok,
ClockSuccess::Done => ClockReturn::Done,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum ClockSuccess {
Ok,
Done,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[must_use]
pub enum ClockError {
Early,
Unscheduled,
Busy,
Badtime,
Error,
Unsupported,
}

View file

@ -101,6 +101,7 @@ mod tag_setter;
mod iterator;
mod device_provider;
mod parse_context;
mod enums;
pub use object::GstObjectExtManual;
pub use element::{ElementExtManual, ElementMessageType, NotifyWatchId};
pub use element::{ELEMENT_METADATA_AUTHOR, ELEMENT_METADATA_DESCRIPTION, ELEMENT_METADATA_DOC_URI,
@ -115,6 +116,7 @@ pub use device_provider::DeviceProviderExtManual;
pub use parse_context::ParseContext;
#[cfg(feature = "futures")]
pub use bus::BusStream;
pub use enums::{StateChangeSuccess, StateChangeError};
mod value;
pub use value::*;

View file

@ -92,7 +92,10 @@ mod tutorial5 {
let pipeline = playbin.clone();
play_button.connect_clicked(move |_| {
let pipeline = &pipeline;
pipeline.set_state(gst::State::Playing);
pipeline
.set_state(gst::State::Playing)
.into_result()
.unwrap();
});
let pause_button = gtk::Button::new_from_icon_name(
@ -102,7 +105,10 @@ mod tutorial5 {
let pipeline = playbin.clone();
pause_button.connect_clicked(move |_| {
let pipeline = &pipeline;
pipeline.set_state(gst::State::Paused);
pipeline
.set_state(gst::State::Paused)
.into_result()
.unwrap();
});
let stop_button = gtk::Button::new_from_icon_name(
@ -112,7 +118,7 @@ mod tutorial5 {
let pipeline = playbin.clone();
stop_button.connect_clicked(move |_| {
let pipeline = &pipeline;
pipeline.set_state(gst::State::Ready);
pipeline.set_state(gst::State::Ready).into_result().unwrap();
});
let slider = gtk::Scale::new_with_range(
@ -313,7 +319,10 @@ mod tutorial5 {
.unwrap();
create_ui(&playbin);
playbin.set_state(gst::State::Playing);
playbin
.set_state(gst::State::Playing)
.into_result()
.unwrap();
let bus = playbin.get_bus().unwrap();
let pipeline = playbin.clone();
@ -324,7 +333,7 @@ mod tutorial5 {
// We just set the pipeline to READY (which stops playback).
gst::MessageView::Eos(..) => {
println!("End-Of-Stream reached.");
pipeline.set_state(gst::State::Ready);
pipeline.set_state(gst::State::Ready).into_result().unwrap();
}
// This is called when an error message is posted on the bus
@ -346,7 +355,7 @@ mod tutorial5 {
});
gtk::main();
playbin.set_state(gst::State::Null);
playbin.set_state(gst::State::Null).into_result().unwrap();
}
}

View file

@ -55,7 +55,7 @@ fn main() {
tee_audio_pad.get_name()
);
let queue_audio_pad = audio_queue.get_static_pad("sink").unwrap();
tee_audio_pad.link(&queue_audio_pad);
tee_audio_pad.link(&queue_audio_pad).into_result().unwrap();
let tee_video_pad = tee.get_request_pad("src_%u").unwrap();
println!(
@ -63,14 +63,9 @@ fn main() {
tee_video_pad.get_name()
);
let queue_video_pad = video_queue.get_static_pad("sink").unwrap();
tee_video_pad.link(&queue_video_pad);
tee_video_pad.link(&queue_video_pad).into_result().unwrap();
let ret = pipeline.set_state(gst::State::Playing);
assert_ne!(
ret,
gst::StateChangeReturn::Failure,
"Unable to set the pipeline to the Playing state."
);
pipeline.set_state(gst::State::Playing).into_result().expect("Unable to set the pipeline to the Playing state.");
let bus = pipeline.get_bus().unwrap();
while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) {
use gst::MessageView;
@ -89,12 +84,5 @@ fn main() {
}
}
pipeline.set_state(gst::State::Null);
let ret = pipeline.set_state(gst::State::Null);
assert_ne!(
ret,
gst::StateChangeReturn::Failure,
"Unable to set the pipeline to the Null state."
);
pipeline.set_state(gst::State::Null).into_result().expect("Unable to set the pipeline to the Null state.");
}