Struct mimicry::ThreadLocal
source · pub struct ThreadLocal<T: Send> { /* private fields */ }
Expand description
Thread-local mock state wrapper.
This type rarely needs to be used directly; #[derive(Mock)]
macro with the default settings
sets a wrapper automatically.
As the name implies, this wrapper does not share the mock state across threads. If a thread
is spawned during test, mocked functions called from this thread will always use the real
implementations. This behavior is fine in simple cases, i.e., unless mocked functions
are called from multiple threads spawned by a single test. If cross-thread mocking is required,
consider Shared
wrapper.
Examples
use mimicry::Mock;
#[derive(Default, Mock)]
struct MockState {
// fields to support mock logic
}
#[test]
fn some_test() {
// Sets the mock state until `mock_guard` is dropped.
let mock_guard = MockState::default().set_as_mock();
// Call mocked functions (maybe, indirectly). All calls
// need to happen from the original test thread.
let state = mock_guard.into_inner();
// Can check the state here...
}
Trait Implementations
sourceimpl<T: Debug + Send> Debug for ThreadLocal<T>
impl<T: Debug + Send> Debug for ThreadLocal<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for ThreadLocal<T>
impl<T> Send for ThreadLocal<T>
impl<T> Sync for ThreadLocal<T>
impl<T> Unpin for ThreadLocal<T>
impl<T> UnwindSafe for ThreadLocal<T>where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more