Use SendCell from the newly created crate

This commit is contained in:
Sebastian Dröge 2017-08-04 22:57:12 +03:00
parent 43a014b6fa
commit be6f03a1af
3 changed files with 11 additions and 100 deletions

7
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -14,6 +14,9 @@ use gtk::ApplicationExt as GtkApplicationExt;
use std::env;
extern crate send_cell;
use send_cell::SendCell;
fn create_ui(app: &gtk::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<T> {
data: T,
thread_id: thread::ThreadId,
}
impl<T> SendCell<T> {
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<T, Self> {
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<T> {
Ref { data: self.get() }
}
pub fn try_borrow(&self) -> Option<Ref<T>> {
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<T> From<T> for SendCell<T> {
fn from(t: T) -> SendCell<T> {
SendCell::new(t)
}
}
impl<T: Default> Default for SendCell<T> {
fn default() -> SendCell<T> {
SendCell::new(T::default())
}
}
impl<T: PartialEq> PartialEq<SendCell<T>> for SendCell<T> {
fn eq(&self, other: &Self) -> bool {
self.data.eq(&other.data)
}
}
impl<T: Eq> Eq for SendCell<T> {}
impl<T: PartialOrd> PartialOrd<SendCell<T>> for SendCell<T> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.data.partial_cmp(&other.data)
}
}
impl<T: Ord> Ord for SendCell<T> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.data.cmp(&other.data)
}
}
unsafe impl<T> Send for SendCell<T> {}