@@ -445,9 +445,30 @@ impl HeaderMap {
445
445
}
446
446
447
447
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
+ /// ```
449
470
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 " )
451
472
}
452
473
453
474
/// Create an empty `HeaderMap` with the specified capacity.
@@ -614,7 +635,28 @@ impl<T> HeaderMap<T> {
614
635
usable_capacity ( self . indices . len ( ) )
615
636
}
616
637
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
+ /// ```
618
660
pub fn reserve ( & mut self , additional : usize ) {
619
661
self . try_reserve ( additional) . expect ( "reservation failed" )
620
662
}
@@ -628,9 +670,12 @@ impl<T> HeaderMap<T> {
628
670
/// patterns could cause additional allocations before the number is
629
671
/// reached.
630
672
///
631
- /// # Panics
673
+ /// # Errors
632
674
///
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.
634
679
///
635
680
/// # Examples
636
681
///
@@ -639,7 +684,7 @@ impl<T> HeaderMap<T> {
639
684
/// # use http::header::HOST;
640
685
/// let mut map = HeaderMap::new();
641
686
/// map.try_reserve(10).unwrap();
642
- /// # map.insert (HOST, "bar".parse().unwrap());
687
+ /// # map.try_insert (HOST, "bar".parse().unwrap()).unwrap( );
643
688
/// ```
644
689
pub fn try_reserve ( & mut self , additional : usize ) -> Result < ( ) , CapacityOverflow > {
645
690
// TODO: This can't overflow if done properly... since the max # of
@@ -1033,7 +1078,15 @@ impl<T> HeaderMap<T> {
1033
1078
}
1034
1079
}
1035
1080
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.
1037
1090
pub fn entry < K > ( & mut self , key : K ) -> Entry < ' _ , T >
1038
1091
where
1039
1092
K : IntoHeaderName ,
@@ -1094,7 +1147,33 @@ impl<T> HeaderMap<T> {
1094
1147
) )
1095
1148
}
1096
1149
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
+ /// ```
1098
1177
pub fn insert < K > ( & mut self , key : K , val : T ) -> Option < T >
1099
1178
where
1100
1179
K : IntoHeaderName ,
@@ -1206,7 +1285,32 @@ impl<T> HeaderMap<T> {
1206
1285
}
1207
1286
}
1208
1287
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
+ /// ```
1210
1314
pub fn append < K > ( & mut self , key : K , value : T ) -> bool
1211
1315
where
1212
1316
K : IntoHeaderName ,
@@ -2278,7 +2382,34 @@ unsafe impl<'a, T: Send> Send for Drain<'a, T> {}
2278
2382
// ===== impl Entry =====
2279
2383
2280
2384
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
+ /// ```
2282
2413
pub fn or_insert ( self , default : T ) -> & ' a mut T {
2283
2414
self . or_try_insert ( default) . expect ( "capacity error" )
2284
2415
}
@@ -2320,7 +2451,42 @@ impl<'a, T> Entry<'a, T> {
2320
2451
}
2321
2452
}
2322
2453
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
+ /// ```
2324
2490
pub fn or_insert_with < F : FnOnce ( ) -> T > ( self , default : F ) -> & ' a mut T {
2325
2491
self . or_try_insert_with ( default) . expect ( "reservation error" )
2326
2492
}
@@ -2423,7 +2589,23 @@ impl<'a, T> VacantEntry<'a, T> {
2423
2589
self . key
2424
2590
}
2425
2591
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
+ /// ```
2427
2609
pub fn insert ( self , value : T ) -> & ' a mut T {
2428
2610
self . try_insert ( value) . expect ( "reservation failed" )
2429
2611
}
@@ -2454,7 +2636,24 @@ impl<'a, T> VacantEntry<'a, T> {
2454
2636
Ok ( & mut self . map . entries [ index] . value )
2455
2637
}
2456
2638
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
+ /// ```
2458
2657
pub fn insert_entry ( self , value : T ) -> OccupiedEntry < ' a , T > {
2459
2658
self . try_insert_entry ( value) . expect ( "reserve failed" )
2460
2659
}
0 commit comments