-
Notifications
You must be signed in to change notification settings - Fork 19
/
listable.rs
83 lines (70 loc) · 2.01 KB
/
listable.rs
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
use crate::*;
use core::fmt;
pub trait Listable: Valuable {
fn size_hint(&self) -> (usize, Option<usize>);
}
impl<L: ?Sized + Listable> Listable for &L {
fn size_hint(&self) -> (usize, Option<usize>) {
L::size_hint(*self)
}
}
#[cfg(feature = "alloc")]
impl<L: ?Sized + Listable> Listable for alloc::boxed::Box<L> {
fn size_hint(&self) -> (usize, Option<usize>) {
L::size_hint(&**self)
}
}
#[cfg(feature = "alloc")]
impl<L: ?Sized + Listable> Listable for alloc::rc::Rc<L> {
fn size_hint(&self) -> (usize, Option<usize>) {
L::size_hint(&**self)
}
}
#[cfg(not(valuable_no_atomic_cas))]
#[cfg(feature = "alloc")]
impl<L: ?Sized + Listable> Listable for alloc::sync::Arc<L> {
fn size_hint(&self) -> (usize, Option<usize>) {
L::size_hint(&**self)
}
}
impl<T: Valuable> Listable for &'_ [T] {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
}
#[cfg(feature = "alloc")]
impl<T: Valuable> Listable for alloc::boxed::Box<[T]> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
}
impl<T: Valuable, const N: usize> Listable for [T; N] {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
}
#[cfg(feature = "alloc")]
impl<T: Valuable> Listable for alloc::vec::Vec<T> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
}
impl fmt::Debug for dyn Listable + '_ {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
struct DebugListable<'a, 'b> {
fmt: fmt::DebugList<'a, 'b>,
}
impl Visit for DebugListable<'_, '_> {
fn visit_slice(&mut self, slice: Slice<'_>) {
for value in &slice {
self.fmt.entry(&value);
}
}
}
let mut debug = DebugListable {
fmt: fmt.debug_list(),
};
self.visit(&mut debug);
debug.fmt.finish()
}
}