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

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.