Skip to content

Commit

Permalink
Merge commit 'refs/pull/20/head' of https://github.com/eggyal/copse
Browse files Browse the repository at this point in the history
  • Loading branch information
eggyal committed Apr 3, 2023
2 parents fa29add + 9f039e4 commit 849b228
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 8 deletions.
15 changes: 14 additions & 1 deletion src/liballoc/collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,6 @@ impl<T: SortableByWithOrder<O>, O: TotalOrder> BinaryHeap<T, O> {
///
/// assert_eq!(heap.into_sorted_vec(), [-10, 2, 4])
/// ```
#[cfg(feature = "binary_heap_retain")]
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&T) -> bool,
Expand Down Expand Up @@ -1479,6 +1478,20 @@ cfg_if! {

impl<T> FusedIterator for IntoIter<T> {}

#[rustversion::since(1.69)]
impl<T> Default for IntoIter<T> {
/// Creates an empty `binary_heap::IntoIter`.
///
/// ```
/// # use copse::binary_heap;
/// let iter: binary_heap::IntoIter<u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IntoIter { iter: Default::default() }
}
}

// In addition to the SAFETY invariants of the following three unsafe traits
// also refer to the vec::in_place_collect module documentation to get an overview
#[cfg(feature = "inplace_iteration")]
Expand Down
113 changes: 113 additions & 0 deletions src/liballoc/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,19 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> {
}
}

impl<'a, K: 'a, V: 'a> Default for Iter<'a, K, V> {
/// Creates an empty `btree_map::Iter`.
///
/// ```
/// # use copse::btree_map;
/// let iter: btree_map::Iter<'_, u8, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
Iter { range: Default::default(), length: 0 }
}
}

/// A mutable iterator over the entries of a `BTreeMap`.
///
/// This `struct` is created by the [`iter_mut`] method on [`BTreeMap`]. See its
Expand All @@ -404,6 +417,19 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
}
}

impl<'a, K: 'a, V: 'a> Default for IterMut<'a, K, V> {
/// Creates an empty `btree_map::IterMut`.
///
/// ```
/// # use copse::btree_map;
/// let iter: btree_map::IterMut<'_, u8, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IterMut { range: Default::default(), length: 0, _marker: PhantomData {} }
}
}

/// An owning iterator over the entries of a `BTreeMap`.
///
/// This `struct` is created by the [`into_iter`] method on [`BTreeMap`]
Expand Down Expand Up @@ -433,6 +459,22 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for IntoIter<K, V, A> {
}
}

impl<K, V, A> Default for IntoIter<K, V, A>
where
A: Allocator + Default + Clone,
{
/// Creates an empty `btree_map::IntoIter`.
///
/// ```
/// # use copse::btree_map;
/// let iter: btree_map::IntoIter<u8, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IntoIter { range: Default::default(), length: 0, alloc: Default::default() }
}
}

/// An iterator over the keys of a `BTreeMap`.
///
/// This `struct` is created by the [`keys`] method on [`BTreeMap`]. See its
Expand Down Expand Up @@ -1861,6 +1903,19 @@ impl<K, V> Clone for Keys<'_, K, V> {
}
}

impl<K, V> Default for Keys<'_, K, V> {
/// Creates an empty `btree_map::Keys`.
///
/// ```
/// # use copse::btree_map;
/// let iter: btree_map::Keys<'_, u8, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
Keys { inner: Default::default() }
}
}

impl<'a, K, V> Iterator for Values<'a, K, V> {
type Item = &'a V;

Expand Down Expand Up @@ -1897,6 +1952,19 @@ impl<K, V> Clone for Values<'_, K, V> {
}
}

impl<K, V> Default for Values<'_, K, V> {
/// Creates an empty `btree_map::Values`.
///
/// ```
/// # use copse::btree_map;
/// let iter: btree_map::Values<'_, u8, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
Values { inner: Default::default() }
}
}

decorate_if! {
/// An iterator produced by calling `drain_filter` on BTreeMap.
if #[cfg(feature = "btree_drain_filter")] { pub }
Expand Down Expand Up @@ -2025,6 +2093,19 @@ impl<'a, K, V> Iterator for Range<'a, K, V> {
}
}

impl<K, V> Default for Range<'_, K, V> {
/// Creates an empty `btree_map::Range`.
///
/// ```
/// # use copse::btree_map;
/// let iter: btree_map::Range<'_, u8, u8> = Default::default();
/// assert_eq!(iter.count(), 0);
/// ```
fn default() -> Self {
Range { inner: Default::default() }
}
}

impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
type Item = &'a mut V;

Expand Down Expand Up @@ -2093,6 +2174,22 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoKeys<K, V, A> {

impl<K, V, A: Allocator + Clone> FusedIterator for IntoKeys<K, V, A> {}

impl<K, V, A> Default for IntoKeys<K, V, A>
where
A: Allocator + Default + Clone,
{
/// Creates an empty `btree_map::IntoKeys`.
///
/// ```
/// # use copse::btree_map;
/// let iter: btree_map::IntoKeys<u8, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IntoKeys { inner: Default::default() }
}
}

impl<K, V, A: Allocator + Clone> Iterator for IntoValues<K, V, A> {
type Item = V;

Expand Down Expand Up @@ -2123,6 +2220,22 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoValues<K, V, A> {

impl<K, V, A: Allocator + Clone> FusedIterator for IntoValues<K, V, A> {}

impl<K, V, A> Default for IntoValues<K, V, A>
where
A: Allocator + Default + Clone,
{
/// Creates an empty `btree_map::IntoValues`.
///
/// ```
/// # use copse::btree_map;
/// let iter: btree_map::IntoValues<u8, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IntoValues { inner: Default::default() }
}
}

impl<'a, K, V> DoubleEndedIterator for Range<'a, K, V> {
fn next_back(&mut self) -> Option<(&'a K, &'a V)> {
self.inner.next_back_checked()
Expand Down
1 change: 0 additions & 1 deletion src/liballoc/collections/btree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub mod set;
mod set_val;
mod split;

#[doc(hidden)]
trait Recover<Q: ?Sized> {
type Key;

Expand Down
12 changes: 12 additions & 0 deletions src/liballoc/collections/btree/navigate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ impl<'a, K: 'a, V: 'a> Clone for LeafRange<marker::Immut<'a>, K, V> {
}
}

impl<B, K, V> Default for LeafRange<B, K, V> {
fn default() -> Self {
LeafRange { front: None, back: None }
}
}

impl<BorrowType, K, V> LeafRange<BorrowType, K, V> {
pub fn none() -> Self {
LeafRange { front: None, back: None }
Expand Down Expand Up @@ -124,6 +130,12 @@ pub struct LazyLeafRange<BorrowType, K, V> {
back: Option<LazyLeafHandle<BorrowType, K, V>>,
}

impl<B, K, V> Default for LazyLeafRange<B, K, V> {
fn default() -> Self {
LazyLeafRange { front: None, back: None }
}
}

impl<'a, K: 'a, V: 'a> Clone for LazyLeafRange<marker::Immut<'a>, K, V> {
fn clone(&self) -> Self {
LazyLeafRange { front: self.front.clone(), back: self.back.clone() }
Expand Down
48 changes: 43 additions & 5 deletions src/liballoc/collections/btree/set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// This is pretty much entirely stolen from TreeSet, since BTreeMap has an identical interface
// to TreeMap

use crate::{
default::{OrdStoredKey, OrdTotalOrder},
SortableBy, SortableByWithOrder, TotalOrder,
Expand All @@ -21,8 +18,6 @@ use super::Recover;

use crate::polyfill::*;

// FIXME(conventions): implement bounded iterators

/// An ordered set based on a B-Tree.
///
/// See [`BTreeMap`]'s documentation for a detailed discussion of this collection's performance
Expand Down Expand Up @@ -1598,6 +1593,20 @@ impl<T, A: Allocator + Clone> Iterator for IntoIter<T, A> {
self.iter.size_hint()
}
}

impl<T> Default for Iter<'_, T> {
/// Creates an empty `btree_set::Iter`.
///
/// ```
/// # use copse::btree_set;
/// let iter: btree_set::Iter<'_, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
Iter { iter: Default::default() }
}
}

impl<T, A: Allocator + Clone> DoubleEndedIterator for IntoIter<T, A> {
fn next_back(&mut self) -> Option<T> {
self.iter.next_back().map(|(k, _)| k)
Expand All @@ -1611,6 +1620,22 @@ impl<T, A: Allocator + Clone> ExactSizeIterator for IntoIter<T, A> {

impl<T, A: Allocator + Clone> FusedIterator for IntoIter<T, A> {}

impl<T, A> Default for IntoIter<T, A>
where
A: Allocator + Default + Clone,
{
/// Creates an empty `btree_set::IntoIter`.
///
/// ```
/// # use copse::btree_set;
/// let iter: btree_set::IntoIter<u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IntoIter { iter: Default::default() }
}
}

impl<T> Clone for Range<'_, T> {
fn clone(&self) -> Self {
Range { iter: self.iter.clone() }
Expand Down Expand Up @@ -1645,6 +1670,19 @@ impl<'a, T> DoubleEndedIterator for Range<'a, T> {

impl<T> FusedIterator for Range<'_, T> {}

impl<T> Default for Range<'_, T> {
/// Creates an empty `btree_set::Range`.
///
/// ```
/// # use copse::btree_set;
/// let iter: btree_set::Range<'_, u8> = Default::default();
/// assert_eq!(iter.count(), 0);
/// ```
fn default() -> Self {
Range { iter: Default::default() }
}
}

impl<T, O, A: Allocator + Clone> Clone for Difference<'_, T, O, A> {
fn clone(&self) -> Self {
Difference {
Expand Down
2 changes: 1 addition & 1 deletion src/polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod definitions {
}
}

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Default)]
pub struct Global;

impl Allocator for Global {}
Expand Down

0 comments on commit 849b228

Please sign in to comment.