vecset 0.0.3

A vector-based sorted map, set and keyed-set implementation
Documentation
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
use super::{Keyed, KeyedVecSet};
use core::mem;
use core::ops::Deref;

/// `&mut V` wrapper to defer unsafe operations.
///
/// Accessing `&mut V` in `KeyedVecSet` is unsafe because changing key may cause the map to be unsorted or have duplicate keys.
/// Since most of operations in `Entry` returns `&mut V`, they must be regarded as unsafe in principle.
///
/// On the other hand, those entry operations are actually safe unless mutably accessing the returned value.
/// `Mut` helps to regard entry operations itself as safe, but accessing the returned value as unsafe.
pub struct Mut<'a, V>(&'a mut V);

impl<V> Mut<'_, V> {
    /// # Safety
    /// Changing key may cause the map to be unsorted or have duplicate keys. Those states will make `KeyedVecSet` working unspecified way. Those states will make `KeyedVecSet` working unspecified way.
    pub unsafe fn as_mut(&mut self) -> &mut V {
        self.0
    }
}

impl<V> Deref for Mut<'_, V> {
    type Target = V;

    fn deref(&self) -> &V {
        self.0
    }
}

/// Entry for an existing key-value pair or a vacant location to insert one.
#[derive(Debug)]
pub enum Entry<'a, K, V> {
    /// Existing slot with equivalent key.
    Occupied(OccupiedEntry<'a, K, V>),
    /// Vacant slot (no equivalent key in the map).
    Vacant(VacantEntry<'a, K, V>),
}

impl<'a, K, V> Entry<'a, K, V>
where
    K: Ord,
    V: Keyed<K>,
{
    /// Ensures a value is in the entry by inserting the default if empty, and returns a mutable
    /// reference to the value in the entry.
    ///
    /// # Panics
    ///
    /// Panics if the key of the provided value doesn't match the entry's key.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    ///
    /// map.entry("poneyland").or_insert(("poneyland", 3));
    /// assert_eq!(map["poneyland"].1, 3);
    ///
    /// unsafe { map.entry("poneyland").or_insert(("poneyland", 10)).as_mut().1 *= 2 };
    /// assert_eq!(map["poneyland"].1, 6);
    /// ```
    pub fn or_insert(self, default: V) -> Mut<'a, V> {
        assert!(self.key() == default.key());
        match self {
            Entry::Occupied(entry) => Mut(unsafe { entry.into_mut() }),
            Entry::Vacant(entry) => entry.insert(default),
        }
    }

    /// Ensures a value is in the entry by inserting the result of the default function if empty,
    /// and returns a mutable reference to the value in the entry.
    ///
    /// # Panics
    ///
    /// Panics if the key of the value returned by the function doesn't match the entry's key.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::{Keyed, KeyedVecSet};
    ///
    /// let mut map: KeyedVecSet<&str, (&str, String)> = KeyedVecSet::new();
    /// let s = "hoho".to_string();
    ///
    /// map.entry("poneyland").or_insert_with(|| ("poneyland", s));
    ///
    /// assert_eq!(map["poneyland"].1, "hoho".to_string());
    /// ```
    pub fn or_insert_with<F>(self, call: F) -> Mut<'a, V>
    where
        F: FnOnce() -> V,
    {
        match self {
            Entry::Occupied(entry) => Mut(unsafe { entry.into_mut() }),
            Entry::Vacant(entry) => {
                let value = call();
                assert!(&entry.key == value.key());
                entry.insert(value)
            }
        }
    }

    /// Ensures a value is in the entry by inserting, if empty, the result of the default function.
    /// This method allows for generating key-derived values for insertion by providing the default
    /// function a reference to the key that was moved during the `.entry(key)` method call.
    ///
    /// The reference to the moved key is provided so that cloning or copying the key is
    /// unnecessary, unlike with `.or_insert_with(|| ... )`.
    ///
    /// # Panics
    ///
    /// Panics if the key of the value returned by the function doesn't match the entry's key.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, usize)> = KeyedVecSet::new();
    ///
    /// map.entry("poneyland").or_insert_with_key(|key| (key, key.chars().count()));
    ///
    /// assert_eq!(map["poneyland"].1, 9);
    /// ```
    pub fn or_insert_with_key<F>(self, default: F) -> Mut<'a, V>
    where
        F: FnOnce(&K) -> V,
    {
        match self {
            Entry::Occupied(entry) => Mut(unsafe { entry.into_mut() }),
            Entry::Vacant(entry) => {
                let value = default(&entry.key);
                assert!(&entry.key == value.key());
                entry.insert(value)
            }
        }
    }

    /// Ensures a value is in the entry by inserting if it was vacant, and returns
    /// the index of the entry and whether it was inserted.
    ///
    /// # Panics
    ///
    /// Panics if the key of the provided value doesn't match the entry's key.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map = KeyedVecSet::new();
    /// let (index, inserted) = map.entry("a").or_insert_full(("a", 1));
    /// assert_eq!(index, 0);
    /// assert_eq!(inserted, true);
    ///
    /// let (index, inserted) = map.entry("a").or_insert_full(("a", 2));
    /// assert_eq!(index, 0);
    /// assert_eq!(inserted, false);
    /// ```
    pub fn or_insert_full(self, value: V) -> (usize, bool) {
        assert!(self.key() == value.key());
        match self {
            Entry::Occupied(e) => (e.index, false),
            Entry::Vacant(e) => {
                let index = e.index;
                e.map.base.insert(index, value);
                (index, true)
            }
        }
    }

    /// Ensures a value is in the entry by inserting if it was vacant, and returns
    /// the index of the entry and whether it was inserted.
    ///
    /// This version doesn't check if the key matches.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the key of the provided value matches the entry's key.
    /// Failing to do so may result in a map with unsorted keys or duplicate keys.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map = KeyedVecSet::new();
    /// let (index, inserted) = unsafe { map.entry("a").or_insert_full_unchecked(("a", 1)) };
    /// assert_eq!(index, 0);
    /// assert_eq!(inserted, true);
    ///
    /// let (index, inserted) = unsafe { map.entry("a").or_insert_full_unchecked(("a", 2)) };
    /// assert_eq!(index, 0);
    /// assert_eq!(inserted, false);
    /// ```
    pub unsafe fn or_insert_full_unchecked(self, value: V) -> (usize, bool) {
        match self {
            Entry::Occupied(e) => (e.index, false),
            Entry::Vacant(e) => {
                let index = e.index;
                e.map.base.insert(index, value);
                (index, true)
            }
        }
    }

    /// Ensures a value is in the entry by inserting the result of the default function if empty,
    /// and returns the index of the entry and whether it was inserted.
    ///
    /// # Panics
    ///
    /// Panics if the key of the value returned by the function doesn't match the entry's key.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map = KeyedVecSet::new();
    /// let (index, inserted) = map.entry("a").or_insert_with_full(|| ("a", 1));
    /// assert_eq!(index, 0);
    /// assert_eq!(inserted, true);
    ///
    /// let (index, inserted) = map.entry("a").or_insert_with_full(|| ("a", 2));
    /// assert_eq!(index, 0);
    /// assert_eq!(inserted, false);
    /// ```
    pub fn or_insert_with_full<F>(self, call: F) -> (usize, bool)
    where
        F: FnOnce() -> V,
    {
        match self {
            Entry::Occupied(e) => (e.index, false),
            Entry::Vacant(e) => {
                let index = e.index;
                let value = call();
                assert!(&e.key == value.key());
                e.map.base.insert(index, value);
                (index, true)
            }
        }
    }

    /// Ensures a value is in the entry by inserting the result of the default function if empty,
    /// and returns the index of the entry and whether it was inserted.
    ///
    /// This version doesn't check if the key matches.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the key of the value returned by the function matches the entry's key.
    /// Failing to do so may result in a map with unsorted keys or duplicate keys.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map = KeyedVecSet::new();
    /// let (index, inserted) = unsafe { map.entry("a").or_insert_with_full_unchecked(|| ("a", 1)) };
    /// assert_eq!(index, 0);
    /// assert_eq!(inserted, true);
    ///
    /// let (index, inserted) = unsafe { map.entry("a").or_insert_with_full_unchecked(|| ("a", 2)) };
    /// assert_eq!(index, 0);
    /// assert_eq!(inserted, false);
    /// ```
    pub unsafe fn or_insert_with_full_unchecked<F>(self, call: F) -> (usize, bool)
    where
        F: FnOnce() -> V,
    {
        match self {
            Entry::Occupied(e) => (e.index, false),
            Entry::Vacant(e) => {
                let index = e.index;
                let value = call();
                e.map.base.insert(index, value);
                (index, true)
            }
        }
    }

    /// Returns a reference to this entry's key.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map: KeyedVecSet<&str, &str> = KeyedVecSet::new();
    /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
    /// ```
    pub fn key(&self) -> &K {
        match self {
            Entry::Occupied(entry) => entry.key(),
            Entry::Vacant(entry) => entry.key(),
        }
    }

    /// Returns the index where the key-value pair exists or will be inserted.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map: KeyedVecSet<&str, &str> = KeyedVecSet::new();
    /// assert_eq!(map.entry("poneyland").index(), 0);
    /// ```
    pub fn index(&self) -> usize {
        match self {
            Entry::Occupied(entry) => entry.index(),
            Entry::Vacant(entry) => entry.index(),
        }
    }

    /// Provides in-place mutable access to an occupied entry before any potential inserts into the
    /// map.
    ///
    /// # Safety
    /// Changing key may cause the map to be unsorted or have duplicate keys. Those states will make `KeyedVecSet` working unspecified way. Those states will make `KeyedVecSet` working unspecified way.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    ///
    /// unsafe {
    ///    map.entry("poneyland")
    ///        .and_modify(|e| { e.1 += 1 })
    /// }.or_insert(("poneyland", 42));
    /// assert_eq!(map["poneyland"].1, 42);
    ///
    /// unsafe {
    ///     map.entry("poneyland")
    ///        .and_modify(|e| { e.1 += 1 })
    /// }.or_insert(("poneyland", 42));
    /// assert_eq!(map["poneyland"].1, 43);
    /// ```
    pub unsafe fn and_modify<F>(self, f: F) -> Self
    where
        F: FnOnce(&mut V),
    {
        match self {
            Entry::Occupied(mut o) => {
                unsafe { f(o.get_mut()) };
                Entry::Occupied(o)
            }
            x => x,
        }
    }
}

/// A view into an occupied entry in a `KeyedVecSet`. It is part of the [`Entry`] enum.
#[derive(Debug)]
pub struct OccupiedEntry<'a, K, V> {
    map: &'a mut KeyedVecSet<K, V>,
    key: K,
    index: usize,
}

impl<'a, K, V> OccupiedEntry<'a, K, V> {
    pub(super) fn new(
        map: &'a mut KeyedVecSet<K, V>,
        key: K,
        index: usize,
    ) -> OccupiedEntry<'a, K, V> {
        OccupiedEntry { map, key, index }
    }

    /// Gets a reference to the key in the entry.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    /// map.entry("poneyland").or_insert(("poneyland", 12));
    /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
    /// ```
    pub fn key(&self) -> &K {
        &self.key
    }

    /// Gets the index of the entry.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    /// map.entry("poneyland").or_insert(("poneyland", 12));
    /// assert_eq!(map.entry("poneyland").index(), 0);
    /// ```
    pub fn index(&self) -> usize {
        self.index
    }

    /// Take ownership of the key.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    /// use vecset::keyed::Entry;
    ///
    /// let mut map: KeyedVecSet<&str, &str> = KeyedVecSet::new();
    /// map.insert("foo");
    ///
    /// if let Entry::Occupied(v) = map.entry("foo") {
    ///     v.into_key();
    /// }
    /// ```
    pub fn into_key(self) -> K {
        self.key
    }

    /// Gets a reference to the value in the entry.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    /// use vecset::keyed::Entry;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    /// map.entry("poneyland").or_insert(("poneyland", 12));
    ///
    /// if let Entry::Occupied(o) = map.entry("poneyland") {
    ///     assert_eq!(o.get().1, 12);
    /// }
    /// ```
    pub fn get(&self) -> &V {
        &self.map[self.index]
    }

    /// Gets a mutable reference to the value in the entry.
    ///
    /// If you need a reference to the `OccupiedEntry` which may outlive the
    /// destruction of the `Entry` value, see [`into_mut`].
    ///
    /// [`into_mut`]: Self::into_mut
    ///
    /// # Safety
    /// Changing key may cause the map to be unsorted or have duplicate keys. Those states will make `KeyedVecSet` working unspecified way.
    ///
    /// # Panics
    ///
    /// Panics if the key doesn't exist (which should not happen for an `OccupiedEntry`).
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    /// use vecset::keyed::Entry;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    /// map.entry("poneyland").or_insert(("poneyland", 12));
    ///
    /// assert_eq!(map["poneyland"].1, 12);
    /// if let Entry::Occupied(mut o) = map.entry("poneyland") {
    ///     unsafe { o.get_mut().1 += 10 };
    ///     assert_eq!(o.get().1, 22);
    ///
    ///     // We can use the same Entry multiple times.
    ///     unsafe { o.get_mut().1 += 2 };
    /// }
    ///
    /// assert_eq!(map["poneyland"].1, 24);
    /// ```
    pub unsafe fn get_mut(&mut self) -> &mut V {
        unsafe { self.map.get_index_mut(self.index).expect("unexisting key") }
    }

    /// Converts the `OccupiedEntry` into a mutable reference to the value in the entry
    /// with a lifetime bound to the map itself.
    ///
    /// If you need multiple references to the `OccupiedEntry`, see [`get_mut`].
    ///
    /// [`get_mut`]: Self::get_mut
    ///
    /// # Safety
    /// Changing key may cause the map to be unsorted or have duplicate keys. Those states will make `KeyedVecSet` working unspecified way.
    ///
    /// # Panics
    ///
    /// Panics if the key doesn't exist (which should not happen for an `OccupiedEntry`).
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    /// use vecset::keyed::Entry;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    /// map.entry("poneyland").or_insert(("poneyland", 12));
    ///
    /// assert_eq!(map["poneyland"].1, 12);
    /// if let Entry::Occupied(o) = map.entry("poneyland") {
    ///     unsafe { o.into_mut().1 += 10 };
    /// }
    ///
    /// assert_eq!(map["poneyland"].1, 22);
    /// ```
    pub unsafe fn into_mut(self) -> &'a mut V {
        unsafe { self.map.get_index_mut(self.index).expect("unexisting key") }
    }

    /// Sets the value of the entry, and returns the entry's old value.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    /// use vecset::keyed::Entry;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    /// map.entry("poneyland").or_insert(("poneyland", 12));
    ///
    /// if let Entry::Occupied(mut o) = map.entry("poneyland") {
    ///     assert_eq!(o.insert(("poneyland", 15)), ("poneyland", 12));
    /// }
    ///
    /// assert_eq!(map["poneyland"].1, 15);
    /// ```
    pub fn insert(&mut self, value: V) -> V {
        mem::replace(unsafe { self.get_mut() }, value)
    }

    /// Removes and return the key-value pair stored in the map for this entry.
    ///
    /// Like `Vec::remove`, the pair is removed by shifting all of the elements that follow it,
    /// preserving their relative order. **This perturbs the index of all of those elements!**
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    /// use vecset::keyed::Entry;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    /// map.entry("poneyland").or_insert(("poneyland", 12));
    /// map.entry("foo").or_insert(("foo", 13));
    /// map.entry("bar").or_insert(("bar", 14));
    ///
    /// if let Entry::Occupied(o) = map.entry("poneyland") {
    ///     // We delete the entry from the map.
    ///     o.remove_entry();
    /// }
    ///
    /// assert_eq!(map.contains_key("poneyland"), false);
    /// assert_eq!(map.binary_search("bar"), Ok(0));
    /// assert_eq!(map.binary_search("foo"), Ok(1));
    /// ```
    pub fn remove_entry(self) -> V {
        self.map.remove_index(self.index)
    }

    /// Removes the key-value pair stored in the map for this entry, and return the value.
    ///
    /// Like `Vec::remove`, the pair is removed by shifting all of the elements that follow it,
    /// preserving their relative order. **This perturbs the index of all of those elements!**
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    /// use vecset::keyed::Entry;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    /// map.entry("poneyland").or_insert(("poneyland", 12));
    /// map.entry("foo").or_insert(("foo", 13));
    /// map.entry("bar").or_insert(("bar", 14));
    ///
    /// if let Entry::Occupied(o) = map.entry("poneyland") {
    ///     assert_eq!(o.remove().1, 12);
    /// }
    ///
    /// assert_eq!(map.contains_key("poneyland"), false);
    /// assert_eq!(map.binary_search("bar"), Ok(0));
    /// assert_eq!(map.binary_search("foo"), Ok(1));
    /// ```
    pub fn remove(self) -> V {
        self.remove_entry()
    }
}

/// A view into a vacant entry in a `KeyedVecSet`. It is part of the [`Entry`] enum.
#[derive(Debug)]
pub struct VacantEntry<'a, K, V> {
    map: &'a mut KeyedVecSet<K, V>,
    key: K,
    index: usize,
}

impl<'a, K, V> VacantEntry<'a, K, V> {
    pub(super) fn new(
        map: &'a mut KeyedVecSet<K, V>,
        key: K,
        index: usize,
    ) -> VacantEntry<'a, K, V> {
        VacantEntry { map, key, index }
    }

    /// Gets a reference to the key that would be used when inserting a value through the
    /// `VacantEntry`.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map: KeyedVecSet<&str, &str> = KeyedVecSet::new();
    /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
    /// ```
    pub fn key(&self) -> &K {
        &self.key
    }

    /// Return the index where the key-value pair will be inserted.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    ///
    /// let mut map: KeyedVecSet<&str, &str> = KeyedVecSet::new();
    /// assert_eq!(map.entry("poneyland").index(), 0);
    /// ```
    pub fn index(&self) -> usize {
        self.index
    }

    /// Take ownership of the key.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    /// use vecset::keyed::Entry;
    ///
    /// let mut map: KeyedVecSet<&str, &str> = KeyedVecSet::new();
    ///
    /// if let Entry::Vacant(v) = map.entry("poneyland") {
    ///     v.into_key();
    /// }
    /// ```
    pub fn into_key(self) -> K {
        self.key
    }

    /// Sets the value of the entry with the `VacantEntry`'s key, and returns a mutable reference
    /// to it.
    ///
    /// # Examples
    ///
    /// ```
    /// use vecset::KeyedVecSet;
    /// use vecset::keyed::Entry;
    ///
    /// let mut map: KeyedVecSet<&str, (&str, u32)> = KeyedVecSet::new();
    ///
    /// if let Entry::Vacant(o) = map.entry("poneyland") {
    ///     o.insert(("poneyland", 37));
    /// }
    /// assert_eq!(map["poneyland"], ("poneyland", 37));
    /// ```
    pub fn insert(self, value: V) -> Mut<'a, V>
    where
        K: Ord,
        V: Keyed<K>,
    {
        self.map.base.insert(self.index, value);
        Mut(unsafe { self.map.base.get_unchecked_mut(self.index) })
    }
}