2018-09-17 11:18:35 +02:00
|
|
|
#![allow(unused_must_use)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_imports)]
|
2015-03-22 13:13:15 -07:00
|
|
|
|
2015-04-20 19:01:20 -07:00
|
|
|
#![feature(rustc_private)]
|
Fix orphan checking (cc #19470). (This is not a complete fix of #19470 because of the backwards compatibility feature gate.)
This is a [breaking-change]. The new rules require that, for an impl of a trait defined
in some other crate, two conditions must hold:
1. Some type must be local.
2. Every type parameter must appear "under" some local type.
Here are some examples that are legal:
```rust
struct MyStruct<T> { ... }
// Here `T` appears "under' `MyStruct`.
impl<T> Clone for MyStruct<T> { }
// Here `T` appears "under' `MyStruct` as well. Note that it also appears
// elsewhere.
impl<T> Iterator<T> for MyStruct<T> { }
```
Here is an illegal example:
```rust
// Here `U` does not appear "under" `MyStruct` or any other local type.
// We call `U` "uncovered".
impl<T,U> Iterator<U> for MyStruct<T> { }
```
There are a couple of ways to rewrite this last example so that it is
legal:
1. In some cases, the uncovered type parameter (here, `U`) should be converted
into an associated type. This is however a non-local change that requires access
to the original trait. Also, associated types are not fully baked.
2. Add `U` as a type parameter of `MyStruct`:
```rust
struct MyStruct<T,U> { ... }
impl<T,U> Iterator<U> for MyStruct<T,U> { }
```
3. Create a newtype wrapper for `U`
```rust
impl<T,U> Iterator<Wrapper<U>> for MyStruct<T,U> { }
```
Because associated types are not fully baked, which in the case of the
`Hash` trait makes adhering to this rule impossible, you can
temporarily disable this rule in your crate by using
`#![feature(old_orphan_check)]`. Note that the `old_orphan_check`
feature will be removed before 1.0 is released.
2014-12-26 03:30:51 -05:00
|
|
|
|
2014-04-08 14:31:25 -07:00
|
|
|
extern crate serialize;
|
2018-09-17 11:48:20 +02:00
|
|
|
use serialize as rustc_serialize;
|
2014-04-02 23:31:00 -05:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
use std::io::Cursor;
|
|
|
|
use std::io::prelude::*;
|
2014-12-12 10:59:41 -08:00
|
|
|
use std::fmt;
|
2014-07-29 16:31:39 -07:00
|
|
|
use std::slice;
|
|
|
|
|
2014-04-08 14:31:25 -07:00
|
|
|
use serialize::{Encodable, Encoder};
|
|
|
|
use serialize::json;
|
2016-09-01 16:55:33 +03:00
|
|
|
use serialize::opaque;
|
2014-04-02 23:31:00 -05:00
|
|
|
|
2018-09-17 11:48:20 +02:00
|
|
|
#[derive(RustcEncodable)]
|
2014-04-02 23:31:00 -05:00
|
|
|
struct Foo {
|
|
|
|
baz: bool,
|
|
|
|
}
|
|
|
|
|
2018-09-17 11:48:20 +02:00
|
|
|
#[derive(RustcEncodable)]
|
2014-04-02 23:31:00 -05:00
|
|
|
struct Bar {
|
2015-03-25 17:06:52 -07:00
|
|
|
froboz: usize,
|
2014-04-02 23:31:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
enum WireProtocol {
|
|
|
|
JSON,
|
2016-09-16 17:25:54 +03:00
|
|
|
Opaque,
|
2014-04-02 23:31:00 -05:00
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
fn encode_json<T: Encodable>(val: &T, wr: &mut Cursor<Vec<u8>>) {
|
2014-12-12 10:59:41 -08:00
|
|
|
write!(wr, "{}", json::as_json(val));
|
2014-04-02 23:31:00 -05:00
|
|
|
}
|
2018-06-04 22:14:02 +02:00
|
|
|
fn encode_opaque<T: Encodable>(val: &T, wr: Vec<u8>) {
|
2016-09-01 16:55:33 +03:00
|
|
|
let mut encoder = opaque::Encoder::new(wr);
|
2014-04-02 23:31:00 -05:00
|
|
|
val.encode(&mut encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let target = Foo{baz: false,};
|
2014-11-06 00:05:53 -08:00
|
|
|
let proto = WireProtocol::JSON;
|
2014-04-02 23:31:00 -05:00
|
|
|
match proto {
|
2018-06-04 22:14:02 +02:00
|
|
|
WireProtocol::JSON => encode_json(&target, &mut Cursor::new(Vec::new())),
|
|
|
|
WireProtocol::Opaque => encode_opaque(&target, Vec::new())
|
2014-04-02 23:31:00 -05:00
|
|
|
}
|
|
|
|
}
|