Trait rustc_serialize::Decodable [] [src]

pub trait Decodable: Sized {
    fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error>;
}

Trait for deserializing a type.

This can be implemented for custom data types to allow them to be decoded with Decoder implementations. Most of Rust's built-in or standard data types (like i32 and Vec<T>) have Decodable implementations provided by this module.

Note that, in general, you should let the compiler implement this for you by using the derive(RustcDecodable) attribute.

Examples

extern crate rustc_serialize;

#[derive(RustcDecodable)]
struct Point {
    x: i32,
    y: i32,
}

This generates code equivalent to:

extern crate rustc_serialize;
use rustc_serialize::Decodable;
use rustc_serialize::Decoder;

struct Point {
    x: i32,
    y: i32,
}

impl Decodable for Point {
    fn decode<D: Decoder>(d: &mut D) -> Result<Point, D::Error> {
        d.read_struct("Point", 2, |d| {
            let x = try!(d.read_struct_field("x", 0, |d| { d.read_i32() }));
            let y = try!(d.read_struct_field("y", 1, |d| { d.read_i32() }));
            Ok(Point{ x: x, y: y })
        })
    }
}

Required Methods

Deserialize a value using a Decoder.

Implementors