From be6f03a1affe9d03249ec35c9d8f5259849feb60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 4 Aug 2017 22:57:12 +0300 Subject: [PATCH] Use SendCell from the newly created crate --- Cargo.lock | 7 +++ examples/Cargo.toml | 2 +- examples/src/bin/gtksink.rs | 102 ++---------------------------------- 3 files changed, 11 insertions(+), 100 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed8da9195..31a75faa7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,6 +105,7 @@ dependencies = [ "gstreamer 0.1.0", "gstreamer-app 0.1.0", "gtk 0.1.3 (git+https://github.com/gtk-rs/gtk)", + "send-cell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -482,6 +483,11 @@ name = "scoped-tls" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "send-cell" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "slab" version = "0.3.0" @@ -577,6 +583,7 @@ dependencies = [ "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" "checksum rustdoc-stripper 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ab70973988ecb752bc19e28e47bb3913bd98a8db8abe2dbd0f862067b176cf95" "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d" +"checksum send-cell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a994a88c42f2b24f2426714696cffc90fe5dbc79b5043fa3676cd50cf3614df" "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23" "checksum tokio-core 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e85d419699ec4b71bfe35bbc25bb8771e52eff0471a7f75c853ad06e200b4f86" "checksum tokio-io 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c2c3ce9739f7387a0fa65b5421e81feae92e04d603f008898f4257790ce8c2db" diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 3cc195e9b..eecea99e9 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -11,4 +11,4 @@ gtk = { version = "0.1.3", git = "https://github.com/gtk-rs/gtk", features = ["v gio = { version = "0.1.3", git = "https://github.com/gtk-rs/gio" } futures = "0.1" tokio-core = "0.1" - +send-cell = "0.1" diff --git a/examples/src/bin/gtksink.rs b/examples/src/bin/gtksink.rs index f756109b6..38b0ec7d7 100644 --- a/examples/src/bin/gtksink.rs +++ b/examples/src/bin/gtksink.rs @@ -14,6 +14,9 @@ use gtk::ApplicationExt as GtkApplicationExt; use std::env; +extern crate send_cell; +use send_cell::SendCell; + fn create_ui(app: >k::Application) { let pipeline = Pipeline::new(None); let src = ElementFactory::make("videotestsrc", None).unwrap(); @@ -123,102 +126,3 @@ fn main() { app.run(&args_ref); } -// Workaround for GTK objects not implementing Send (nor Sync) -// but us having to use them in a closure that requires Send -use std::thread; -use std::cmp; -use std::ops; - -#[derive(Clone, Debug)] -pub struct SendCell { - data: T, - thread_id: thread::ThreadId, -} - -impl SendCell { - pub fn new(data: T) -> Self { - SendCell { - data: data, - thread_id: thread::current().id(), - } - } - - pub fn into_inner(self) -> T { - assert_eq!(thread::current().id(), self.thread_id); - self.data - } - - pub fn try_into_inner(self) -> Result { - if thread::current().id() == self.thread_id { - Ok(self.data) - } else { - Err(self) - } - } - - pub fn get(&self) -> &T { - assert_eq!(thread::current().id(), self.thread_id); - &self.data - } - - pub fn try_get(&self) -> Option<&T> { - if thread::current().id() == self.thread_id { - Some(&self.data) - } else { - None - } - } - - pub fn borrow(&self) -> Ref { - Ref { data: self.get() } - } - - pub fn try_borrow(&self) -> Option> { - self.try_get().map(|data| Ref { data: data }) - } -} - -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] -pub struct Ref<'a, T: 'a> { - data: &'a T, -} - -impl<'a, T: 'a> ops::Deref for Ref<'a, T> { - type Target = T; - - fn deref(&self) -> &T { - self.data - } -} - -impl From for SendCell { - fn from(t: T) -> SendCell { - SendCell::new(t) - } -} - -impl Default for SendCell { - fn default() -> SendCell { - SendCell::new(T::default()) - } -} - -impl PartialEq> for SendCell { - fn eq(&self, other: &Self) -> bool { - self.data.eq(&other.data) - } -} -impl Eq for SendCell {} - -impl PartialOrd> for SendCell { - fn partial_cmp(&self, other: &Self) -> Option { - self.data.partial_cmp(&other.data) - } -} -impl Ord for SendCell { - fn cmp(&self, other: &Self) -> cmp::Ordering { - self.data.cmp(&other.data) - } -} - -unsafe impl Send for SendCell {}