Make use of downcast_ref() to prevent some clones

This commit is contained in:
Sebastian Dröge 2018-07-12 08:31:58 +03:00
parent b3089ca047
commit 6e33313b78
5 changed files with 37 additions and 41 deletions

View file

@ -340,9 +340,8 @@ impl Demuxer {
) -> bool { ) -> bool {
let element = parent let element = parent
.as_ref() .as_ref()
.cloned()
.unwrap() .unwrap()
.downcast::<Element>() .downcast_ref::<Element>()
.unwrap(); .unwrap();
let demuxer = element.get_impl().downcast_ref::<Demuxer>().unwrap(); let demuxer = element.get_impl().downcast_ref::<Demuxer>().unwrap();
@ -352,7 +351,7 @@ impl Demuxer {
.peer_query_duration::<gst::format::Bytes>() .peer_query_duration::<gst::format::Bytes>()
.and_then(|v| v.0); .and_then(|v| v.0);
if !demuxer.start(&element, upstream_size, mode == gst::PadMode::Pull) { if !demuxer.start(element, upstream_size, mode == gst::PadMode::Pull) {
return false; return false;
} }
@ -367,7 +366,7 @@ impl Demuxer {
let _ = demuxer.sinkpad.stop_task(); let _ = demuxer.sinkpad.stop_task();
} }
demuxer.stop(&element) demuxer.stop(element)
} }
} }
@ -378,23 +377,22 @@ impl Demuxer {
) -> gst::FlowReturn { ) -> gst::FlowReturn {
let element = parent let element = parent
.as_ref() .as_ref()
.cloned()
.unwrap() .unwrap()
.downcast::<Element>() .downcast_ref::<Element>()
.unwrap(); .unwrap();
let demuxer = element.get_impl().downcast_ref::<Demuxer>().unwrap(); let demuxer = element.get_impl().downcast_ref::<Demuxer>().unwrap();
let mut res = { let mut res = {
let demuxer_impl = &mut demuxer.imp.lock().unwrap(); let demuxer_impl = &mut demuxer.imp.lock().unwrap();
gst_trace!(demuxer.cat, obj: &element, "Handling buffer {:?}", buffer); gst_trace!(demuxer.cat, obj: element, "Handling buffer {:?}", buffer);
match demuxer_impl.handle_buffer(&element, Some(buffer)) { match demuxer_impl.handle_buffer(element, Some(buffer)) {
Ok(res) => res, Ok(res) => res,
Err(flow_error) => { Err(flow_error) => {
gst_error!( gst_error!(
demuxer.cat, demuxer.cat,
obj: &element, obj: element,
"Failed handling buffer: {:?}", "Failed handling buffer: {:?}",
flow_error flow_error
); );
@ -411,33 +409,33 @@ impl Demuxer {
// Loop until AllEos, NeedMoreData or error when pushing downstream // Loop until AllEos, NeedMoreData or error when pushing downstream
loop { loop {
gst_trace!(demuxer.cat, obj: &element, "Handled {:?}", res); gst_trace!(demuxer.cat, obj: element, "Handled {:?}", res);
match res { match res {
HandleBufferResult::NeedMoreData => { HandleBufferResult::NeedMoreData => {
return gst::FlowReturn::Ok; return gst::FlowReturn::Ok;
} }
HandleBufferResult::StreamAdded(stream) => { HandleBufferResult::StreamAdded(stream) => {
demuxer.add_stream(&element, stream.index, stream.caps, &stream.stream_id); demuxer.add_stream(element, stream.index, stream.caps, &stream.stream_id);
} }
HandleBufferResult::HaveAllStreams => { HandleBufferResult::HaveAllStreams => {
demuxer.added_all_streams(&element); demuxer.added_all_streams(element);
} }
HandleBufferResult::StreamChanged(stream) => { HandleBufferResult::StreamChanged(stream) => {
demuxer.stream_format_changed(&element, stream.index, stream.caps); demuxer.stream_format_changed(element, stream.index, stream.caps);
} }
HandleBufferResult::StreamsChanged(streams) => for stream in streams { HandleBufferResult::StreamsChanged(streams) => for stream in streams {
demuxer.stream_format_changed(&element, stream.index, stream.caps); demuxer.stream_format_changed(element, stream.index, stream.caps);
}, },
HandleBufferResult::BufferForStream(index, buffer) => { HandleBufferResult::BufferForStream(index, buffer) => {
let flow_ret = demuxer.stream_push_buffer(&element, index, buffer); let flow_ret = demuxer.stream_push_buffer(element, index, buffer);
if flow_ret != gst::FlowReturn::Ok { if flow_ret != gst::FlowReturn::Ok {
return flow_ret; return flow_ret;
} }
} }
HandleBufferResult::Eos(index) => { HandleBufferResult::Eos(index) => {
demuxer.stream_eos(&element, index); demuxer.stream_eos(element, index);
return gst::FlowReturn::Eos; return gst::FlowReturn::Eos;
} }
HandleBufferResult::Again => { HandleBufferResult::Again => {
@ -445,16 +443,16 @@ impl Demuxer {
} }
}; };
gst_trace!(demuxer.cat, obj: &element, "Calling again"); gst_trace!(demuxer.cat, obj: element, "Calling again");
res = { res = {
let demuxer_impl = &mut demuxer.imp.lock().unwrap(); let demuxer_impl = &mut demuxer.imp.lock().unwrap();
match demuxer_impl.handle_buffer(&element, None) { match demuxer_impl.handle_buffer(element, None) {
Ok(res) => res, Ok(res) => res,
Err(flow_error) => { Err(flow_error) => {
gst_error!( gst_error!(
demuxer.cat, demuxer.cat,
obj: &element, obj: element,
"Failed calling again: {:?}", "Failed calling again: {:?}",
flow_error flow_error
); );
@ -476,9 +474,8 @@ impl Demuxer {
let element = parent let element = parent
.as_ref() .as_ref()
.cloned()
.unwrap() .unwrap()
.downcast::<Element>() .downcast_ref::<Element>()
.unwrap(); .unwrap();
let demuxer = element.get_impl().downcast_ref::<Demuxer>().unwrap(); let demuxer = element.get_impl().downcast_ref::<Demuxer>().unwrap();
@ -486,13 +483,13 @@ impl Demuxer {
EventView::Eos(..) => { EventView::Eos(..) => {
let demuxer_impl = &mut demuxer.imp.lock().unwrap(); let demuxer_impl = &mut demuxer.imp.lock().unwrap();
gst_debug!(demuxer.cat, obj: &element, "End of stream"); gst_debug!(demuxer.cat, obj: element, "End of stream");
match demuxer_impl.end_of_stream(&element) { match demuxer_impl.end_of_stream(element) {
Ok(_) => (), Ok(_) => (),
Err(ref msg) => { Err(ref msg) => {
gst_error!( gst_error!(
demuxer.cat, demuxer.cat,
obj: &element, obj: element,
"Failed end of stream: {:?}", "Failed end of stream: {:?}",
msg msg
); );
@ -511,9 +508,8 @@ impl Demuxer {
let element = parent let element = parent
.as_ref() .as_ref()
.cloned()
.unwrap() .unwrap()
.downcast::<Element>() .downcast_ref::<Element>()
.unwrap(); .unwrap();
let demuxer = element.get_impl().downcast_ref::<Demuxer>().unwrap(); let demuxer = element.get_impl().downcast_ref::<Demuxer>().unwrap();
@ -526,7 +522,7 @@ impl Demuxer {
let position = demuxer_impl.get_position(&element); let position = demuxer_impl.get_position(&element);
gst_trace!( gst_trace!(
demuxer.cat, demuxer.cat,
obj: &element, obj: element,
"Returning position {:?}", "Returning position {:?}",
position position
); );
@ -550,7 +546,7 @@ impl Demuxer {
let duration = demuxer_impl.get_duration(&element); let duration = demuxer_impl.get_duration(&element);
gst_trace!( gst_trace!(
demuxer.cat, demuxer.cat,
obj: &element, obj: element,
"Returning duration {:?}", "Returning duration {:?}",
duration duration
); );

View file

@ -1194,7 +1194,7 @@ impl ToggleRecord {
impl ObjectImpl<Element> for ToggleRecord { impl ObjectImpl<Element> for ToggleRecord {
fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) { fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) {
let prop = &PROPERTIES[id as usize]; let prop = &PROPERTIES[id as usize];
let element = obj.clone().downcast::<Element>().unwrap(); let element = obj.downcast_ref::<Element>().unwrap();
match *prop { match *prop {
Property::Boolean("record", ..) => { Property::Boolean("record", ..) => {
@ -1202,7 +1202,7 @@ impl ObjectImpl<Element> for ToggleRecord {
let record = value.get().unwrap(); let record = value.get().unwrap();
gst_debug!( gst_debug!(
self.cat, self.cat,
obj: &element, obj: element,
"Setting record from {:?} to {:?}", "Setting record from {:?} to {:?}",
settings.record, settings.record,
record record

View file

@ -206,7 +206,7 @@ impl ObjectImpl<BaseTransform> for Rgb2Gray {
// at any time from any thread. // at any time from any thread.
fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) { fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) {
let prop = &PROPERTIES[id as usize]; let prop = &PROPERTIES[id as usize];
let element = obj.clone().downcast::<BaseTransform>().unwrap(); let element = obj.downcast_ref::<BaseTransform>().unwrap();
match *prop { match *prop {
Property::Boolean("invert", ..) => { Property::Boolean("invert", ..) => {
@ -214,7 +214,7 @@ impl ObjectImpl<BaseTransform> for Rgb2Gray {
let invert = value.get().unwrap(); let invert = value.get().unwrap();
gst_info!( gst_info!(
self.cat, self.cat,
obj: &element, obj: element,
"Changing invert from {} to {}", "Changing invert from {} to {}",
settings.invert, settings.invert,
invert invert
@ -226,7 +226,7 @@ impl ObjectImpl<BaseTransform> for Rgb2Gray {
let shift = value.get().unwrap(); let shift = value.get().unwrap();
gst_info!( gst_info!(
self.cat, self.cat,
obj: &element, obj: element,
"Changing shift from {} to {}", "Changing shift from {} to {}",
settings.shift, settings.shift,
shift shift

View file

@ -251,7 +251,7 @@ impl ObjectImpl<BaseSrc> for SineSrc {
// at any time from any thread. // at any time from any thread.
fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) { fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) {
let prop = &PROPERTIES[id as usize]; let prop = &PROPERTIES[id as usize];
let element = obj.clone().downcast::<BaseSrc>().unwrap(); let element = obj.downcast_ref::<BaseSrc>().unwrap();
match *prop { match *prop {
Property::UInt("samples-per-buffer", ..) => { Property::UInt("samples-per-buffer", ..) => {
@ -259,7 +259,7 @@ impl ObjectImpl<BaseSrc> for SineSrc {
let samples_per_buffer = value.get().unwrap(); let samples_per_buffer = value.get().unwrap();
gst_info!( gst_info!(
self.cat, self.cat,
obj: &element, obj: element,
"Changing samples-per-buffer from {} to {}", "Changing samples-per-buffer from {} to {}",
settings.samples_per_buffer, settings.samples_per_buffer,
samples_per_buffer samples_per_buffer
@ -268,14 +268,14 @@ impl ObjectImpl<BaseSrc> for SineSrc {
drop(settings); drop(settings);
let _ = let _ =
element.post_message(&gst::Message::new_latency().src(Some(&element)).build()); element.post_message(&gst::Message::new_latency().src(Some(element)).build());
} }
Property::UInt("freq", ..) => { Property::UInt("freq", ..) => {
let mut settings = self.settings.lock().unwrap(); let mut settings = self.settings.lock().unwrap();
let freq = value.get().unwrap(); let freq = value.get().unwrap();
gst_info!( gst_info!(
self.cat, self.cat,
obj: &element, obj: element,
"Changing freq from {} to {}", "Changing freq from {} to {}",
settings.freq, settings.freq,
freq freq
@ -287,7 +287,7 @@ impl ObjectImpl<BaseSrc> for SineSrc {
let volume = value.get().unwrap(); let volume = value.get().unwrap();
gst_info!( gst_info!(
self.cat, self.cat,
obj: &element, obj: element,
"Changing volume from {} to {}", "Changing volume from {} to {}",
settings.volume, settings.volume,
volume volume
@ -299,7 +299,7 @@ impl ObjectImpl<BaseSrc> for SineSrc {
let mute = value.get().unwrap(); let mute = value.get().unwrap();
gst_info!( gst_info!(
self.cat, self.cat,
obj: &element, obj: element,
"Changing mute from {} to {}", "Changing mute from {} to {}",
settings.mute, settings.mute,
mute mute
@ -311,7 +311,7 @@ impl ObjectImpl<BaseSrc> for SineSrc {
let is_live = value.get().unwrap(); let is_live = value.get().unwrap();
gst_info!( gst_info!(
self.cat, self.cat,
obj: &element, obj: element,
"Changing is-live from {} to {}", "Changing is-live from {} to {}",
settings.is_live, settings.is_live,
is_live is_live

View file

@ -78,7 +78,7 @@ where
f: F, f: F,
) -> R { ) -> R {
// FIXME: Does this work for element subclasses? // FIXME: Does this work for element subclasses?
let element = parent.as_ref().cloned().unwrap().downcast::<T>().unwrap(); let element = parent.as_ref().unwrap().downcast_ref::<T>().unwrap();
let imp = element.get_impl(); let imp = element.get_impl();
let imp = Any::downcast_ref::<Box<ElementImpl<T> + 'static>>(imp).unwrap(); let imp = Any::downcast_ref::<Box<ElementImpl<T> + 'static>>(imp).unwrap();
let imp = imp.downcast_ref::<S>().unwrap(); let imp = imp.downcast_ref::<S>().unwrap();