2019-07-26 21:54:25 +00:00
|
|
|
// run-pass
|
|
|
|
|
2018-09-17 09:18:35 +00:00
|
|
|
#![allow(unused_imports)]
|
|
|
|
#![allow(unused_must_use)]
|
2015-03-22 20:13:15 +00:00
|
|
|
// pretty-expanded FIXME #23616
|
2014-10-02 19:52:06 +00:00
|
|
|
|
2020-07-04 15:20:24 +00:00
|
|
|
use std::fmt;
|
2022-05-05 16:34:13 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
trait Encoder {
|
|
|
|
type Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Encodable<S: Encoder> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), S::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: Encoder> Encodable<S> for i32 {
|
|
|
|
fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct JsonEncoder<'a>(PhantomData<&'a mut ()>);
|
|
|
|
|
|
|
|
impl Encoder for JsonEncoder<'_> {
|
|
|
|
type Error = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_json<T: for<'r> Encodable<JsonEncoder<'r>>>(
|
|
|
|
object: &T,
|
|
|
|
) -> Result<String, ()> {
|
|
|
|
let s = String::new();
|
|
|
|
{
|
|
|
|
let mut encoder = JsonEncoder(PhantomData);
|
|
|
|
object.encode(&mut encoder)?;
|
|
|
|
}
|
|
|
|
Ok(s)
|
|
|
|
}
|
2014-10-02 19:52:06 +00:00
|
|
|
|
2022-05-05 16:34:13 +00:00
|
|
|
struct Foo<T: for<'a> Encodable<JsonEncoder<'a>>> {
|
2014-10-02 19:52:06 +00:00
|
|
|
v: T,
|
|
|
|
}
|
|
|
|
|
2022-05-05 16:34:13 +00:00
|
|
|
impl<T: for<'a> Encodable<JsonEncoder<'a>>> Drop for Foo<T> {
|
2014-10-02 19:52:06 +00:00
|
|
|
fn drop(&mut self) {
|
2022-05-05 16:34:13 +00:00
|
|
|
encode_json(&self.v);
|
2014-10-02 19:52:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2015-01-25 21:05:03 +00:00
|
|
|
let _ = Foo { v: 10 };
|
2014-10-02 19:52:06 +00:00
|
|
|
}
|