Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an `Unserializable` type to utils #9379

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -196,7 +196,7 @@ impl<'a> CanvasPaintThread<'a> {
}
}
CanvasMsg::FromPaint(message) => {
match message {
match message.get() {
FromPaintMsg::SendNativeSurface(chan) => {
painter.send_native_surface(chan)
}
@@ -222,7 +222,7 @@ impl WebGLPaintThread {
}
}
CanvasMsg::FromPaint(message) => {
match message {
match message.get() {
FromPaintMsg::SendNativeSurface(chan) =>
painter.send_native_surface(chan),
}
@@ -35,11 +35,11 @@ use gfx_traits::color;
use ipc_channel::ipc::{IpcSender, IpcSharedMemory};
use layers::platform::surface::NativeSurface;
use offscreen_gl_context::GLContextAttributes;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::default::Default;
use std::fmt;
use std::str::FromStr;
use std::sync::mpsc::Sender;
use util::ipc::Unserializable;
use util::mem::HeapSizeOf;

#[derive(Clone, Deserialize, Serialize)]
@@ -53,7 +53,7 @@ pub enum CanvasMsg {
Canvas2d(Canvas2dMsg),
Common(CanvasCommonMsg),
FromLayout(FromLayoutMsg),
FromPaint(FromPaintMsg),
FromPaint(Unserializable<FromPaintMsg>),
WebGL(CanvasWebGLMsg),
}

@@ -73,18 +73,6 @@ pub enum FromPaintMsg {
SendNativeSurface(Sender<NativeSurface>),
}

impl Serialize for FromPaintMsg {
fn serialize<S>(&self, _: &mut S) -> Result<(), S::Error> where S: Serializer {
panic!("can't serialize a `FromPaintMsg`!")
}
}

impl Deserialize for FromPaintMsg {
fn deserialize<D>(_: &mut D) -> Result<FromPaintMsg, D::Error> where D: Deserializer {
panic!("can't deserialize a `FromPaintMsg`!")
}
}

#[derive(Clone, Deserialize, Serialize)]
pub enum Canvas2dMsg {
Arc(Point2D<f32>, f32, f32, f32, bool),

Some generated files are not rendered by default. Learn more.

@@ -8,9 +8,12 @@ use opts;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::fmt;
use std::intrinsics::type_name;
use std::io::{Error, ErrorKind};
use std::marker::Reflect;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::sync::Mutex;
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
use std::sync::mpsc::{self, Receiver, Sender};
@@ -177,3 +180,62 @@ pub fn optional_ipc_channel<T>() -> (OptionalIpcSender<T>, Receiver<T>)
}
}

pub struct Unserializable<T>(T);

impl<T> Unserializable<T> {
pub fn new(inner: T) -> Unserializable<T> {
Unserializable(inner)
}

pub fn get(self) -> T {
self.0
}
}

impl<T: fmt::Debug> fmt::Debug for Unserializable<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Unserializable({:?})", self.0)
}
}

impl<T: Clone> Clone for Unserializable<T> {
fn clone(&self) -> Self {
Unserializable(self.0.clone())
}
}

impl<T: Copy> Copy for Unserializable<T> {}

impl<T: PartialEq> PartialEq for Unserializable<T> {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}

impl<T: Eq> Eq for Unserializable<T> {}

impl<T> Serialize for Unserializable<T> {
fn serialize<S>(&self, _: &mut S) -> Result<(), S::Error> where S: Serializer {
panic!("Can't serialize a `Unserializable({})` struct", unsafe { type_name::<T>() });
}
}

impl<T> Deserialize for Unserializable<T> {
fn deserialize<D>(_: &mut D) -> Result<Unserializable<T>, D::Error> where D: Deserializer {
panic!("Can't deserialize a `Unserializable({})` struct", unsafe { type_name::<T>() });
}
}

impl<T> Deref for Unserializable<T> {
type Target = T;

fn deref(&self) -> &T {
&self.0
}
}

impl<T> DerefMut for Unserializable<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}
@@ -17,6 +17,11 @@ path = "../../../components/plugins"

[dependencies]
app_units = {version = "0.1", features = ["plugins"]}
libc = "0.2"
euclid = {version = "0.4", features = ["plugins"]}
libc = "0.2"
serde = "0.6"
serde_macros = "0.6"

[dependencies.ipc-channel]
git = "https://github.com/servo/ipc-channel"

@@ -0,0 +1,34 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use ipc_channel::ipc;
use std::sync::mpsc::channel;
use util::ipc::Unserializable;

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
enum Dummy {
Serializable(i32),
Unserializable(Unserializable<i32>),
}

#[test]
fn test_serializable() {
let (tx, rx) = ipc::channel().unwrap();
tx.send(Dummy::Serializable(4)).unwrap();
assert_eq!(rx.recv().unwrap(), Dummy::Serializable(4));
}

#[test]
fn test_unserializable_in_process() {
let (tx, rx) = channel();
tx.send(Dummy::Unserializable(Unserializable::new(4))).unwrap();
assert_eq!(rx.recv().unwrap(), Dummy::Unserializable(Unserializable::new(4)));
}

#[test]
#[should_panic]
fn test_unserializable_over_ipc() {
let (tx, _rx) = ipc::channel().unwrap();
tx.send(Dummy::Unserializable(Unserializable::new(4))).unwrap();
}
@@ -5,11 +5,15 @@
#![cfg_attr(test, feature(plugin, custom_derive, heap_api))]
#![cfg_attr(test, plugin(plugins))]
#![feature(alloc)]
#![feature(plugin)]
#![plugin(serde_macros)]

extern crate alloc;
extern crate app_units;
extern crate euclid;
extern crate ipc_channel;
extern crate libc;
extern crate serde;
extern crate util;

#[cfg(test)] mod cache;
@@ -19,3 +23,4 @@ extern crate util;
#[cfg(test)] mod mem;
#[cfg(test)] mod str;
#[cfg(test)] mod opts;
#[cfg(test)] mod ipc;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.