1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! `serde` support for `HipPath`.
//!
//! This module provides support for serializing and deserializing `HipStr`
//! using [`serde`]. It is enabled by default when the `serde` feature is
//! enabled.
//!
//! # Examples
//!
//! ```
//! use hipstr::HipPath;
//!
//! let s = HipPath::borrowed("/usr/bin");
//! let serialized = serde_json::to_string(&s).unwrap();
//! assert_eq!(serialized, r#""/usr/bin""#);
//!
//! let deserialized: HipPath = serde_json::from_str(&serialized).unwrap();
//! assert_eq!(deserialized, s);
//! ```
//!
//! # Notable aspects of the implementation
//!
//! During deserialization, this implementation minimizes allocations by reusing
//! the deserializer's internal buffer if possible.
//!
//! Unlike `PathBuf`'s `Deserialize`, this implementation declares transparently
//! that it's expecting a string. Indeed not reusing `HipStr`'s implementation
//! just does not make any sense.
use ;
use HipPath;
use crateHipStr;
use crateBackend;
/// Deserializes a `HipPath` as a borrow if possible.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use hipstr::HipPath;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct MyStruct<'a> {
/// #[serde(borrow, deserialize_with = "hipstr::path::serde::borrow_deserialize")]
/// field: HipPath<'a>,
/// }
///
/// # fn main() {
/// let s: MyStruct = serde_json::from_str(r#"{"field": "abc"}"#).unwrap();
/// assert!(s.field.is_borrowed());
/// # }
/// ```
///
/// # Errors
///
/// Returns a deserializer error if either the serialization is incorrect or an unexpected value is encountered.