rustc_serialize

Trait Encodable

Source
pub trait Encodable {
    // Required method
    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>;
}
Expand description

Trait for serializing a type.

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

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

§Examples

extern crate rustc_serialize;

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

This generates code equivalent to:

extern crate rustc_serialize;
use rustc_serialize::Encodable;
use rustc_serialize::Encoder;

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

impl Encodable for Point {
    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
        s.emit_struct("Point", 2, |s| {
            try!(s.emit_struct_field("x", 0, |s| {
                s.emit_i32(self.x)
            }));
            try!(s.emit_struct_field("y", 1, |s| {
                s.emit_i32(self.y)
            }));
            Ok(())
        })
    }
}

Required Methods§

Source

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Serialize a value using an Encoder.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Encodable for bool

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for char

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for f32

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for f64

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for i8

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for i16

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for i32

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for i64

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for isize

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for str

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for u8

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for u16

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for u32

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for u64

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for ()

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for usize

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for String

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for Path

Source§

fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error>

Source§

impl Encodable for PathBuf

Source§

fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error>

Source§

impl<'a, T: Encodable + ToOwned + ?Sized> Encodable for Cow<'a, T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<'a, T: ?Sized + Encodable> Encodable for &'a T

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<K, V> Encodable for HashMap<K, V>
where K: Encodable + Hash + Eq, V: Encodable,

Source§

fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error>

Source§

impl<K: Encodable + Ord, V: Encodable> Encodable for BTreeMap<K, V>

Source§

fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error>

Source§

impl<T0: Encodable, T1: Encodable, T2: Encodable, T3: Encodable, T4: Encodable, T5: Encodable, T6: Encodable, T7: Encodable, T8: Encodable, T9: Encodable, T10: Encodable, T11: Encodable> Encodable for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T1: Encodable, T2: Encodable, T3: Encodable, T4: Encodable, T5: Encodable, T6: Encodable, T7: Encodable, T8: Encodable, T9: Encodable, T10: Encodable, T11: Encodable> Encodable for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T2: Encodable, T3: Encodable, T4: Encodable, T5: Encodable, T6: Encodable, T7: Encodable, T8: Encodable, T9: Encodable, T10: Encodable, T11: Encodable> Encodable for (T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T3: Encodable, T4: Encodable, T5: Encodable, T6: Encodable, T7: Encodable, T8: Encodable, T9: Encodable, T10: Encodable, T11: Encodable> Encodable for (T3, T4, T5, T6, T7, T8, T9, T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T4: Encodable, T5: Encodable, T6: Encodable, T7: Encodable, T8: Encodable, T9: Encodable, T10: Encodable, T11: Encodable> Encodable for (T4, T5, T6, T7, T8, T9, T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T5: Encodable, T6: Encodable, T7: Encodable, T8: Encodable, T9: Encodable, T10: Encodable, T11: Encodable> Encodable for (T5, T6, T7, T8, T9, T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T6: Encodable, T7: Encodable, T8: Encodable, T9: Encodable, T10: Encodable, T11: Encodable> Encodable for (T6, T7, T8, T9, T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T7: Encodable, T8: Encodable, T9: Encodable, T10: Encodable, T11: Encodable> Encodable for (T7, T8, T9, T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T8: Encodable, T9: Encodable, T10: Encodable, T11: Encodable> Encodable for (T8, T9, T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T9: Encodable, T10: Encodable, T11: Encodable> Encodable for (T9, T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T10: Encodable, T11: Encodable> Encodable for (T10, T11)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T11: Encodable> Encodable for (T11,)

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T> Encodable for PhantomData<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T> Encodable for HashSet<T>
where T: Encodable + Hash + Eq,

Source§

fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error>

Source§

impl<T: Encodable + Ord> Encodable for BTreeSet<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable + Copy> Encodable for Cell<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for Option<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 0]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 1]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 2]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 3]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 4]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 5]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 6]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 7]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 8]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 9]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 10]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 11]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 12]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 13]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 14]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 15]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 16]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 17]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 18]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 19]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 20]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 21]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 22]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 23]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 24]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 25]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 26]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 27]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 28]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 29]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 30]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 31]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T; 32]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for [T]

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for LinkedList<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for VecDeque<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for Rc<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for Arc<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for Vec<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable> Encodable for RefCell<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: Encodable, E: Encodable> Encodable for Result<T, E>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Source§

impl<T: ?Sized + Encodable> Encodable for Box<T>

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Implementors§