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 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

ipc: Add `Unserializable` structure.

This allows unifying the way we prevent the serialisation of things that
can't be sent over IPC for their in-process nature.

This is a proposed solution to things like:
#9373

Where something like the following can be used:

```rust
    Exit(Unserializable<Sender<()>>),
```
  • Loading branch information
emilio committed Jan 19, 2016
commit d15097846b96f591d8e56e19d1747aa69647d0d4
@@ -8,9 +8,11 @@ use opts;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::fmt;
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 +179,54 @@ 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> Serialize for Unserializable<T> {
fn serialize<S>(&self, _: &mut S) -> Result<(), S::Error> where S: Serializer {
panic!("Can't serialize a `Unserializable` struct");
}
}

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");
}
}

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
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.