Skip to content

Commit c8e53ed

Browse files
authored
ctutils: group alloc-dependent impls under alloc submodules (#1393)
Gets rid of repetitive `#[cfg(feature = "alloc")]` gating by putting all of the gated functionality for a given module under a `mod alloc` submodule.
1 parent 947ecd0 commit c8e53ed

File tree

5 files changed

+183
-183
lines changed

5 files changed

+183
-183
lines changed

ctutils/src/traits/ct_assign.rs

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ use core::{
88
},
99
};
1010

11-
#[cfg(feature = "alloc")]
12-
use alloc::{boxed::Box, vec::Vec};
13-
1411
#[cfg(doc)]
1512
use core::num::NonZero;
1613

@@ -136,82 +133,82 @@ where
136133

137134
impl<T, const N: usize> CtAssignSlice for [T; N] where [T]: CtAssign {}
138135

139-
#[cfg(feature = "alloc")]
140-
impl<T> CtAssign for Box<T>
141-
where
142-
T: CtAssign,
143-
{
136+
#[cfg(feature = "subtle")]
137+
impl CtAssign for subtle::Choice {
144138
#[inline]
145-
#[track_caller]
146139
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
147-
(**self).ct_assign(rhs, choice);
140+
*self = Self::ct_select(self, rhs, choice);
148141
}
149142
}
150143

151-
#[cfg(feature = "alloc")]
152-
impl<T> CtAssign for Box<[T]>
144+
#[cfg(feature = "subtle")]
145+
impl<T> CtAssign for subtle::CtOption<T>
153146
where
154-
[T]: CtAssign,
147+
T: Default + subtle::ConditionallySelectable,
155148
{
156149
#[inline]
157-
#[track_caller]
158150
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
159-
self.ct_assign(&**rhs, choice);
151+
use subtle::ConditionallySelectable as _;
152+
self.conditional_assign(rhs, choice.into());
160153
}
161154
}
162-
163155
#[cfg(feature = "alloc")]
164-
impl<T> CtAssign<[T]> for Box<[T]>
165-
where
166-
[T]: CtAssign,
167-
{
168-
#[inline]
169-
#[track_caller]
170-
fn ct_assign(&mut self, rhs: &[T], choice: Choice) {
171-
(**self).ct_assign(rhs, choice);
156+
mod alloc {
157+
use super::{Choice, CtAssign};
158+
use ::alloc::{boxed::Box, vec::Vec};
159+
160+
impl<T> CtAssign for Box<T>
161+
where
162+
T: CtAssign,
163+
{
164+
#[inline]
165+
#[track_caller]
166+
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
167+
(**self).ct_assign(rhs, choice);
168+
}
172169
}
173-
}
174170

175-
#[cfg(feature = "alloc")]
176-
impl<T> CtAssign for Vec<T>
177-
where
178-
[T]: CtAssign,
179-
{
180-
#[inline]
181-
#[track_caller]
182-
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
183-
self.ct_assign(rhs.as_slice(), choice);
171+
impl<T> CtAssign for Box<[T]>
172+
where
173+
[T]: CtAssign,
174+
{
175+
#[inline]
176+
#[track_caller]
177+
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
178+
self.ct_assign(&**rhs, choice);
179+
}
184180
}
185-
}
186181

187-
#[cfg(feature = "alloc")]
188-
impl<T> CtAssign<[T]> for Vec<T>
189-
where
190-
[T]: CtAssign,
191-
{
192-
#[inline]
193-
#[track_caller]
194-
fn ct_assign(&mut self, rhs: &[T], choice: Choice) {
195-
self.as_mut_slice().ct_assign(rhs, choice);
182+
impl<T> CtAssign<[T]> for Box<[T]>
183+
where
184+
[T]: CtAssign,
185+
{
186+
#[inline]
187+
#[track_caller]
188+
fn ct_assign(&mut self, rhs: &[T], choice: Choice) {
189+
(**self).ct_assign(rhs, choice);
190+
}
196191
}
197-
}
198192

199-
#[cfg(feature = "subtle")]
200-
impl CtAssign for subtle::Choice {
201-
#[inline]
202-
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
203-
*self = Self::ct_select(self, rhs, choice);
193+
impl<T> CtAssign for Vec<T>
194+
where
195+
[T]: CtAssign,
196+
{
197+
#[inline]
198+
#[track_caller]
199+
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
200+
self.ct_assign(rhs.as_slice(), choice);
201+
}
204202
}
205-
}
206203

207-
#[cfg(feature = "subtle")]
208-
impl<T> CtAssign for subtle::CtOption<T>
209-
where
210-
T: Default + subtle::ConditionallySelectable,
211-
{
212-
#[inline]
213-
fn ct_assign(&mut self, rhs: &Self, choice: Choice) {
214-
use subtle::ConditionallySelectable as _;
215-
self.conditional_assign(rhs, choice.into());
204+
impl<T> CtAssign<[T]> for Vec<T>
205+
where
206+
[T]: CtAssign,
207+
{
208+
#[inline]
209+
#[track_caller]
210+
fn ct_assign(&mut self, rhs: &[T], choice: Choice) {
211+
self.as_mut_slice().ct_assign(rhs, choice);
212+
}
216213
}
217214
}

ctutils/src/traits/ct_eq.rs

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use core::{
1010

1111
#[cfg(feature = "subtle")]
1212
use crate::CtOption;
13-
#[cfg(feature = "alloc")]
14-
use alloc::{boxed::Box, vec::Vec};
1513

1614
/// Constant-time equality: like `(Partial)Eq` with [`Choice`] instead of [`bool`].
1715
///
@@ -164,82 +162,83 @@ where
164162

165163
impl<T, const N: usize> CtEqSlice for [T; N] where [T]: CtEq {}
166164

167-
#[cfg(feature = "alloc")]
168-
impl<T> CtEq for Box<T>
169-
where
170-
T: CtEq,
171-
{
165+
#[cfg(feature = "subtle")]
166+
impl CtEq for subtle::Choice {
172167
#[inline]
173-
#[track_caller]
174-
fn ct_eq(&self, rhs: &Self) -> Choice {
175-
(**self).ct_eq(rhs)
168+
fn ct_eq(&self, other: &Self) -> Choice {
169+
self.unwrap_u8().ct_eq(&other.unwrap_u8())
176170
}
177171
}
178172

179-
#[cfg(feature = "alloc")]
180-
impl<T> CtEq for Box<[T]>
173+
#[cfg(feature = "subtle")]
174+
impl<T> CtEq for subtle::CtOption<T>
181175
where
182-
[T]: CtEq,
176+
T: CtEq + Default + subtle::ConditionallySelectable,
183177
{
184178
#[inline]
185-
#[track_caller]
186-
fn ct_eq(&self, rhs: &Self) -> Choice {
187-
self.ct_eq(&**rhs)
179+
fn ct_eq(&self, other: &Self) -> Choice {
180+
CtOption::from(*self).ct_eq(&CtOption::from(*other))
188181
}
189182
}
190183

191184
#[cfg(feature = "alloc")]
192-
impl<T> CtEq<[T]> for Box<[T]>
193-
where
194-
[T]: CtEq,
195-
{
196-
#[inline]
197-
#[track_caller]
198-
fn ct_eq(&self, rhs: &[T]) -> Choice {
199-
(**self).ct_eq(rhs)
185+
mod alloc {
186+
use super::{Choice, CtEq};
187+
use ::alloc::{boxed::Box, vec::Vec};
188+
189+
impl<T> CtEq for Box<T>
190+
where
191+
T: CtEq,
192+
{
193+
#[inline]
194+
#[track_caller]
195+
fn ct_eq(&self, rhs: &Self) -> Choice {
196+
(**self).ct_eq(rhs)
197+
}
200198
}
201-
}
202199

203-
#[cfg(feature = "alloc")]
204-
impl<T> CtEq for Vec<T>
205-
where
206-
[T]: CtEq,
207-
{
208-
#[inline]
209-
#[track_caller]
210-
fn ct_eq(&self, rhs: &Self) -> Choice {
211-
self.ct_eq(rhs.as_slice())
200+
impl<T> CtEq for Box<[T]>
201+
where
202+
[T]: CtEq,
203+
{
204+
#[inline]
205+
#[track_caller]
206+
fn ct_eq(&self, rhs: &Self) -> Choice {
207+
self.ct_eq(&**rhs)
208+
}
212209
}
213-
}
214210

215-
#[cfg(feature = "alloc")]
216-
impl<T> CtEq<[T]> for Vec<T>
217-
where
218-
[T]: CtEq,
219-
{
220-
#[inline]
221-
#[track_caller]
222-
fn ct_eq(&self, rhs: &[T]) -> Choice {
223-
self.as_slice().ct_eq(rhs)
211+
impl<T> CtEq<[T]> for Box<[T]>
212+
where
213+
[T]: CtEq,
214+
{
215+
#[inline]
216+
#[track_caller]
217+
fn ct_eq(&self, rhs: &[T]) -> Choice {
218+
(**self).ct_eq(rhs)
219+
}
224220
}
225-
}
226221

227-
#[cfg(feature = "subtle")]
228-
impl CtEq for subtle::Choice {
229-
#[inline]
230-
fn ct_eq(&self, other: &Self) -> Choice {
231-
self.unwrap_u8().ct_eq(&other.unwrap_u8())
222+
impl<T> CtEq for Vec<T>
223+
where
224+
[T]: CtEq,
225+
{
226+
#[inline]
227+
#[track_caller]
228+
fn ct_eq(&self, rhs: &Self) -> Choice {
229+
self.ct_eq(rhs.as_slice())
230+
}
232231
}
233-
}
234232

235-
#[cfg(feature = "subtle")]
236-
impl<T> CtEq for subtle::CtOption<T>
237-
where
238-
T: CtEq + Default + subtle::ConditionallySelectable,
239-
{
240-
#[inline]
241-
fn ct_eq(&self, other: &Self) -> Choice {
242-
CtOption::from(*self).ct_eq(&CtOption::from(*other))
233+
impl<T> CtEq<[T]> for Vec<T>
234+
where
235+
[T]: CtEq,
236+
{
237+
#[inline]
238+
#[track_caller]
239+
fn ct_eq(&self, rhs: &[T]) -> Choice {
240+
self.as_slice().ct_eq(rhs)
241+
}
243242
}
244243
}
245244

ctutils/src/traits/ct_find.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use crate::{Choice, CtAssign, CtOption};
22

3-
#[cfg(feature = "alloc")]
4-
use alloc::{boxed::Box, vec::Vec};
5-
63
#[cfg(doc)]
74
use core::iter::Iterator;
85

@@ -54,30 +51,35 @@ where
5451
}
5552

5653
#[cfg(feature = "alloc")]
57-
impl<T> CtFind<T> for Box<[T]>
58-
where
59-
T: CtAssign + Default,
60-
{
61-
#[inline]
62-
fn ct_find<P>(&self, predicate: P) -> CtOption<T>
54+
mod alloc {
55+
use super::{Choice, CtAssign, CtFind, CtOption};
56+
use ::alloc::{boxed::Box, vec::Vec};
57+
58+
impl<T> CtFind<T> for Box<[T]>
6359
where
64-
P: Fn(&T) -> Choice,
60+
T: CtAssign + Default,
6561
{
66-
(**self).ct_find(predicate)
62+
#[inline]
63+
fn ct_find<P>(&self, predicate: P) -> CtOption<T>
64+
where
65+
P: Fn(&T) -> Choice,
66+
{
67+
(**self).ct_find(predicate)
68+
}
6769
}
68-
}
6970

70-
#[cfg(feature = "alloc")]
71-
impl<T> CtFind<T> for Vec<T>
72-
where
73-
T: CtAssign + Default,
74-
{
75-
#[inline]
76-
fn ct_find<P>(&self, predicate: P) -> CtOption<T>
71+
#[cfg(feature = "alloc")]
72+
impl<T> CtFind<T> for Vec<T>
7773
where
78-
P: Fn(&T) -> Choice,
74+
T: CtAssign + Default,
7975
{
80-
self.as_slice().ct_find(predicate)
76+
#[inline]
77+
fn ct_find<P>(&self, predicate: P) -> CtOption<T>
78+
where
79+
P: Fn(&T) -> Choice,
80+
{
81+
self.as_slice().ct_find(predicate)
82+
}
8183
}
8284
}
8385

0 commit comments

Comments
 (0)