use alloc::vec::Vec;
use alloc::boxed::Box;
use alloc::collections::{BTreeSet, BTreeMap, VecDeque};
use alloc::rc::Rc;
#[cfg(target_has_atomic = "ptr")]
use alloc::sync::Arc;
use alloc::string::String;
use super::WellBehavedCollection;
super::impl_collection!(Vec<T>, BTreeSet<T>, VecDeque<T>);
super::delegate_first_and_co!(<T> Vec<T>);
#[cfg(feature = "newer-rust")]
if_rust_version::if_rust_version! {
>= 1.66 {
super::delegate_first_shared_and_co!(<T: Ord> BTreeSet<T>);
}
}
super::delegate_first_and_co!(<T> VecDeque<T>);
impl crate::Collection for String {
type Item = char;
}
impl crate::Len for String {
fn len(&self) -> usize {
self.len()
}
}
unsafe impl<T: WellBehavedCollection + ?Sized> WellBehavedCollection for Box<T> {}
unsafe impl<T: WellBehavedCollection + ?Sized> WellBehavedCollection for Rc<T> {}
#[cfg(target_has_atomic = "ptr")]
unsafe impl<T: WellBehavedCollection + ?Sized> WellBehavedCollection for Arc<T> {}
unsafe impl<T> WellBehavedCollection for Vec<T> {}
unsafe impl<T> WellBehavedCollection for BTreeSet<T> {}
unsafe impl<T> WellBehavedCollection for VecDeque<T> {}
unsafe impl<K, V> WellBehavedCollection for BTreeMap<K, V> {}
macro_rules! impl_reserve {
($($ty:ident$(<$gen:ident>)?),*) => {
$(
impl$(<$gen>)? super::Reserve for $ty$(<$gen>)? {
fn reserve(&mut self, capacity: usize) {
self.reserve(capacity);
}
}
)*
}
}
pub(crate) use impl_reserve;
impl_reserve!(Vec<T>, VecDeque<T>, String);
impl<K, V> crate::Collection for BTreeMap<K, V> {
type Item = V;
}
impl<K, V> crate::Len for BTreeMap<K, V> {
fn len(&self) -> usize {
self.len()
}
}
impl<K: Ord, V> crate::First for BTreeMap<K, V> {
fn first(&self) -> Option<&Self::Item> {
self.values().next()
}
}
impl<K: Ord, V> crate::FirstMut for BTreeMap<K, V> {
fn first_mut(&mut self) -> Option<&mut Self::Item> {
self.values_mut().next()
}
}
impl<K: Ord, V> crate::Last for BTreeMap<K, V> {
fn last(&self) -> Option<&Self::Item> {
self.values().next_back()
}
}
impl<K: Ord, V> crate::LastMut for BTreeMap<K, V> {
fn last_mut(&mut self) -> Option<&mut Self::Item> {
self.values_mut().next_back()
}
}
impl<K: Ord, V> crate::NonEmpty<BTreeMap<K, V>> {
pub fn first_key_value(&self) -> (&K, &V) {
unsafe { crate::unwrap_opt::<BTreeMap<K, V>, _>(self.as_inner().iter().next()) }
}
pub fn first_key_value_mut(&mut self) -> (&K, &mut V) {
unsafe { crate::unwrap_opt::<BTreeMap<K, V>, _>(self.inner_mut().iter_mut().next()) }
}
#[cfg(feature = "newer-rust")]
if_rust_version::if_rust_version! {
>= 1.66 {
pub fn try_pop_first_key_value(&mut self) -> Option<(K, V)> {
if self.as_inner().len() >= 2 {
unsafe { self.inner_mut().pop_first() }
} else {
None
}
}
}
}
pub fn last_key_value(&self) -> (&K, &V) {
unsafe { crate::unwrap_opt::<BTreeMap<K, V>, _>(self.as_inner().iter().next_back()) }
}
pub fn last_key_value_mut(&mut self) -> (&K, &mut V) {
unsafe { crate::unwrap_opt::<BTreeMap<K, V>, _>(self.inner_mut().iter_mut().next_back()) }
}
#[cfg(feature = "newer-rust")]
if_rust_version::if_rust_version! {
>= 1.66 {
pub fn try_pop_last_key_value(&mut self) -> Option<(K, V)> {
if self.as_inner().len() >= 2 {
unsafe { self.inner_mut().pop_last() }
} else {
None
}
}
}
}
}
impl<T> crate::PopFirst for Vec<T> {
fn pop_first(&mut self) -> Option<Self::Item> {
if self.is_empty() {
None
} else {
Some(self.remove(0))
}
}
}
impl<T> crate::PopLast for Vec<T> {
fn pop_last(&mut self) -> Option<Self::Item> {
self.pop()
}
}
#[cfg(feature = "newer-rust")]
if_rust_version::if_rust_version! {
>= 1.66 {
impl<T: Ord> crate::PopFirst for BTreeSet<T> {
fn pop_first(&mut self) -> Option<Self::Item> {
self.pop_first()
}
}
impl<T: Ord> crate::PopLast for BTreeSet<T> {
fn pop_last(&mut self) -> Option<Self::Item> {
self.pop_last()
}
}
impl<K: Ord, V> crate::PopFirst for BTreeMap<K, V> {
fn pop_first(&mut self) -> Option<Self::Item> {
self.pop_first().map(|value| value.1)
}
}
impl<K: Ord, V> crate::PopLast for BTreeMap<K, V> {
fn pop_last(&mut self) -> Option<Self::Item> {
self.pop_last().map(|value| value.1)
}
}
}
}
impl<T> crate::PopFirst for VecDeque<T> {
fn pop_first(&mut self) -> Option<Self::Item> {
self.pop_front()
}
}
impl<T> crate::PopLast for VecDeque<T> {
fn pop_last(&mut self) -> Option<Self::Item> {
self.pop_back()
}
}
impl crate::PopFirst for String {
fn pop_first(&mut self) -> Option<Self::Item> {
if self.is_empty() {
None
} else {
Some(self.remove(0))
}
}
}
impl crate::PopLast for String {
fn pop_last(&mut self) -> Option<Self::Item> {
self.pop()
}
}
unsafe impl<const N: usize, T> crate::traits::FixedLenCollection for Box<[T; N]> {}
unsafe impl<const N: usize, T> crate::traits::FixedLenCollection for Rc<[T; N]> {}
#[cfg(target_has_atomic = "ptr")]
unsafe impl<const N: usize, T> crate::traits::FixedLenCollection for Arc<[T; N]> {}