cheap_clone/
lib.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
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
// `CheapClone` trait is inspired by https://github.com/graphprotocol/graph-node/blob/master/graph/src/cheap_clone.rs

//! A trait which indicates that such type can be cloned cheaply.
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, allow(unused_attributes))]
#![deny(missing_docs)]
#![forbid(unsafe_code)]

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

macro_rules! impl_cheap_clone_for_copy {
  ($($ty: ty), +$(,)?) => {
    $(
      impl crate::CheapClone for $ty {
        fn cheap_clone(&self) -> Self {
          *self
        }
      }
    )*
  };
}

/// Things that are fast to clone in the context of an application.
///
/// The purpose of this API is to reduce the number of calls to .clone() which need to
/// be audited for performance.
///
/// As a rule of thumb, only constant-time `Clone` impls should also implement CheapClone.
/// Eg:
/// - ✔ [`Arc<T>`](alloc::sync::Arc)
/// - ✔ [`Rc<T>`](alloc::rc::Rc)
/// - ✔ [`Bytes`](bytes1::Bytes)
/// - ✗ [`Vec<T>`](alloc::vec::Vec)
/// - ✔ [`SmolStr`](smol_str03::SmolStr)
/// - ✔ [`FastStr`](faststr02::FastStr)
/// - ✗ [`String`]
pub trait CheapClone: Clone {
  /// Returns a copy of the value.
  fn cheap_clone(&self) -> Self {
    self.clone()
  }
}

#[cfg(feature = "bytes1")]
#[cfg_attr(docsrs, doc(cfg(feature = "bytes1")))]
impl CheapClone for bytes1::Bytes {}

#[cfg(feature = "smol_str03")]
#[cfg_attr(docsrs, doc(cfg(feature = "smol_str03")))]
impl CheapClone for smol_str03::SmolStr {}

#[cfg(feature = "smol_str02")]
#[cfg_attr(docsrs, doc(cfg(feature = "smol_str02")))]
impl CheapClone for smol_str02::SmolStr {}

#[cfg(feature = "faststr02")]
#[cfg_attr(docsrs, doc(cfg(feature = "faststr02")))]
impl CheapClone for faststr02::FastStr {}

#[cfg(feature = "triomphe01")]
#[cfg_attr(docsrs, doc(cfg(feature = "triomphe01")))]
impl<T> CheapClone for triomphe01::Arc<T> {}

#[cfg(feature = "alloc")]
mod a {
  use super::CheapClone;

  impl<T: ?Sized> CheapClone for alloc::rc::Rc<T> {}
  impl<T: ?Sized> CheapClone for alloc::sync::Arc<T> {}
}

#[cfg(feature = "std")]
mod s {
  use super::CheapClone;

  impl<T: CheapClone> CheapClone for std::pin::Pin<T> {}

  impl_cheap_clone_for_copy!(
    std::net::IpAddr,
    std::net::Ipv4Addr,
    std::net::Ipv6Addr,
    std::net::SocketAddr,
    std::net::SocketAddrV4,
    std::net::SocketAddrV6,
  );
}

impl<T: CheapClone> CheapClone for core::cmp::Reverse<T> {
  #[inline]
  fn cheap_clone(&self) -> Self {
    core::cmp::Reverse(self.0.cheap_clone())
  }
}
impl<T: CheapClone> CheapClone for Option<T> {
  #[inline]
  fn cheap_clone(&self) -> Self {
    self.as_ref().map(CheapClone::cheap_clone)
  }
}
impl<T: CheapClone, E: CheapClone> CheapClone for Result<T, E> {
  #[inline]
  fn cheap_clone(&self) -> Self {
    match self {
      Ok(ok) => Ok(ok.cheap_clone()),
      Err(err) => Err(err.cheap_clone()),
    }
  }
}
#[cfg(feature = "either")]
impl<L: CheapClone, R: CheapClone> CheapClone for either::Either<L, R> {
  #[inline]
  fn cheap_clone(&self) -> Self {
    match self {
      either::Either::Left(left) => either::Either::Left(left.cheap_clone()),
      either::Either::Right(right) => either::Either::Right(right.cheap_clone()),
    }
  }
}
#[cfg(feature = "among")]
impl<L: CheapClone, M: CheapClone, R: CheapClone> CheapClone for among::Among<L, M, R> {
  #[inline]
  fn cheap_clone(&self) -> Self {
    match self {
      among::Among::Left(left) => among::Among::Left(left.cheap_clone()),
      among::Among::Middle(middle) => among::Among::Middle(middle.cheap_clone()),
      among::Among::Right(right) => among::Among::Right(right.cheap_clone()),
    }
  }
}

impl_cheap_clone_for_copy! {
  (),
  bool, char, f32, f64, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize,
  core::num::NonZeroI8,
  core::num::NonZeroI16,
  core::num::NonZeroI32,
  core::num::NonZeroI64,
  core::num::NonZeroI128,
  core::num::NonZeroIsize,
  core::num::NonZeroU8,
  core::num::NonZeroU16,
  core::num::NonZeroU32,
  core::num::NonZeroU64,
  core::num::NonZeroU128,
  core::num::NonZeroUsize,
  &str
}

impl<T: Copy, const N: usize> CheapClone for [T; N] {
  fn cheap_clone(&self) -> Self {
    *self
  }
}

impl<T> CheapClone for &T {
  fn cheap_clone(&self) -> Self {
    self
  }
}

macro_rules! impl_cheap_clone_for_tuple {
  ($($param:literal),+ $(,)?) => {
    ::paste::paste! {
      impl<$([< T $param >]: CheapClone),+> CheapClone for ($([< T $param >],)+) {
        fn cheap_clone(&self) -> Self {
          ($(self.$param.cheap_clone(),)+)
        }
      }
    }
  };
}

impl_cheap_clone_for_tuple!(0);
impl_cheap_clone_for_tuple!(0, 1);
impl_cheap_clone_for_tuple!(0, 1, 2);
impl_cheap_clone_for_tuple!(0, 1, 2, 3);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18);
impl_cheap_clone_for_tuple!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19);
impl_cheap_clone_for_tuple!(
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
);
impl_cheap_clone_for_tuple!(
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
);
impl_cheap_clone_for_tuple!(
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22
);
impl_cheap_clone_for_tuple!(
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
);