1
#![cfg_attr(docsrs, feature(doc_cfg))]
2
#![doc = include_str!("../README.md")]
3
// @@ begin lint list maintained by maint/add_warning @@
4
#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5
#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6
#![warn(missing_docs)]
7
#![warn(noop_method_call)]
8
#![warn(unreachable_pub)]
9
#![warn(clippy::all)]
10
#![deny(clippy::await_holding_lock)]
11
#![deny(clippy::cargo_common_metadata)]
12
#![deny(clippy::cast_lossless)]
13
#![deny(clippy::checked_conversions)]
14
#![warn(clippy::cognitive_complexity)]
15
#![deny(clippy::debug_assert_with_mut_call)]
16
#![deny(clippy::exhaustive_enums)]
17
#![deny(clippy::exhaustive_structs)]
18
#![deny(clippy::expl_impl_clone_on_copy)]
19
#![deny(clippy::fallible_impl_from)]
20
#![deny(clippy::implicit_clone)]
21
#![deny(clippy::large_stack_arrays)]
22
#![warn(clippy::manual_ok_or)]
23
#![deny(clippy::missing_docs_in_private_items)]
24
#![warn(clippy::needless_borrow)]
25
#![warn(clippy::needless_pass_by_value)]
26
#![warn(clippy::option_option)]
27
#![deny(clippy::print_stderr)]
28
#![deny(clippy::print_stdout)]
29
#![warn(clippy::rc_buffer)]
30
#![deny(clippy::ref_option_ref)]
31
#![warn(clippy::semicolon_if_nothing_returned)]
32
#![warn(clippy::trait_duplication_in_bounds)]
33
#![deny(clippy::unchecked_time_subtraction)]
34
#![deny(clippy::unnecessary_wraps)]
35
#![warn(clippy::unseparated_literal_suffix)]
36
#![deny(clippy::unwrap_used)]
37
#![deny(clippy::mod_module_files)]
38
#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39
#![allow(clippy::uninlined_format_args)]
40
#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41
#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42
#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43
#![allow(clippy::needless_lifetimes)] // See arti#1765
44
#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45
#![allow(clippy::collapsible_if)] // See arti#2342
46
#![deny(clippy::unused_async)]
47
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
48

            
49
use std::fmt;
50
use std::ops::{RangeInclusive, RangeToInclusive};
51
use std::path::Path;
52
use std::time::Duration;
53

            
54
pub mod error_sources;
55
pub mod intern;
56
pub mod iter;
57
pub mod n_key_list;
58
pub mod n_key_set;
59
pub mod rand_hostname;
60
pub mod rangebounds;
61
pub mod retry;
62
pub mod test_rng;
63

            
64
mod byte_qty;
65
pub use byte_qty::ByteQty;
66

            
67
pub use paste::paste;
68

            
69
use rand::Rng;
70

            
71
/// Sealed
72
mod sealed {
73
    /// Sealed
74
    pub trait Sealed {}
75
}
76
use sealed::Sealed;
77

            
78
// ----------------------------------------------------------------------
79

            
80
/// Function with the signature of `Debug::fmt` that just prints `".."`
81
///
82
/// ```
83
/// use educe::Educe;
84
/// use tor_basic_utils::skip_fmt;
85
///
86
/// #[derive(Educe, Default)]
87
/// #[educe(Debug)]
88
/// struct Wombat {
89
///     visible: usize,
90
///
91
///     #[educe(Debug(method = "skip_fmt"))]
92
///     invisible: [u8; 2],
93
/// }
94
///
95
/// assert_eq!( format!("{:?}", &Wombat::default()),
96
///             "Wombat { visible: 0, invisible: .. }" );
97
/// ```
98
15996
pub fn skip_fmt<T>(_: &T, f: &mut fmt::Formatter) -> fmt::Result {
99
    /// Inner function avoids code bloat due to generics
100
15996
    fn inner(f: &mut fmt::Formatter) -> fmt::Result {
101
15996
        write!(f, "..")
102
15996
    }
103
15996
    inner(f)
104
15996
}
105

            
106
// ----------------------------------------------------------------------
107

            
108
/// Formats an iterator as an object whose display implementation is a `separator`-separated string
109
/// of items from `iter`.
110
12
pub fn iter_join(
111
12
    separator: &str,
112
12
    iter: impl Iterator<Item: fmt::Display> + Clone,
113
12
) -> impl fmt::Display {
114
    // TODO MSRV 1.93: Replace with `std::fmt::from_fn()`?
115
    struct Fmt<'a, I: Iterator<Item: fmt::Display> + Clone> {
116
        /// Separates items in `iter`.
117
        separator: &'a str,
118
        /// Iterator to join.
119
        iter: I,
120
    }
121
    impl<'a, I: Iterator<Item: fmt::Display> + Clone> fmt::Display for Fmt<'a, I> {
122
12
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123
12
            let Self { separator, iter } = self;
124
12
            let mut iter = iter.clone();
125
12
            if let Some(first) = iter.next() {
126
12
                write!(f, "{first}")?;
127
            }
128
12
            for x in iter {
129
12
                write!(f, "{separator}{x}")?;
130
            }
131
12
            Ok(())
132
12
        }
133
    }
134
12
    Fmt { separator, iter }
135
12
}
136

            
137
// ----------------------------------------------------------------------
138

            
139
/// Extension trait to provide `.strip_suffix_ignore_ascii_case()` etc.
140
// Using `.as_ref()` as a supertrait lets us make the method a provided one.
141
pub trait StrExt: AsRef<str> {
142
    /// Like `str.strip_suffix()` but ASCII-case-insensitive
143
6654
    fn strip_suffix_ignore_ascii_case(&self, suffix: &str) -> Option<&str> {
144
6654
        let whole = self.as_ref();
145
6654
        let suffix_start = whole.len().checked_sub(suffix.len())?;
146
6585
        whole[suffix_start..]
147
6585
            .eq_ignore_ascii_case(suffix)
148
6585
            .then(|| &whole[..suffix_start])
149
6654
    }
150

            
151
    /// Like `str.ends_with()` but ASCII-case-insensitive
152
82
    fn ends_with_ignore_ascii_case(&self, suffix: &str) -> bool {
153
82
        self.strip_suffix_ignore_ascii_case(suffix).is_some()
154
82
    }
155
}
156
impl StrExt for str {}
157

            
158
// ----------------------------------------------------------------------
159

            
160
/// Extension trait to provide `.gen_range_checked()`
161
pub trait RngExt: Rng {
162
    /// Generate a random value in the given range.
163
    ///
164
    /// This function is optimised for the case that only a single sample is made from the given range. See also the [`Uniform`](rand::distr::uniform::Uniform)  distribution type which may be faster if sampling from the same range repeatedly.
165
    ///
166
    /// If the supplied range is empty, returns `None`.
167
    ///
168
    /// (This is a non-panicking version of [`rand::RngExt::random_range`].)
169
    ///
170
    /// ### Example
171
    ///
172
    /// ```
173
    /// use tor_basic_utils::RngExt as _;
174
    //
175
    // Fake plastic imitation tor_error, since that's actually higher up the stack
176
    /// # #[macro_use]
177
    /// # mod tor_error {
178
    /// #     #[derive(Debug)]
179
    /// #     pub struct Bug;
180
    /// #     pub fn internal() {} // makes `use` work
181
    /// # }
182
    /// # macro_rules! internal { { $x:expr } => { Bug } }
183
    //
184
    /// use tor_error::{Bug, internal};
185
    ///
186
    /// fn choose(slice: &[i32]) -> Result<i32, Bug> {
187
    ///     let index = rand::rng()
188
    ///         .gen_range_checked(0..slice.len())
189
    ///         .ok_or_else(|| internal!("empty slice"))?;
190
    ///     Ok(slice[index])
191
    /// }
192
    ///
193
    /// assert_eq!(choose(&[42]).unwrap(), 42);
194
    /// let _: Bug = choose(&[]).unwrap_err();
195
    /// ```
196
    //
197
    // TODO: We may someday wish to rename this function to random_range_checked,
198
    // since gen_range was renamed to random_range in rand 0.9.
199
    // Or we might decide to leave it alone.
200
442272
    fn gen_range_checked<T, R>(&mut self, range: R) -> Option<T>
201
442272
    where
202
442272
        T: rand::distr::uniform::SampleUniform,
203
442272
        R: rand::distr::uniform::SampleRange<T>,
204
    {
205
        #[allow(clippy::disallowed_methods)]
206
        {
207
            // Prove that rand::RngExt::random_range exists.  See arti.git/clippy.toml.
208
            let _ = |r: &mut rand::rngs::ThreadRng| rand::RngExt::random_range::<u8, _>(r, 0..10);
209
        }
210

            
211
442272
        if range.is_empty() {
212
            None
213
        } else {
214
            use rand::RngExt;
215
            #[allow(clippy::disallowed_methods)]
216
442272
            Some(self.random_range(range))
217
        }
218
442272
    }
219

            
220
    /// Generate a random value in the given upper-bounded-only range.
221
    ///
222
    /// For use with an inclusive upper-bounded-only range,
223
    /// with types that implement `GenRangeInfallible`
224
    /// (that necessarily then implement the appropriate `rand` traits).
225
    ///
226
    /// This function is optimised for the case that only a single sample is made from the given range. See also the [`Uniform`](rand::distr::uniform::Uniform)  distribution type which may be faster if sampling from the same range repeatedly.
227
    ///
228
    /// ### Example
229
    ///
230
    /// ```
231
    /// use std::time::Duration;
232
    /// use tor_basic_utils::RngExt as _;
233
    ///
234
    /// fn stochastic_sleep(max: Duration) {
235
    ///     let chosen_delay = rand::rng()
236
    ///         .gen_range_infallible(..=max);
237
    ///     std::thread::sleep(chosen_delay);
238
    /// }
239
    /// ```
240
313609
    fn gen_range_infallible<T>(&mut self, range: RangeToInclusive<T>) -> T
241
313609
    where
242
313609
        T: GenRangeInfallible,
243
    {
244
313609
        self.gen_range_checked(T::lower_bound()..=range.end)
245
313609
            .expect("GenRangeInfallible type with an empty lower_bound()..=T range")
246
313609
    }
247
}
248
impl<T: Rng> RngExt for T {}
249

            
250
/// Types that can be infallibly sampled using `gen_range_infallible`
251
///
252
/// In addition to the supertraits, the implementor of this trait must guarantee that:
253
///
254
/// `<Self as GenRangeInfallible>::lower_bound() ..= UPPER`
255
/// is a nonempty range for every value of `UPPER`.
256
//
257
// One might think that this trait is wrong because we might want to be able to
258
// implement gen_range_infallible for arguments other than RangeToInclusive<T>.
259
// However, double-ended ranges are inherently fallible because the actual values
260
// might be in the wrong order.  Non-inclusive ranges are fallible because the
261
// upper bound might be zero, unless a NonZero type is used, which seems like a further
262
// complication that we probably don't want to introduce here.  That leaves lower-bounded
263
// ranges, but those are very rare.
264
pub trait GenRangeInfallible: rand::distr::uniform::SampleUniform + Ord
265
where
266
    RangeInclusive<Self>: rand::distr::uniform::SampleRange<Self>,
267
{
268
    /// The usual lower bound, for converting a `RangeToInclusive` to a `RangeInclusive`
269
    ///
270
    /// Only makes sense with types with a sensible lower bound, such as zero.
271
    fn lower_bound() -> Self;
272
}
273

            
274
impl GenRangeInfallible for Duration {
275
426666
    fn lower_bound() -> Self {
276
426666
        Duration::ZERO
277
426666
    }
278
}
279

            
280
// ----------------------------------------------------------------------
281

            
282
/// Renaming of `Path::display` as `display_lossy`
283
pub trait PathExt: Sealed {
284
    /// Display this `Path` as an approximate string, for human consumption in messages
285
    ///
286
    /// Operating system paths cannot always be faithfully represented as Rust strings,
287
    /// because they might not be valid Unicode.
288
    ///
289
    /// This helper method provides a way to display a string for human users.
290
    /// **This may lose information** so should only be used for error messages etc.
291
    ///
292
    /// This method is exactly the same as [`std::path::Path::display`],
293
    /// but with a different and more discouraging name.
294
    fn display_lossy(&self) -> std::path::Display<'_>;
295
}
296
impl Sealed for Path {}
297
impl PathExt for Path {
298
    #[allow(clippy::disallowed_methods)]
299
2709
    fn display_lossy(&self) -> std::path::Display<'_> {
300
2709
        self.display()
301
2709
    }
302
}
303

            
304
// ----------------------------------------------------------------------
305

            
306
/// Define an "accessor trait", which describes structs that have fields of certain types
307
///
308
/// This can be useful if a large struct, living high up in the dependency graph,
309
/// contains fields that lower-lever crates want to be able to use without having
310
/// to copy the data about etc.
311
///
312
/// ```
313
/// // imagine this in the lower-level module
314
/// pub trait Supertrait {}
315
/// use tor_basic_utils::define_accessor_trait;
316
/// define_accessor_trait! {
317
///     pub trait View: Supertrait {
318
///         lorem: String,
319
///         ipsum: usize,
320
///         +
321
///         fn other_accessor(&self) -> bool;
322
///         // any other trait items can go here
323
///    }
324
/// }
325
///
326
/// fn test_view<V: View>(v: &V) {
327
///     assert_eq!(v.lorem(), "sit");
328
///     assert_eq!(v.ipsum(), &42);
329
/// }
330
///
331
/// // imagine this in the higher-level module
332
/// use derive_more::AsRef;
333
/// #[derive(AsRef)]
334
/// struct Everything {
335
///     #[as_ref] lorem: String,
336
///     #[as_ref] ipsum: usize,
337
///     dolor: Vec<()>,
338
/// }
339
/// impl Supertrait for Everything { }
340
/// impl View for Everything {
341
///     fn other_accessor(&self) -> bool { false }
342
/// }
343
///
344
/// let everything = Everything {
345
///     lorem: "sit".into(),
346
///     ipsum: 42,
347
///     dolor: vec![()],
348
/// };
349
///
350
/// test_view(&everything);
351
/// ```
352
///
353
/// ### Generated code
354
///
355
/// ```
356
/// # pub trait Supertrait { }
357
/// pub trait View: AsRef<String> + AsRef<usize> + Supertrait {
358
///     fn lorem(&self) -> &String { self.as_ref() }
359
///     fn ipsum(&self) -> &usize { self.as_ref() }
360
/// }
361
/// ```
362
#[macro_export]
363
macro_rules! define_accessor_trait {
364
    {
365
        $( #[ $attr:meta ])*
366
        $vis:vis trait $Trait:ident $( : $( $Super:path )* )? {
367
            $( $accessor:ident: $type:ty, )*
368
            $( + $( $rest:tt )* )?
369
        }
370
    } => {
371
        $( #[ $attr ])*
372
        $vis trait $Trait: $( core::convert::AsRef<$type> + )* $( $( $Super + )* )?
373
        {
374
            $(
375
                /// Access the field
376
848
                fn $accessor(&self) -> &$type { core::convert::AsRef::as_ref(self) }
377
            )*
378
            $(
379
                $( $rest )*
380
            )?
381
        }
382
    }
383
}
384

            
385
// ----------------------------------------------------------------------
386

            
387
/// Helper for assisting with macro "argument" defaulting
388
///
389
/// ```ignore
390
/// macro_coalesce_args!{ [ something ]  ... }  // =>   something
391
/// macro_coalesce_args!{ [ ], [ other ] ... }  // =>   other
392
/// // etc.
393
/// ```
394
///
395
/// ### Usage note
396
///
397
/// It is generally possible to avoid use of `macro_coalesce_args`, at the cost of
398
/// providing many alternative matcher patterns.  Using `macro_coalesce_args` can make
399
/// it possible to provide a single pattern with the optional items in `$( )?`.
400
///
401
/// This is valuable because a single pattern with some optional items
402
/// makes much better documentation than several patterns which the reader must compare
403
/// by eye - and it also simplifies the implementation.
404
///
405
/// `macro_coalesce_args` takes each of its possible expansions in `[ ]` and returns
406
/// the first nonempty one.
407
#[macro_export]
408
macro_rules! macro_first_nonempty {
409
    { [ $($yes:tt)+ ] $($rhs:tt)* } => { $($yes)* };
410
    { [ ]$(,)? [ $($otherwise:tt)* ] $($rhs:tt)* } => {
411
        $crate::macro_first_nonempty!{ [ $($otherwise)* ] $($rhs)* }
412
    };
413
}
414

            
415
// ----------------------------------------------------------------------
416

            
417
/// Define `Debug` to print as hex
418
///
419
/// # Usage
420
///
421
/// ```ignore
422
/// impl_debug_hex! { $type }
423
/// impl_debug_hex! { $type . $field_accessor }
424
/// impl_debug_hex! { $type , $accessor_fn }
425
/// ```
426
///
427
/// By default, this expects `$type` to implement `AsRef<[u8]>`.
428
///
429
/// Or, you can supply a series of tokens `$field_accessor`,
430
/// which will be used like this: `self.$field_accessor.as_ref()`
431
/// to get a `&[u8]`.
432
///
433
/// Or, you can supply `$accessor: fn(&$type) -> &[u8]`.
434
///
435
/// # Examples
436
///
437
/// ```
438
/// use tor_basic_utils::impl_debug_hex;
439
/// #[derive(Default)]
440
/// struct FourBytes([u8; 4]);
441
/// impl AsRef<[u8]> for FourBytes { fn as_ref(&self) -> &[u8] { &self.0 } }
442
/// impl_debug_hex! { FourBytes }
443
///
444
/// assert_eq!(
445
///     format!("{:?}", FourBytes::default()),
446
///     "FourBytes(00000000)",
447
/// );
448
/// ```
449
///
450
/// ```
451
/// use tor_basic_utils::impl_debug_hex;
452
/// #[derive(Default)]
453
/// struct FourBytes([u8; 4]);
454
/// impl_debug_hex! { FourBytes .0 }
455
///
456
/// assert_eq!(
457
///     format!("{:?}", FourBytes::default()),
458
///     "FourBytes(00000000)",
459
/// );
460
/// ```
461
///
462
/// ```
463
/// use tor_basic_utils::impl_debug_hex;
464
/// struct FourBytes([u8; 4]);
465
/// impl_debug_hex! { FourBytes, |self_| &self_.0 }
466
///
467
/// assert_eq!(
468
///     format!("{:?}", FourBytes([1,2,3,4])),
469
///     "FourBytes(01020304)",
470
/// )
471
/// ```
472
#[macro_export]
473
macro_rules! impl_debug_hex {
474
    { $type:ty $(,)? } => {
475
        $crate::impl_debug_hex! { $type, |self_| <$type as AsRef<[u8]>>::as_ref(&self_) }
476
    };
477
    { $type:ident . $($accessor:tt)+ } => {
478
557
        $crate::impl_debug_hex! { $type, |self_| self_ . $($accessor)* .as_ref() }
479
    };
480
    { $type:ty, $obtain:expr $(,)? } => {
481
        impl std::fmt::Debug for $type {
482
557
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
483
                use std::fmt::Write;
484
557
                let obtain: fn(&$type) -> &[u8] = $obtain;
485
557
                let bytes: &[u8] = obtain(self);
486
557
                write!(f, "{}(", stringify!($type))?;
487
18656
                for b in bytes {
488
18656
                    write!(f, "{:02x}", b)?;
489
                }
490
557
                write!(f, ")")?;
491
557
                Ok(())
492
557
            }
493
        }
494
    };
495
}
496

            
497
// ----------------------------------------------------------------------
498

            
499
/// Helper for defining a struct which can be (de)serialized several ways, including "natively"
500
///
501
/// Ideally we would have
502
/// ```rust ignore
503
/// #[derive(Deserialize)]
504
/// #[serde(try_from=Possibilities)]
505
/// struct Main { /* principal definition */ }
506
///
507
/// #[derive(Deserialize)]
508
/// #[serde(untagged)]
509
/// enum Possibilities { Main(Main), Other(OtherRepr) }
510
///
511
/// #[derive(Deserialize)]
512
/// struct OtherRepr { /* other representation we still want to read */ }
513
///
514
/// impl TryFrom<Possibilities> for Main { /* ... */ }
515
/// ```
516
///
517
/// But the impl for `Possibilities` ends up honouring the `try_from` on `Main`
518
/// so is recursive.
519
///
520
/// We solve that (ab)using serde's remote feature,
521
/// on a second copy of the struct definition.
522
///
523
/// See the Example for instructions.
524
/// It is important to **add test cases**
525
/// for all the representations you expect to parse and serialise,
526
/// since there are easy-to-write bugs,
527
/// for example omitting some of the necessary attributes.
528
///
529
/// # Generated output:
530
///
531
///  * The original struct definition, unmodified
532
///  * `#[derive(Serialize, Deserialize)] struct $main_Raw { }`
533
///
534
/// The `$main_Raw` struct ought not normally be to constructed anywhere,
535
/// and *isn't* convertible to or from the near-identical `$main` struct.
536
/// It exists only as a thing to feed to the serde remove derive,
537
/// and name in `with=`.
538
///
539
/// # Example
540
///
541
/// ```
542
/// use serde::{Deserialize, Serialize};
543
/// use tor_basic_utils::derive_serde_raw;
544
///
545
/// derive_serde_raw! {
546
///     #[derive(Deserialize, Serialize, Default, Clone, Debug)]
547
///     #[serde(try_from="BridgeConfigBuilderSerde", into="BridgeConfigBuilderSerde")]
548
///     pub struct BridgeConfigBuilder = "BridgeConfigBuilder" {
549
///         transport: Option<String>,
550
///         //...
551
///     }
552
/// }
553
///
554
/// #[derive(Serialize,Deserialize)]
555
/// #[serde(untagged)]
556
/// enum BridgeConfigBuilderSerde {
557
///     BridgeLine(String),
558
///     Dict(#[serde(with="BridgeConfigBuilder_Raw")] BridgeConfigBuilder),
559
/// }
560
///
561
/// impl TryFrom<BridgeConfigBuilderSerde> for BridgeConfigBuilder { //...
562
/// #    type Error = std::io::Error;
563
/// #    fn try_from(_: BridgeConfigBuilderSerde) -> Result<Self, Self::Error> { todo!() } }
564
/// impl From<BridgeConfigBuilder> for BridgeConfigBuilderSerde { //...
565
/// #    fn from(_: BridgeConfigBuilder) -> BridgeConfigBuilderSerde { todo!() } }
566
/// ```
567
#[macro_export]
568
macro_rules! derive_serde_raw { {
569
    $( #[ $($attrs:meta)* ] )*
570
    $vis:vis struct $main:ident=$main_s:literal
571
    $($body:tt)*
572
} => {
573
    $(#[ $($attrs)* ])*
574
    $vis struct $main
575
    $($body)*
576

            
577
    $crate::paste! {
578
        #[allow(non_camel_case_types)]
579
        #[derive(Serialize, Deserialize)]
580
        #[serde(remote=$main_s)]
581
        struct [< $main _Raw >]
582
        $($body)*
583
    }
584
} }
585

            
586
// ----------------------------------------------------------------------
587

            
588
/// Asserts that the type of the expression implements the given trait.
589
///
590
/// Example:
591
///
592
/// ```
593
/// # use tor_basic_utils::assert_val_impl_trait;
594
/// let x: u32 = 0;
595
/// assert_val_impl_trait!(x, Clone);
596
/// ```
597
#[macro_export]
598
macro_rules! assert_val_impl_trait {
599
    ($check:expr, $trait:path $(,)?) => {{
600
234
        fn ensure_trait<T: $trait>(_s: &T) {}
601
        ensure_trait(&$check);
602
    }};
603
}
604

            
605
// ----------------------------------------------------------------------
606

            
607
#[cfg(test)]
608
mod test {
609
    // @@ begin test lint list maintained by maint/add_warning @@
610
    #![allow(clippy::bool_assert_comparison)]
611
    #![allow(clippy::clone_on_copy)]
612
    #![allow(clippy::dbg_macro)]
613
    #![allow(clippy::mixed_attributes_style)]
614
    #![allow(clippy::print_stderr)]
615
    #![allow(clippy::print_stdout)]
616
    #![allow(clippy::single_char_pattern)]
617
    #![allow(clippy::unwrap_used)]
618
    #![allow(clippy::unchecked_time_subtraction)]
619
    #![allow(clippy::useless_vec)]
620
    #![allow(clippy::needless_pass_by_value)]
621
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
622
    use super::*;
623

            
624
    #[test]
625
    fn test_strip_suffix_ignore_ascii_case() {
626
        assert_eq!(
627
            "hi there".strip_suffix_ignore_ascii_case("THERE"),
628
            Some("hi ")
629
        );
630
        assert_eq!("hi here".strip_suffix_ignore_ascii_case("THERE"), None);
631
        assert_eq!("THERE".strip_suffix_ignore_ascii_case("there"), Some(""));
632
        assert_eq!("hi".strip_suffix_ignore_ascii_case("THERE"), None);
633
    }
634
}