rustc_serialize/lib.rs
1// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Support code for encoding and decoding types.
12//!
13//! > **NOTE**: This crate is deprecated in favor of [`serde`]. No new feature
14//! > development will happen in this crate, although bug fixes proposed through
15//! > PRs will still be merged. It is very highly recommended by the Rust
16//! > Library Team that you use [`serde`], not this crate.
17//!
18//! [`serde`]: https://serde.rs
19//!
20//! # Usage
21//!
22//! This crate is [on crates.io](https://crates.io/crates/rustc-serialize) and
23//! can be used by adding `rustc-serialize` to the dependencies in your
24//! project's `Cargo.toml`.
25//!
26//! ```toml
27//! [dependencies]
28//! rustc-serialize = "0.3"
29//! ```
30//!
31//! and this to your crate root:
32//!
33//! ```rust
34//! extern crate rustc_serialize;
35//! ```
36
37#![cfg_attr(rustbuild, feature(staged_api, rustc_private))]
38#![cfg_attr(rustbuild, unstable(feature = "rustc_private", issue = "27812"))]
39
40#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
41 html_favicon_url = "https://www.rust-lang.org/favicon.ico",
42 html_root_url = "https://doc.rust-lang.org/rustc-serialize/")]
43#![cfg_attr(test, deny(warnings))]
44#![allow(trivial_numeric_casts)]
45#![cfg_attr(rust_build, feature(staged_api))]
46#![cfg_attr(rust_build, staged_api)]
47#![cfg_attr(rust_build,
48 unstable(feature = "rustc_private",
49 reason = "use the crates.io `rustc-serialize` library instead"))]
50
51#[cfg(test)] extern crate rand;
52
53pub use self::serialize::{Decoder, Encoder, Decodable, Encodable,
54 DecoderHelpers, EncoderHelpers};
55
56
57// Limit collections from allocating more than
58// 1 MB for calls to `with_capacity`.
59fn cap_capacity<T>(given_len: usize) -> usize {
60 use std::cmp::min;
61 use std::mem::size_of;
62 const PRE_ALLOCATE_CAP: usize = 0x100000;
63
64 match size_of::<T>() {
65 0 => min(given_len, PRE_ALLOCATE_CAP),
66 n => min(given_len, PRE_ALLOCATE_CAP / n)
67 }
68}
69
70mod serialize;
71mod collection_impls;
72
73pub mod base64;
74pub mod hex;
75pub mod json;
76
77mod rustc_serialize {
78 pub use serialize::*;
79}