Skip to content

Commit 743cca5

Browse files
committed
Don't require proliferation of Rng: Clone in sample method
1 parent 37e5969 commit 743cca5

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

crates/templates/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ http.workspace = true
2525
minijinja-contrib.workspace = true
2626
minijinja.workspace = true
2727
rand.workspace = true
28+
rand_chacha.workspace = true
2829
serde_json.workspace = true
2930
serde_urlencoded.workspace = true
3031
serde.workspace = true

crates/templates/src/context.rs

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ use mas_iana::jose::JsonWebSignatureAlg;
3131
use mas_router::{Account, GraphQL, PostAuthAction, UrlBuilder};
3232
use oauth2_types::scope::{OPENID, Scope};
3333
use rand::{
34-
Rng,
34+
Rng, SeedableRng,
3535
distributions::{Alphanumeric, DistString},
3636
};
37+
use rand_chacha::ChaCha8Rng;
3738
use serde::{Deserialize, Serialize, ser::SerializeStruct};
3839
use ulid::Ulid;
3940
use url::Url;
@@ -106,7 +107,7 @@ pub trait TemplateContext: Serialize {
106107
///
107108
/// This is then used to check for template validity in unit tests and in
108109
/// the CLI (`cargo run -- templates check`)
109-
fn sample<R: Rng + Clone>(
110+
fn sample<R: Rng>(
110111
now: chrono::DateTime<Utc>,
111112
rng: &mut R,
112113
locales: &[DataLocale],
@@ -144,7 +145,7 @@ pub(crate) fn sample_list<T: TemplateContext>(samples: Vec<T>) -> BTreeMap<Sampl
144145
}
145146

146147
impl TemplateContext for () {
147-
fn sample<R: Rng + Clone>(
148+
fn sample<R: Rng>(
148149
_now: chrono::DateTime<Utc>,
149150
_rng: &mut R,
150151
_locales: &[DataLocale],
@@ -181,20 +182,20 @@ impl<T> std::ops::Deref for WithLanguage<T> {
181182
}
182183

183184
impl<T: TemplateContext> TemplateContext for WithLanguage<T> {
184-
fn sample<R: Rng + Clone>(
185+
fn sample<R: Rng>(
185186
now: chrono::DateTime<Utc>,
186187
rng: &mut R,
187188
locales: &[DataLocale],
188189
) -> BTreeMap<SampleIdentifier, Self>
189190
where
190191
Self: Sized,
191192
{
193+
// Create a forked RNG so we make samples deterministic between locales
194+
let rng = ChaCha8Rng::from_rng(rng).unwrap();
192195
locales
193196
.iter()
194197
.flat_map(|locale| {
195-
// Make samples deterministic between locales
196-
let mut rng = rng.clone();
197-
T::sample(now, &mut rng, locales)
198+
T::sample(now, &mut rng.clone(), locales)
198199
.into_iter()
199200
.map(|(sample_id, sample)| {
200201
(
@@ -220,7 +221,7 @@ pub struct WithCsrf<T> {
220221
}
221222

222223
impl<T: TemplateContext> TemplateContext for WithCsrf<T> {
223-
fn sample<R: Rng + Clone>(
224+
fn sample<R: Rng>(
224225
now: chrono::DateTime<Utc>,
225226
rng: &mut R,
226227
locales: &[DataLocale],
@@ -253,7 +254,7 @@ pub struct WithSession<T> {
253254
}
254255

255256
impl<T: TemplateContext> TemplateContext for WithSession<T> {
256-
fn sample<R: Rng + Clone>(
257+
fn sample<R: Rng>(
257258
now: chrono::DateTime<Utc>,
258259
rng: &mut R,
259260
locales: &[DataLocale],
@@ -291,7 +292,7 @@ pub struct WithOptionalSession<T> {
291292
}
292293

293294
impl<T: TemplateContext> TemplateContext for WithOptionalSession<T> {
294-
fn sample<R: Rng + Clone>(
295+
fn sample<R: Rng>(
295296
now: chrono::DateTime<Utc>,
296297
rng: &mut R,
297298
locales: &[DataLocale],
@@ -342,7 +343,7 @@ impl Serialize for EmptyContext {
342343
}
343344

344345
impl TemplateContext for EmptyContext {
345-
fn sample<R: Rng + Clone>(
346+
fn sample<R: Rng>(
346347
_now: chrono::DateTime<Utc>,
347348
_rng: &mut R,
348349
_locales: &[DataLocale],
@@ -370,7 +371,7 @@ impl IndexContext {
370371
}
371372

372373
impl TemplateContext for IndexContext {
373-
fn sample<R: Rng + Clone>(
374+
fn sample<R: Rng>(
374375
_now: chrono::DateTime<Utc>,
375376
_rng: &mut R,
376377
_locales: &[DataLocale],
@@ -416,7 +417,7 @@ impl AppContext {
416417
}
417418

418419
impl TemplateContext for AppContext {
419-
fn sample<R: Rng + Clone>(
420+
fn sample<R: Rng>(
420421
_now: chrono::DateTime<Utc>,
421422
_rng: &mut R,
422423
_locales: &[DataLocale],
@@ -449,7 +450,7 @@ impl ApiDocContext {
449450
}
450451

451452
impl TemplateContext for ApiDocContext {
452-
fn sample<R: Rng + Clone>(
453+
fn sample<R: Rng>(
453454
_now: chrono::DateTime<Utc>,
454455
_rng: &mut R,
455456
_locales: &[DataLocale],
@@ -541,7 +542,7 @@ pub struct LoginContext {
541542
}
542543

543544
impl TemplateContext for LoginContext {
544-
fn sample<R: Rng + Clone>(
545+
fn sample<R: Rng>(
545546
_now: chrono::DateTime<Utc>,
546547
_rng: &mut R,
547548
_locales: &[DataLocale],
@@ -649,7 +650,7 @@ pub struct RegisterContext {
649650
}
650651

651652
impl TemplateContext for RegisterContext {
652-
fn sample<R: Rng + Clone>(
653+
fn sample<R: Rng>(
653654
_now: chrono::DateTime<Utc>,
654655
_rng: &mut R,
655656
_locales: &[DataLocale],
@@ -692,7 +693,7 @@ pub struct PasswordRegisterContext {
692693
}
693694

694695
impl TemplateContext for PasswordRegisterContext {
695-
fn sample<R: Rng + Clone>(
696+
fn sample<R: Rng>(
696697
_now: chrono::DateTime<Utc>,
697698
_rng: &mut R,
698699
_locales: &[DataLocale],
@@ -734,7 +735,7 @@ pub struct ConsentContext {
734735
}
735736

736737
impl TemplateContext for ConsentContext {
737-
fn sample<R: Rng + Clone>(
738+
fn sample<R: Rng>(
738739
now: chrono::DateTime<Utc>,
739740
rng: &mut R,
740741
_locales: &[DataLocale],
@@ -792,7 +793,7 @@ pub struct PolicyViolationContext {
792793
}
793794

794795
impl TemplateContext for PolicyViolationContext {
795-
fn sample<R: Rng + Clone>(
796+
fn sample<R: Rng>(
796797
now: chrono::DateTime<Utc>,
797798
rng: &mut R,
798799
_locales: &[DataLocale],
@@ -867,7 +868,7 @@ pub struct CompatSsoContext {
867868
}
868869

869870
impl TemplateContext for CompatSsoContext {
870-
fn sample<R: Rng + Clone>(
871+
fn sample<R: Rng>(
871872
now: chrono::DateTime<Utc>,
872873
rng: &mut R,
873874
_locales: &[DataLocale],
@@ -929,7 +930,7 @@ impl EmailRecoveryContext {
929930
}
930931

931932
impl TemplateContext for EmailRecoveryContext {
932-
fn sample<R: Rng + Clone>(
933+
fn sample<R: Rng>(
933934
now: chrono::DateTime<Utc>,
934935
rng: &mut R,
935936
_locales: &[DataLocale],
@@ -994,7 +995,7 @@ impl EmailVerificationContext {
994995
}
995996

996997
impl TemplateContext for EmailVerificationContext {
997-
fn sample<R: Rng + Clone>(
998+
fn sample<R: Rng>(
998999
now: chrono::DateTime<Utc>,
9991000
rng: &mut R,
10001001
_locales: &[DataLocale],
@@ -1069,7 +1070,7 @@ impl RegisterStepsVerifyEmailContext {
10691070
}
10701071

10711072
impl TemplateContext for RegisterStepsVerifyEmailContext {
1072-
fn sample<R: Rng + Clone>(
1073+
fn sample<R: Rng>(
10731074
now: chrono::DateTime<Utc>,
10741075
rng: &mut R,
10751076
_locales: &[DataLocale],
@@ -1109,7 +1110,7 @@ impl RegisterStepsEmailInUseContext {
11091110
}
11101111

11111112
impl TemplateContext for RegisterStepsEmailInUseContext {
1112-
fn sample<R: Rng + Clone>(
1113+
fn sample<R: Rng>(
11131114
_now: chrono::DateTime<Utc>,
11141115
_rng: &mut R,
11151116
_locales: &[DataLocale],
@@ -1164,7 +1165,7 @@ impl RegisterStepsDisplayNameContext {
11641165
}
11651166

11661167
impl TemplateContext for RegisterStepsDisplayNameContext {
1167-
fn sample<R: Rng + Clone>(
1168+
fn sample<R: Rng>(
11681169
_now: chrono::DateTime<chrono::Utc>,
11691170
_rng: &mut R,
11701171
_locales: &[DataLocale],
@@ -1219,7 +1220,7 @@ impl RegisterStepsRegistrationTokenContext {
12191220
}
12201221

12211222
impl TemplateContext for RegisterStepsRegistrationTokenContext {
1222-
fn sample<R: Rng + Clone>(
1223+
fn sample<R: Rng>(
12231224
_now: chrono::DateTime<chrono::Utc>,
12241225
_rng: &mut R,
12251226
_locales: &[DataLocale],
@@ -1270,7 +1271,7 @@ impl RecoveryStartContext {
12701271
}
12711272

12721273
impl TemplateContext for RecoveryStartContext {
1273-
fn sample<R: Rng + Clone>(
1274+
fn sample<R: Rng>(
12741275
_now: chrono::DateTime<Utc>,
12751276
_rng: &mut R,
12761277
_locales: &[DataLocale],
@@ -1312,7 +1313,7 @@ impl RecoveryProgressContext {
13121313
}
13131314

13141315
impl TemplateContext for RecoveryProgressContext {
1315-
fn sample<R: Rng + Clone>(
1316+
fn sample<R: Rng>(
13161317
now: chrono::DateTime<Utc>,
13171318
rng: &mut R,
13181319
_locales: &[DataLocale],
@@ -1358,7 +1359,7 @@ impl RecoveryExpiredContext {
13581359
}
13591360

13601361
impl TemplateContext for RecoveryExpiredContext {
1361-
fn sample<R: Rng + Clone>(
1362+
fn sample<R: Rng>(
13621363
now: chrono::DateTime<Utc>,
13631364
rng: &mut R,
13641365
_locales: &[DataLocale],
@@ -1422,7 +1423,7 @@ impl RecoveryFinishContext {
14221423
}
14231424

14241425
impl TemplateContext for RecoveryFinishContext {
1425-
fn sample<R: Rng + Clone>(
1426+
fn sample<R: Rng>(
14261427
now: chrono::DateTime<Utc>,
14271428
rng: &mut R,
14281429
_locales: &[DataLocale],
@@ -1471,7 +1472,7 @@ impl UpstreamExistingLinkContext {
14711472
}
14721473

14731474
impl TemplateContext for UpstreamExistingLinkContext {
1474-
fn sample<R: Rng + Clone>(
1475+
fn sample<R: Rng>(
14751476
now: chrono::DateTime<Utc>,
14761477
rng: &mut R,
14771478
_locales: &[DataLocale],
@@ -1509,7 +1510,7 @@ impl UpstreamSuggestLink {
15091510
}
15101511

15111512
impl TemplateContext for UpstreamSuggestLink {
1512-
fn sample<R: Rng + Clone>(
1513+
fn sample<R: Rng>(
15131514
now: chrono::DateTime<Utc>,
15141515
rng: &mut R,
15151516
_locales: &[DataLocale],
@@ -1638,7 +1639,7 @@ impl UpstreamRegister {
16381639
}
16391640

16401641
impl TemplateContext for UpstreamRegister {
1641-
fn sample<R: Rng + Clone>(
1642+
fn sample<R: Rng>(
16421643
now: chrono::DateTime<Utc>,
16431644
_rng: &mut R,
16441645
_locales: &[DataLocale],
@@ -1724,7 +1725,7 @@ impl DeviceLinkContext {
17241725
}
17251726

17261727
impl TemplateContext for DeviceLinkContext {
1727-
fn sample<R: Rng + Clone>(
1728+
fn sample<R: Rng>(
17281729
_now: chrono::DateTime<Utc>,
17291730
_rng: &mut R,
17301731
_locales: &[DataLocale],
@@ -1758,7 +1759,7 @@ impl DeviceConsentContext {
17581759
}
17591760

17601761
impl TemplateContext for DeviceConsentContext {
1761-
fn sample<R: Rng + Clone>(
1762+
fn sample<R: Rng>(
17621763
now: chrono::DateTime<Utc>,
17631764
rng: &mut R,
17641765
_locales: &[DataLocale],
@@ -1803,7 +1804,7 @@ impl AccountInactiveContext {
18031804
}
18041805

18051806
impl TemplateContext for AccountInactiveContext {
1806-
fn sample<R: Rng + Clone>(
1807+
fn sample<R: Rng>(
18071808
now: chrono::DateTime<Utc>,
18081809
rng: &mut R,
18091810
_locales: &[DataLocale],
@@ -1839,7 +1840,7 @@ impl DeviceNameContext {
18391840
}
18401841

18411842
impl TemplateContext for DeviceNameContext {
1842-
fn sample<R: Rng + Clone>(
1843+
fn sample<R: Rng>(
18431844
now: chrono::DateTime<Utc>,
18441845
rng: &mut R,
18451846
_locales: &[DataLocale],
@@ -1865,7 +1866,7 @@ pub struct FormPostContext<T> {
18651866
}
18661867

18671868
impl<T: TemplateContext> TemplateContext for FormPostContext<T> {
1868-
fn sample<R: Rng + Clone>(
1869+
fn sample<R: Rng>(
18691870
now: chrono::DateTime<Utc>,
18701871
rng: &mut R,
18711872
locales: &[DataLocale],
@@ -1947,7 +1948,7 @@ impl std::fmt::Display for ErrorContext {
19471948
}
19481949

19491950
impl TemplateContext for ErrorContext {
1950-
fn sample<R: Rng + Clone>(
1951+
fn sample<R: Rng>(
19511952
_now: chrono::DateTime<Utc>,
19521953
_rng: &mut R,
19531954
_locales: &[DataLocale],
@@ -2041,7 +2042,7 @@ impl NotFoundContext {
20412042
}
20422043

20432044
impl TemplateContext for NotFoundContext {
2044-
fn sample<R: Rng + Clone>(
2045+
fn sample<R: Rng>(
20452046
_now: DateTime<Utc>,
20462047
_rng: &mut R,
20472048
_locales: &[DataLocale],

crates/templates/src/context/captcha.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl<T> WithCaptcha<T> {
5959
}
6060

6161
impl<T: TemplateContext> TemplateContext for WithCaptcha<T> {
62-
fn sample<R: Rng + Clone>(
62+
fn sample<R: Rng>(
6363
now: chrono::DateTime<chrono::prelude::Utc>,
6464
rng: &mut R,
6565
locales: &[DataLocale],

0 commit comments

Comments
 (0)