Created
December 10, 2014 19:16
-
-
Save emk/526cb63c1f82634a85c0 to your computer and use it in GitHub Desktop.
Having trouble with Rust encoder lifetime & trait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Try this on http://play.rust-lang.org/ | |
extern crate libc; | |
extern crate serialize; | |
use std::ptr::null_mut; | |
use libc::c_void; | |
// This needs to be mutable in practice. | |
pub struct Context; | |
impl Context { | |
// The | |
pub unsafe fn push<'a, T, E>(&'a mut self, object: &T) | |
where E : ::serialize::Encoder<MyError>, | |
T : ::serialize::Encodable<E, MyError> | |
{ | |
// Encoder is a local class defined below. | |
let encoder = Encoder::new(self); | |
object.encode(&mut encoder).unwrap(); | |
} | |
} | |
fn main() { | |
let mut ctx = Context; | |
ctx.push(&1i); | |
ctx.push(&"foo"); | |
} | |
// ----- Support types ----- | |
#[deriving(Show)] | |
pub struct MyError; | |
// Encodes data into `ctx`. | |
pub struct Encoder<'a> { | |
ctx: &'a mut Context | |
} | |
impl<'a> Encoder<'a> { | |
/// Create a new encoder which pushes values to the stack of a `Context`. | |
/// Unsafe because all low-level operations on `Context` are unsafe. | |
pub unsafe fn new(ctx: &mut Context) -> Encoder { | |
Encoder{ctx: ctx} | |
} | |
} | |
// What you need know here: Encoder<'a> implements | |
// ::serialize::Encoder<MyError>. | |
type EncodeResult = Result<(), MyError>; | |
impl<'a> ::serialize::Encoder<MyError> for Encoder<'a> { | |
fn emit_nil(&mut self) -> EncodeResult { | |
panic!("Unimplemented") | |
} | |
fn emit_uint(&mut self, v: uint) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_u64(&mut self, v: u64) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_u32(&mut self, v: u32) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_u16(&mut self, v: u16) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_u8(&mut self, v: u8) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_int(&mut self, v: int) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_i64(&mut self, v: i64) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_i32(&mut self, v: i32) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_i16(&mut self, v: i16) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_i8(&mut self, v: i8) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_bool(&mut self, v: bool) -> EncodeResult { | |
panic!("Unimplemented") | |
} | |
fn emit_f64(&mut self, v: f64) -> EncodeResult { | |
panic!("Unimplemented") | |
} | |
fn emit_f32(&mut self, v: f32) -> EncodeResult { self.emit_f64(v as f64) } | |
fn emit_char(&mut self, v: char) -> EncodeResult { | |
panic!("Unimplemented") | |
} | |
fn emit_str(&mut self, v: &str) -> EncodeResult { | |
panic!("Unimplemented") | |
} | |
fn emit_enum(&mut self, _name: &str, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_enum_variant(&mut self, v_name: &str, v_id: uint, | |
len: uint, f: |&mut Encoder<'a>| -> EncodeResult) -> | |
EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_enum_variant_arg(&mut self, a_idx: uint, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> | |
EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_enum_struct_variant(&mut self, v_name: &str, v_id: uint, len: uint, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> | |
EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_enum_struct_variant_field(&mut self, f_name: &str, f_idx: uint, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> | |
EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_struct(&mut self, name: &str, len: uint, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_struct_field(&mut self, f_name: &str, f_idx: uint, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_tuple(&mut self, len: uint, f: |&mut Encoder<'a>| -> EncodeResult) -> | |
EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_tuple_arg(&mut self, idx: uint, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_tuple_struct(&mut self, name: &str, len: uint, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_tuple_struct_arg(&mut self, f_idx: uint, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> | |
EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_option(&mut self, f: |&mut Encoder<'a>| -> EncodeResult) -> | |
EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_option_none(&mut self) -> EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_option_some(&mut self, f: |&mut Encoder<'a>| -> EncodeResult) -> | |
EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_seq(&mut self, len: uint, f: |this: &mut Encoder<'a>| -> | |
EncodeResult) -> EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_seq_elt(&mut self, idx: uint, | |
f: |this: &mut Encoder<'a>| -> EncodeResult) -> EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_map(&mut self, len: uint, f: |&mut Encoder<'a>| -> EncodeResult) -> | |
EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_map_elt_key(&mut self, idx: uint, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
#[allow(unused_variables)] | |
fn emit_map_elt_val(&mut self, idx: uint, | |
f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult | |
{ | |
panic!("Unimplemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment