2019-07-26 21:54:25 +00:00
|
|
|
// run-pass
|
|
|
|
|
2018-09-17 09:18:35 +00:00
|
|
|
#![allow(unused_must_use)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_imports)]
|
2015-04-21 02:01:20 +00: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 08:30:51 +00:00
|
|
|
|
2020-07-04 15:20:24 +00:00
|
|
|
extern crate rustc_macros;
|
2020-06-02 19:46:42 +00:00
|
|
|
extern crate rustc_serialize;
|
2014-04-03 04:31:00 +00:00
|
|
|
|
2014-12-12 18:59:41 +00:00
|
|
|
use std::fmt;
|
2020-07-04 15:20:24 +00:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io::Cursor;
|
2014-07-29 23:31:39 +00:00
|
|
|
use std::slice;
|
|
|
|
|
2020-07-04 15:20:24 +00:00
|
|
|
use rustc_macros::Encodable;
|
2019-08-10 15:38:27 +00:00
|
|
|
use rustc_serialize::json;
|
|
|
|
use rustc_serialize::opaque;
|
2020-07-04 15:20:24 +00:00
|
|
|
use rustc_serialize::{Encodable, Encoder};
|
2014-04-03 04:31:00 +00:00
|
|
|
|
2020-07-04 15:20:24 +00:00
|
|
|
#[derive(Encodable)]
|
2014-04-03 04:31:00 +00:00
|
|
|
struct Foo {
|
|
|
|
baz: bool,
|
|
|
|
}
|
|
|
|
|
2020-07-04 15:20:24 +00:00
|
|
|
#[derive(Encodable)]
|
2014-04-03 04:31:00 +00:00
|
|
|
struct Bar {
|
2015-03-26 00:06:52 +00:00
|
|
|
froboz: usize,
|
2014-04-03 04:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum WireProtocol {
|
|
|
|
JSON,
|
2016-09-16 14:25:54 +00:00
|
|
|
Opaque,
|
2014-04-03 04:31:00 +00:00
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2020-07-04 15:20:24 +00:00
|
|
|
fn encode_json<T: for<'a> Encodable<json::Encoder<'a>>>(val: &T, wr: &mut Cursor<Vec<u8>>) {
|
2014-12-12 18:59:41 +00:00
|
|
|
write!(wr, "{}", json::as_json(val));
|
2014-04-03 04:31:00 +00:00
|
|
|
}
|
2020-07-04 15:20:24 +00:00
|
|
|
fn encode_opaque<T: Encodable<opaque::Encoder>>(val: &T, wr: Vec<u8>) {
|
2016-09-01 13:55:33 +00:00
|
|
|
let mut encoder = opaque::Encoder::new(wr);
|
2014-04-03 04:31:00 +00:00
|
|
|
val.encode(&mut encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2020-07-04 15:20:24 +00:00
|
|
|
let target = Foo { baz: false };
|
2014-11-06 08:05:53 +00:00
|
|
|
let proto = WireProtocol::JSON;
|
2014-04-03 04:31:00 +00:00
|
|
|
match proto {
|
2018-06-04 20:14:02 +00:00
|
|
|
WireProtocol::JSON => encode_json(&target, &mut Cursor::new(Vec::new())),
|
2020-07-04 15:20:24 +00:00
|
|
|
WireProtocol::Opaque => encode_opaque(&target, Vec::new()),
|
2014-04-03 04:31:00 +00:00
|
|
|
}
|
|
|
|
}
|