Skip to content

Commit e58102f

Browse files
committed
Document RawValue ownership
1 parent 82abb08 commit e58102f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/raw.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,53 @@ use error::Error;
6464
/// }
6565
/// ```
6666
///
67+
/// # Ownership
68+
///
69+
/// The typical usage of `RawValue` will be in the borrowed form:
70+
///
71+
/// ```
72+
/// # #[macro_use]
73+
/// # extern crate serde_derive;
74+
/// # extern crate serde_json;
75+
/// #
76+
/// # use serde_json::value::RawValue;
77+
/// #
78+
/// #[derive(Deserialize)]
79+
/// struct SomeStruct<'a> {
80+
/// #[serde(borrow)]
81+
/// raw_value: &'a RawValue,
82+
/// }
83+
/// #
84+
/// # fn main() {}
85+
/// ```
86+
///
87+
/// The borrowed form is suitable when deserializing through
88+
/// [`serde_json::from_str`] and [`serde_json::from_slice`] which support
89+
/// borrowing from the input data without memory allocation.
90+
///
91+
/// When deserializing through [`serde_json::from_reader`] you will need to use
92+
/// the boxed form of `RawValue` instead. This is almost as efficient but
93+
/// involves buffering the raw value from the I/O stream into memory.
94+
///
95+
/// [`serde_json::from_str`]: ../fn.from_str.html
96+
/// [`serde_json::from_slice`]: ../fn.from_slice.html
97+
/// [`serde_json::from_reader`]: ../fn.from_reader.html
98+
///
99+
/// ```
100+
/// # #[macro_use]
101+
/// # extern crate serde_derive;
102+
/// # extern crate serde_json;
103+
/// #
104+
/// # use serde_json::value::RawValue;
105+
/// #
106+
/// #[derive(Deserialize)]
107+
/// struct SomeStruct {
108+
/// raw_value: Box<RawValue>,
109+
/// }
110+
/// #
111+
/// # fn main() {}
112+
/// ```
113+
///
67114
/// # Note
68115
///
69116
/// `RawValue` is only available if serde\_json is built with the `"raw_value"`

0 commit comments

Comments
 (0)