Skip to content

Commit 87cda67

Browse files
authored
Rollup merge of rust-lang#91749 - ssomers:btree_comments, r=Mark-Simulacrum
BTree: improve public descriptions and comments BTreeSet has always used the term "value" next to and meaning the same thing as "elements" (in the mathematical sense but also used for key-value pairs in BTreeMap), while in the BTreeMap sense these "values" are known as "keys" and definitely not "values". Today I had enough of that. r? `@Mark-Simulacrum`
2 parents 6227d42 + 27b4b19 commit 87cda67

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed

library/alloc/src/collections/btree/set.rs

+56-53
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ enum DifferenceInner<'a, T: 'a> {
155155
self_iter: Iter<'a, T>,
156156
other_set: &'a BTreeSet<T>,
157157
},
158-
Iterate(Iter<'a, T>), // simply produce all values in `self`
158+
Iterate(Iter<'a, T>), // simply produce all elements in `self`
159159
}
160160

161161
#[stable(feature = "collection_debug", since = "1.17.0")]
@@ -207,7 +207,7 @@ enum IntersectionInner<'a, T: 'a> {
207207
small_iter: Iter<'a, T>,
208208
large_set: &'a BTreeSet<T>,
209209
},
210-
Answer(Option<&'a T>), // return a specific value or emptiness
210+
Answer(Option<&'a T>), // return a specific element or emptiness
211211
}
212212

213213
#[stable(feature = "collection_debug", since = "1.17.0")]
@@ -295,8 +295,8 @@ impl<T> BTreeSet<T> {
295295
Range { iter: self.map.range(range) }
296296
}
297297

298-
/// Visits the values representing the difference,
299-
/// i.e., the values that are in `self` but not in `other`,
298+
/// Visits the elements representing the difference,
299+
/// i.e., the elements that are in `self` but not in `other`,
300300
/// in ascending order.
301301
///
302302
/// # Examples
@@ -356,8 +356,8 @@ impl<T> BTreeSet<T> {
356356
}
357357
}
358358

359-
/// Visits the values representing the symmetric difference,
360-
/// i.e., the values that are in `self` or in `other` but not in both,
359+
/// Visits the elements representing the symmetric difference,
360+
/// i.e., the elements that are in `self` or in `other` but not in both,
361361
/// in ascending order.
362362
///
363363
/// # Examples
@@ -384,8 +384,8 @@ impl<T> BTreeSet<T> {
384384
SymmetricDifference(MergeIterInner::new(self.iter(), other.iter()))
385385
}
386386

387-
/// Visits the values representing the intersection,
388-
/// i.e., the values that are both in `self` and `other`,
387+
/// Visits the elements representing the intersection,
388+
/// i.e., the elements that are both in `self` and `other`,
389389
/// in ascending order.
390390
///
391391
/// # Examples
@@ -437,8 +437,8 @@ impl<T> BTreeSet<T> {
437437
}
438438
}
439439

440-
/// Visits the values representing the union,
441-
/// i.e., all the values in `self` or `other`, without duplicates,
440+
/// Visits the elements representing the union,
441+
/// i.e., all the elements in `self` or `other`, without duplicates,
442442
/// in ascending order.
443443
///
444444
/// # Examples
@@ -463,7 +463,7 @@ impl<T> BTreeSet<T> {
463463
Union(MergeIterInner::new(self.iter(), other.iter()))
464464
}
465465

466-
/// Clears the set, removing all values.
466+
/// Clears the set, removing all elements.
467467
///
468468
/// # Examples
469469
///
@@ -480,11 +480,11 @@ impl<T> BTreeSet<T> {
480480
self.map.clear()
481481
}
482482

483-
/// Returns `true` if the set contains a value.
483+
/// Returns `true` if the set contains an element equal to the value.
484484
///
485-
/// The value may be any borrowed form of the set's value type,
485+
/// The value may be any borrowed form of the set's element type,
486486
/// but the ordering on the borrowed form *must* match the
487-
/// ordering on the value type.
487+
/// ordering on the element type.
488488
///
489489
/// # Examples
490490
///
@@ -504,11 +504,12 @@ impl<T> BTreeSet<T> {
504504
self.map.contains_key(value)
505505
}
506506

507-
/// Returns a reference to the value in the set, if any, that is equal to the given value.
507+
/// Returns a reference to the element in the set, if any, that is equal to
508+
/// the value.
508509
///
509-
/// The value may be any borrowed form of the set's value type,
510+
/// The value may be any borrowed form of the set's element type,
510511
/// but the ordering on the borrowed form *must* match the
511-
/// ordering on the value type.
512+
/// ordering on the element type.
512513
///
513514
/// # Examples
514515
///
@@ -555,7 +556,7 @@ impl<T> BTreeSet<T> {
555556
}
556557

557558
/// Returns `true` if the set is a subset of another,
558-
/// i.e., `other` contains at least all the values in `self`.
559+
/// i.e., `other` contains at least all the elements in `self`.
559560
///
560561
/// # Examples
561562
///
@@ -632,7 +633,7 @@ impl<T> BTreeSet<T> {
632633
}
633634

634635
/// Returns `true` if the set is a superset of another,
635-
/// i.e., `self` contains at least all the values in `other`.
636+
/// i.e., `self` contains at least all the elements in `other`.
636637
///
637638
/// # Examples
638639
///
@@ -660,8 +661,8 @@ impl<T> BTreeSet<T> {
660661
other.is_subset(self)
661662
}
662663

663-
/// Returns a reference to the first value in the set, if any.
664-
/// This value is always the minimum of all values in the set.
664+
/// Returns a reference to the first element in the set, if any.
665+
/// This element is always the minimum of all elements in the set.
665666
///
666667
/// # Examples
667668
///
@@ -687,8 +688,8 @@ impl<T> BTreeSet<T> {
687688
self.map.first_key_value().map(|(k, _)| k)
688689
}
689690

690-
/// Returns a reference to the last value in the set, if any.
691-
/// This value is always the maximum of all values in the set.
691+
/// Returns a reference to the last element in the set, if any.
692+
/// This element is always the maximum of all elements in the set.
692693
///
693694
/// # Examples
694695
///
@@ -714,8 +715,8 @@ impl<T> BTreeSet<T> {
714715
self.map.last_key_value().map(|(k, _)| k)
715716
}
716717

717-
/// Removes the first value from the set and returns it, if any.
718-
/// The first value is always the minimum value in the set.
718+
/// Removes the first element from the set and returns it, if any.
719+
/// The first element is always the minimum element in the set.
719720
///
720721
/// # Examples
721722
///
@@ -739,8 +740,8 @@ impl<T> BTreeSet<T> {
739740
self.map.pop_first().map(|kv| kv.0)
740741
}
741742

742-
/// Removes the last value from the set and returns it, if any.
743-
/// The last value is always the maximum value in the set.
743+
/// Removes the last element from the set and returns it, if any.
744+
/// The last element is always the maximum element in the set.
744745
///
745746
/// # Examples
746747
///
@@ -766,10 +767,10 @@ impl<T> BTreeSet<T> {
766767

767768
/// Adds a value to the set.
768769
///
769-
/// If the set did not have this value present, `true` is returned.
770+
/// If the set did not have an equal element present, `true` is returned.
770771
///
771-
/// If the set did have this value present, `false` is returned, and the
772-
/// entry is not updated. See the [module-level documentation] for more.
772+
/// If the set did have an equal element present, `false` is returned, and
773+
/// the entry is not updated. See the [module-level documentation] for more.
773774
///
774775
/// [module-level documentation]: index.html#insert-and-complex-keys
775776
///
@@ -792,8 +793,8 @@ impl<T> BTreeSet<T> {
792793
self.map.insert(value, ()).is_none()
793794
}
794795

795-
/// Adds a value to the set, replacing the existing value, if any, that is equal to the given
796-
/// one. Returns the replaced value.
796+
/// Adds a value to the set, replacing the existing element, if any, that is
797+
/// equal to the value. Returns the replaced element.
797798
///
798799
/// # Examples
799800
///
@@ -815,12 +816,12 @@ impl<T> BTreeSet<T> {
815816
Recover::replace(&mut self.map, value)
816817
}
817818

818-
/// Removes a value from the set. Returns whether the value was
819-
/// present in the set.
819+
/// If the set contains an element equal to the value, removes it from the
820+
/// set and drops it. Returns whether such an element was present.
820821
///
821-
/// The value may be any borrowed form of the set's value type,
822+
/// The value may be any borrowed form of the set's element type,
822823
/// but the ordering on the borrowed form *must* match the
823-
/// ordering on the value type.
824+
/// ordering on the element type.
824825
///
825826
/// # Examples
826827
///
@@ -842,11 +843,12 @@ impl<T> BTreeSet<T> {
842843
self.map.remove(value).is_some()
843844
}
844845

845-
/// Removes and returns the value in the set, if any, that is equal to the given one.
846+
/// Removes and returns the element in the set, if any, that is equal to
847+
/// the value.
846848
///
847-
/// The value may be any borrowed form of the set's value type,
849+
/// The value may be any borrowed form of the set's element type,
848850
/// but the ordering on the borrowed form *must* match the
849-
/// ordering on the value type.
851+
/// ordering on the element type.
850852
///
851853
/// # Examples
852854
///
@@ -926,8 +928,8 @@ impl<T> BTreeSet<T> {
926928
self.map.append(&mut other.map);
927929
}
928930

929-
/// Splits the collection into two at the given value. Returns everything after the given value,
930-
/// including the value.
931+
/// Splits the collection into two at the value. Returns a new collection
932+
/// with all elements greater than or equal to the value.
931933
///
932934
/// # Examples
933935
///
@@ -963,20 +965,20 @@ impl<T> BTreeSet<T> {
963965
BTreeSet { map: self.map.split_off(value) }
964966
}
965967

966-
/// Creates an iterator that visits all values in ascending order and uses a closure
967-
/// to determine if a value should be removed.
968+
/// Creates an iterator that visits all elements in ascending order and
969+
/// uses a closure to determine if an element should be removed.
968970
///
969-
/// If the closure returns `true`, the value is removed from the set and yielded. If
970-
/// the closure returns `false`, or panics, the value remains in the set and will
971-
/// not be yielded.
971+
/// If the closure returns `true`, the element is removed from the set and
972+
/// yielded. If the closure returns `false`, or panics, the element remains
973+
/// in the set and will not be yielded.
972974
///
973-
/// If the iterator is only partially consumed or not consumed at all, each of the
974-
/// remaining values is still subjected to the closure and removed and dropped if it
975-
/// returns `true`.
975+
/// If the iterator is only partially consumed or not consumed at all, each
976+
/// of the remaining elements is still subjected to the closure and removed
977+
/// and dropped if it returns `true`.
976978
///
977-
/// It is unspecified how many more values will be subjected to the closure if a
978-
/// panic occurs in the closure, or if a panic occurs while dropping a value, or if
979-
/// the `DrainFilter` itself is leaked.
979+
/// It is unspecified how many more elements will be subjected to the
980+
/// closure if a panic occurs in the closure, or if a panic occurs while
981+
/// dropping an element, or if the `DrainFilter` itself is leaked.
980982
///
981983
/// # Examples
982984
///
@@ -1001,7 +1003,8 @@ impl<T> BTreeSet<T> {
10011003
DrainFilter { pred, inner: self.map.drain_filter_inner() }
10021004
}
10031005

1004-
/// Gets an iterator that visits the values in the `BTreeSet` in ascending order.
1006+
/// Gets an iterator that visits the elements in the `BTreeSet` in ascending
1007+
/// order.
10051008
///
10061009
/// # Examples
10071010
///

library/alloc/src/collections/btree/testing/crash_test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// We avoid relying on anything else in the crate, apart from the `Debug` trait.
12
use crate::fmt::Debug;
23
use std::cmp::Ordering;
34
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
@@ -7,8 +8,7 @@ use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
78
/// Events are `clone`, `drop` or some anonymous `query`.
89
///
910
/// Crash test dummies are identified and ordered by an id, so they can be used
10-
/// as keys in a BTreeMap. The implementation intentionally uses does not rely
11-
/// on anything defined in the crate, apart from the `Debug` trait.
11+
/// as keys in a BTreeMap.
1212
#[derive(Debug)]
1313
pub struct CrashTestDummy {
1414
pub id: usize,

0 commit comments

Comments
 (0)