Skip to content

Commit 672ffcf

Browse files
committed
first step of restoring deprecated methods
1 parent c4efe11 commit 672ffcf

File tree

1 file changed

+212
-13
lines changed

1 file changed

+212
-13
lines changed

src/header/map.rs

Lines changed: 212 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,30 @@ impl HeaderMap {
445445
}
446446

447447
impl<T> HeaderMap<T> {
448-
#[deprecated = "use `try_with_capacity` instead"]
448+
/// Create an empty `HeaderMap` with the specified capacity.
449+
///
450+
/// The returned map will allocate internal storage in order to hold about
451+
/// `capacity` elements without reallocating. However, this is a "best
452+
/// effort" as there are usage patterns that could cause additional
453+
/// allocations before `capacity` headers are stored in the map.
454+
///
455+
/// More capacity than requested may be allocated.
456+
///
457+
/// # Panics
458+
///
459+
/// if capacity is >=
460+
///
461+
/// # Examples
462+
///
463+
/// ```
464+
/// # use http::HeaderMap;
465+
/// let map: HeaderMap<u32> = HeaderMap::with_capacity(10);
466+
///
467+
/// assert!(map.is_empty());
468+
/// assert_eq!(12, map.capacity());
469+
/// ```
449470
pub fn with_capacity(capacity: usize) -> HeaderMap<T> {
450-
Self::try_with_capacity(capacity).expect("failed to reserve")
471+
Self::try_with_capacity(capacity).expect("size overflows MAX_SIZE")
451472
}
452473

453474
/// Create an empty `HeaderMap` with the specified capacity.
@@ -614,7 +635,28 @@ impl<T> HeaderMap<T> {
614635
usable_capacity(self.indices.len())
615636
}
616637

617-
#[deprecated = "use try_reserve instead"]
638+
/// Reserves capacity for at least `additional` more headers to be inserted
639+
/// into the `HeaderMap`.
640+
///
641+
/// The header map may reserve more space to avoid frequent reallocations.
642+
/// Like with `with_capacity`, this will be a "best effort" to avoid
643+
/// allocations until `additional` more headers are inserted. Certain usage
644+
/// patterns could cause additional allocations before the number is
645+
/// reached.
646+
///
647+
/// # Panics
648+
///
649+
/// Panics if the new allocation size overflows `HeaderMap` `MAX_SIZE`.
650+
///
651+
/// # Examples
652+
///
653+
/// ```
654+
/// # use http::HeaderMap;
655+
/// # use http::header::HOST;
656+
/// let mut map = HeaderMap::new();
657+
/// map.try_reserve(10).unwrap();
658+
/// # map.insert(HOST, "bar".parse().unwrap());
659+
/// ```
618660
pub fn reserve(&mut self, additional: usize) {
619661
self.try_reserve(additional).expect("reservation failed")
620662
}
@@ -628,9 +670,12 @@ impl<T> HeaderMap<T> {
628670
/// patterns could cause additional allocations before the number is
629671
/// reached.
630672
///
631-
/// # Panics
673+
/// # Errors
632674
///
633-
/// Panics if the new allocation size overflows `usize`.
675+
/// This method differs from `reserve` by allowing types that may not be
676+
/// valid `HeaderName`s to passed as the key (such as `String`). If they
677+
/// do not parse as a valid `HeaderName`, this returns an
678+
/// `InvalidHeaderName` error.
634679
///
635680
/// # Examples
636681
///
@@ -639,7 +684,7 @@ impl<T> HeaderMap<T> {
639684
/// # use http::header::HOST;
640685
/// let mut map = HeaderMap::new();
641686
/// map.try_reserve(10).unwrap();
642-
/// # map.insert(HOST, "bar".parse().unwrap());
687+
/// # map.try_insert(HOST, "bar".parse().unwrap()).unwrap();
643688
/// ```
644689
pub fn try_reserve(&mut self, additional: usize) -> Result<(), CapacityOverflow> {
645690
// TODO: This can't overflow if done properly... since the max # of
@@ -1033,7 +1078,15 @@ impl<T> HeaderMap<T> {
10331078
}
10341079
}
10351080

1036-
#[deprecated = "use `try_entry` instead"]
1081+
/// Gets the given key's corresponding entry in the map for in-place
1082+
/// manipulation.
1083+
///
1084+
/// # Errors
1085+
///
1086+
/// This method differs from `entry` by allowing types that may not be
1087+
/// valid `HeaderName`s to passed as the key (such as `String`). If they
1088+
/// do not parse as a valid `HeaderName`, this returns an
1089+
/// `InvalidHeaderName` error.
10371090
pub fn entry<K>(&mut self, key: K) -> Entry<'_, T>
10381091
where
10391092
K: IntoHeaderName,
@@ -1094,7 +1147,33 @@ impl<T> HeaderMap<T> {
10941147
))
10951148
}
10961149

1097-
#[deprecated = "use `try_insert` instead"]
1150+
/// Inserts a key-value pair into the map.
1151+
///
1152+
/// If the map did not previously have this key present, then `None` is
1153+
/// returned.
1154+
///
1155+
/// If the map did have this key present, the new value is associated with
1156+
/// the key and all previous values are removed. **Note** that only a single
1157+
/// one of the previous values is returned. If there are multiple values
1158+
/// that have been previously associated with the key, then the first one is
1159+
/// returned. See `insert_mult` on `OccupiedEntry` for an API that returns
1160+
/// all values.
1161+
///
1162+
/// The key is not updated, though; this matters for types that can be `==`
1163+
/// without being identical.
1164+
///
1165+
/// # Examples
1166+
///
1167+
/// ```
1168+
/// # use http::HeaderMap;
1169+
/// # use http::header::HOST;
1170+
/// let mut map = HeaderMap::new();
1171+
/// assert!(map.try_insert(HOST, "world".parse().unwrap()).unwrap().is_none());
1172+
/// assert!(!map.is_empty());
1173+
///
1174+
/// let mut prev = map.try_insert(HOST, "earth".parse().unwrap()).unwrap().unwrap();
1175+
/// assert_eq!("world", prev);
1176+
/// ```
10981177
pub fn insert<K>(&mut self, key: K, val: T) -> Option<T>
10991178
where
11001179
K: IntoHeaderName,
@@ -1206,7 +1285,32 @@ impl<T> HeaderMap<T> {
12061285
}
12071286
}
12081287

1209-
#[deprecated = "use `try_append` instead"]
1288+
/// Inserts a key-value pair into the map.
1289+
///
1290+
/// If the map did not previously have this key present, then `false` is
1291+
/// returned.
1292+
///
1293+
/// If the map did have this key present, the new value is pushed to the end
1294+
/// of the list of values currently associated with the key. The key is not
1295+
/// updated, though; this matters for types that can be `==` without being
1296+
/// identical.
1297+
///
1298+
/// # Examples
1299+
///
1300+
/// ```
1301+
/// # use http::HeaderMap;
1302+
/// # use http::header::HOST;
1303+
/// let mut map = HeaderMap::new();
1304+
/// assert!(map.try_insert(HOST, "world".parse().unwrap()).unwrap().is_none());
1305+
/// assert!(!map.is_empty());
1306+
///
1307+
/// map.try_append(HOST, "earth".parse().unwrap()).unwrap();
1308+
///
1309+
/// let values = map.get_all("host");
1310+
/// let mut i = values.iter();
1311+
/// assert_eq!("world", *i.next().unwrap());
1312+
/// assert_eq!("earth", *i.next().unwrap());
1313+
/// ```
12101314
pub fn append<K>(&mut self, key: K, value: T) -> bool
12111315
where
12121316
K: IntoHeaderName,
@@ -2278,7 +2382,34 @@ unsafe impl<'a, T: Send> Send for Drain<'a, T> {}
22782382
// ===== impl Entry =====
22792383

22802384
impl<'a, T> Entry<'a, T> {
2281-
#[deprecated = "user `or_try_insert` instead"]
2385+
/// Ensures a value is in the entry by inserting the default if empty.
2386+
///
2387+
/// Returns a mutable reference to the **first** value in the entry.
2388+
///
2389+
/// # Examples
2390+
///
2391+
/// ```
2392+
/// # use http::HeaderMap;
2393+
/// let mut map: HeaderMap<u32> = HeaderMap::default();
2394+
///
2395+
/// let headers = &[
2396+
/// "content-length",
2397+
/// "x-hello",
2398+
/// "Content-Length",
2399+
/// "x-world",
2400+
/// ];
2401+
///
2402+
/// for &header in headers {
2403+
/// let counter = map.try_entry(header
2404+
/// .unwrap()
2405+
/// .or_try_insert(0)
2406+
/// .unwrap());
2407+
/// *counter += 1;
2408+
/// }
2409+
///
2410+
/// assert_eq!(map["content-length"], 2);
2411+
/// assert_eq!(map["x-hello"], 1);
2412+
/// ```
22822413
pub fn or_insert(self, default: T) -> &'a mut T {
22832414
self.or_try_insert(default).expect("capacity error")
22842415
}
@@ -2320,7 +2451,42 @@ impl<'a, T> Entry<'a, T> {
23202451
}
23212452
}
23222453

2323-
#[deprecated = "use `or_try_insert_with` instead"]
2454+
/// Ensures a value is in the entry by inserting the result of the default
2455+
/// function if empty.
2456+
///
2457+
/// The default function is not called if the entry exists in the map.
2458+
/// Returns a mutable reference to the **first** value in the entry.
2459+
///
2460+
/// # Examples
2461+
///
2462+
/// Basic usage.
2463+
///
2464+
/// ```
2465+
/// # use http::HeaderMap;
2466+
/// let mut map = HeaderMap::new();
2467+
///
2468+
/// let res = map.entry("x-hello")
2469+
/// .or_insert_with(|| "world".parse().unwrap());
2470+
///
2471+
/// assert_eq!(res, "world");
2472+
/// ```
2473+
///
2474+
/// The default function is not called if the entry exists in the map.
2475+
///
2476+
/// ```
2477+
/// # use http::HeaderMap;
2478+
/// # use http::header::HOST;
2479+
/// let mut map = HeaderMap::new();
2480+
/// map.try_insert(HOST, "world".parse().unwrap()).unwrap();
2481+
///
2482+
/// let res = map.try_entry("host")
2483+
/// .unwrap()
2484+
/// .or_try_insert_with(|| unreachable!())
2485+
/// .unwrap();
2486+
///
2487+
///
2488+
/// assert_eq!(res, "world");
2489+
/// ```
23242490
pub fn or_insert_with<F: FnOnce() -> T>(self, default: F) -> &'a mut T {
23252491
self.or_try_insert_with(default).expect("reservation error")
23262492
}
@@ -2423,7 +2589,23 @@ impl<'a, T> VacantEntry<'a, T> {
24232589
self.key
24242590
}
24252591

2426-
#[deprecated = "use `try_insert` instead"]
2592+
/// Insert the value into the entry.
2593+
///
2594+
/// The value will be associated with this entry's key. A mutable reference
2595+
/// to the inserted value will be returned.
2596+
///
2597+
/// # Examples
2598+
///
2599+
/// ```
2600+
/// # use http::header::{HeaderMap, Entry};
2601+
/// let mut map = HeaderMap::new();
2602+
///
2603+
/// if let Entry::Vacant(v) = map.entry("x-hello") {
2604+
/// v.insert("world".parse().unwrap());
2605+
/// }
2606+
///
2607+
/// assert_eq!(map["x-hello"], "world");
2608+
/// ```
24272609
pub fn insert(self, value: T) -> &'a mut T {
24282610
self.try_insert(value).expect("reservation failed")
24292611
}
@@ -2454,7 +2636,24 @@ impl<'a, T> VacantEntry<'a, T> {
24542636
Ok(&mut self.map.entries[index].value)
24552637
}
24562638

2457-
#[deprecated = "user `try_insert_entry` instead"]
2639+
/// Insert the value into the entry.
2640+
///
2641+
/// The value will be associated with this entry's key. The new
2642+
/// `OccupiedEntry` is returned, allowing for further manipulation.
2643+
///
2644+
/// # Examples
2645+
///
2646+
/// ```
2647+
/// # use http::header::*;
2648+
/// let mut map = HeaderMap::new();
2649+
///
2650+
/// if let Entry::Vacant(v) = map.try_entry("x-hello").unwrap() {
2651+
/// let mut e = v.try_insert_entry("world".parse().unwrap()).unwrap();
2652+
/// e.insert("world2".parse().unwrap());
2653+
/// }
2654+
///
2655+
/// assert_eq!(map["x-hello"], "world2");
2656+
/// ```
24582657
pub fn insert_entry(self, value: T) -> OccupiedEntry<'a, T> {
24592658
self.try_insert_entry(value).expect("reserve failed")
24602659
}

0 commit comments

Comments
 (0)