serde_pickle/lib.rs
1// Copyright (c) 2015-2024 Georg Brandl. Licensed under the Apache License,
2// Version 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>
3// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at
4// your option. This file may not be copied, modified, or distributed except
5// according to those terms.
6
7//! Serialization and deserialization for Python's pickle format
8//!
9//! # Pickle format
10//!
11//! Please see the [Python docs](http://docs.python.org/library/pickle) for
12//! details on the Pickle format.
13//!
14//! This crate supports all Pickle protocols (0 to 5) when reading, and writing
15//! protocol 2 (compatible with Python 2 and 3), or protocol 3 (compatible with
16//! Python 3 only).
17//!
18//! # Supported types
19//!
20//! Pickle is very powerful. It is capable of serializing pretty arbitrary
21//! graphs of Python objects, with most custom classes being serialized out
22//! of the box. Currently, this crate only supports Python's built-in types
23//! that map easily to Rust constructs. There are:
24//!
25//! * None
26//! * Boolean (Rust `bool`)
27//! * Integers (Rust `i64` or bigints from num)
28//! * Floats (Rust `f64`)
29//! * Bytes objects and bytearrays (see below)
30//! * (Unicode) strings (Rust `String`)
31//! * Lists and tuples (Rust `Vec<Value>`)
32//! * Sets and frozensets (Rust `HashSet<Value>`)
33//! * Dictionaries (Rust `HashMap<Value, Value>`)
34//!
35//! When deserializing, arbitrary Python objects saved using a pickled instance
36//! dictionary or `__setstate__` are replaced by that state, since version 0.5
37//! of this library. More saved state is kept by enabling the
38//! `keep_restore_state` option in `DeOptions`, as of version 1.2.
39//!
40//! *Note on enums:* Enum variants are serialized as Python tuples `(name,
41//! [data])` instead of mappings (or a plain string for unit variants), which is
42//! the representation selected by e.g. `serde_json`. On deserialization, both
43//! the tuple form and the string/mapping form is accepted.
44//!
45//! *Note on bytes objects:* when deserializing bytes objects, you have to use a
46//! Rust wrapper type that enables deserialization from the serde data model's
47//! "bytes" type. The [`serde_bytes`](https://docs.serde.rs/serde_bytes/) crate
48//! provides such wrappers.
49//!
50//! Likewise, `Vec<u8>`, `[u8; N]` and `&[u8]` are treated as sequences when
51//! serializing. This means that they will be serialized as a tuple or list of
52//! integers unless you use one of the wrappers in `serde_bytes`.
53//!
54//! # Unsupported features
55//!
56//! - Recursive objects using the `PERSID` and `EXT` type opcodes.
57//! - Out-of-band data as introduced in Pickle protocol 5.
58//!
59//! # Exported API
60//!
61//! The library exports generic serde (de)serializing functions `to_*` and
62//! `from_*`. It also exports functions that produce or take only the specific
63//! `Value` struct exposed by this library, which supports all built-in Python
64//! types (notably, long integers and sets, which serde's generic types don't
65//! handle). These functions, called `value_from_*` and `value_to_*`, will
66//! correctly (un)pickle these types.
67//!
68//! # Minimum Supported Rust Version
69//!
70//! The minimum supported version of the toolchain is 1.41.1.
71
72pub use self::ser::{to_vec, to_writer, value_to_vec, value_to_writer, SerOptions, Serializer};
73
74pub use self::de::{
75 from_iter, from_reader, from_slice, value_from_iter, value_from_reader, value_from_slice, DeOptions,
76 Deserializer,
77};
78
79pub use self::value::{from_value, to_value, HashableValue, Value};
80
81pub use self::error::{Error, ErrorCode, Result};
82
83mod consts;
84pub mod de;
85pub mod error;
86pub mod ser;
87pub mod value;
88mod value_impls;
89
90#[cfg(test)]
91#[path = "../test/mod.rs"]
92mod test;