|
| 1 | +use crate::driver::object::{self, DynDriver, Object}; |
| 2 | +use core::fmt; |
| 3 | +use std::cell::RefCell; |
| 4 | + |
| 5 | +pub trait Scope: 'static + DynDriver + core::any::Any { |
| 6 | + fn borrowed(&mut self) -> object::Borrowed; |
| 7 | +} |
| 8 | + |
| 9 | +impl<T> Scope for T |
| 10 | +where |
| 11 | + T: 'static + DynDriver + core::any::Any, |
| 12 | +{ |
| 13 | + fn borrowed(&mut self) -> object::Borrowed { |
| 14 | + object::Borrowed(self) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +type Type = Box<dyn Scope>; |
| 19 | + |
| 20 | +thread_local! { |
| 21 | + static SCOPE: RefCell<Type> = RefCell::new(Box::new(Object(default()))); |
| 22 | +} |
| 23 | + |
| 24 | +fn default() -> impl crate::Driver { |
| 25 | + use rand_core::SeedableRng; |
| 26 | + use rand_xoshiro::Xoshiro128PlusPlus; |
| 27 | + |
| 28 | + let mut seed = [42; 16]; |
| 29 | + // make a best effort to get random seeds |
| 30 | + let _ = getrandom::getrandom(&mut seed); |
| 31 | + let rng = Xoshiro128PlusPlus::from_seed(seed); |
| 32 | + crate::driver::Rng::new(rng, &Default::default()) |
| 33 | +} |
| 34 | + |
| 35 | +fn set(value: Type) -> Type { |
| 36 | + SCOPE.with(|r| core::mem::replace(&mut *r.borrow_mut(), value)) |
| 37 | +} |
| 38 | + |
| 39 | +// protect against panics in the `with` function |
| 40 | +struct Prev(Option<Type>); |
| 41 | + |
| 42 | +impl Prev { |
| 43 | + fn reset(mut self) -> Type { |
| 44 | + set(self.0.take().unwrap()) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl Drop for Prev { |
| 49 | + fn drop(&mut self) { |
| 50 | + if let Some(prev) = self.0.take() { |
| 51 | + let _ = set(prev); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +pub fn with<D, F, R>(driver: Box<D>, f: F) -> (Box<D>, R) |
| 57 | +where |
| 58 | + D: Scope, |
| 59 | + F: FnOnce() -> R, |
| 60 | +{ |
| 61 | + let prev = Prev(Some(set(driver))); |
| 62 | + let res = f(); |
| 63 | + let driver = prev.reset(); |
| 64 | + let driver = if driver.type_id() == core::any::TypeId::of::<D>() { |
| 65 | + unsafe { |
| 66 | + let raw = Box::into_raw(driver); |
| 67 | + Box::from_raw(raw as *mut D) |
| 68 | + } |
| 69 | + } else { |
| 70 | + panic!( |
| 71 | + "invalid scope state; expected {}", |
| 72 | + core::any::type_name::<D>() |
| 73 | + ) |
| 74 | + }; |
| 75 | + (driver, res) |
| 76 | +} |
| 77 | + |
| 78 | +fn borrow_with<F: FnOnce(&mut object::Borrowed) -> R, R>(f: F) -> R { |
| 79 | + SCOPE.with(|r| { |
| 80 | + let mut driver = r.borrow_mut(); |
| 81 | + let mut driver = driver.borrowed(); |
| 82 | + f(&mut driver) |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +#[track_caller] |
| 87 | +pub fn any<G: crate::ValueGenerator>(g: &G) -> G::Output { |
| 88 | + borrow_with(|driver| { |
| 89 | + g.generate(driver).unwrap_or_else(|| { |
| 90 | + std::panic::panic_any(Error { |
| 91 | + location: core::panic::Location::caller(), |
| 92 | + generator: core::any::type_name::<G>(), |
| 93 | + output: core::any::type_name::<G::Output>(), |
| 94 | + }) |
| 95 | + }) |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +#[track_caller] |
| 100 | +pub fn assume(condition: bool, message: &'static str) { |
| 101 | + if !condition { |
| 102 | + std::panic::panic_any(Error { |
| 103 | + location: core::panic::Location::caller(), |
| 104 | + generator: "<assume>", |
| 105 | + output: message, |
| 106 | + }); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[track_caller] |
| 111 | +pub fn fill_bytes(bytes: &mut [u8]) { |
| 112 | + borrow_with(|driver| { |
| 113 | + let len = bytes.len(); |
| 114 | + let mut hint = || (len, Some(len)); |
| 115 | + driver |
| 116 | + .0 |
| 117 | + .gen_from_bytes(&mut hint, &mut |src: &[u8]| { |
| 118 | + if src.len() == len { |
| 119 | + bytes.copy_from_slice(src); |
| 120 | + Some(len) |
| 121 | + } else { |
| 122 | + None |
| 123 | + } |
| 124 | + }) |
| 125 | + .unwrap_or_else(|| { |
| 126 | + std::panic::panic_any(Error { |
| 127 | + location: core::panic::Location::caller(), |
| 128 | + generator: "<fill_bytes>", |
| 129 | + output: "could not generate enough bytes", |
| 130 | + }); |
| 131 | + }) |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +#[derive(Clone)] |
| 136 | +pub struct Error { |
| 137 | + location: &'static core::panic::Location<'static>, |
| 138 | + generator: &'static str, |
| 139 | + output: &'static str, |
| 140 | +} |
| 141 | + |
| 142 | +impl fmt::Debug for Error { |
| 143 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 144 | + f.debug_struct("Error") |
| 145 | + .field("location", &self.location) |
| 146 | + .field("generator", &self.generator) |
| 147 | + .field("output", &self.output) |
| 148 | + .finish() |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +impl fmt::Display for Error { |
| 153 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 154 | + write!( |
| 155 | + f, |
| 156 | + "Could not generate value of type {} at {}", |
| 157 | + self.output, self.location, |
| 158 | + ) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl std::error::Error for Error {} |
0 commit comments