Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix compiler errors on latest nighly #73

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(fn_traits, unboxed_closures)]
#![feature(fn_traits, unboxed_closures, tuple_trait)]

//! Mocking framework for Rust (currently only nightly)
//!
Expand Down
16 changes: 13 additions & 3 deletions src/mock_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::mocking::MockResult;
use std::any::TypeId;
use std::cell::RefCell;
use std::collections::HashMap;
use std::marker::Tuple;
use std::mem::transmute;
use std::rc::Rc;

Expand Down Expand Up @@ -44,7 +45,10 @@ impl MockStore {
.add(id, mock);
}

pub unsafe fn call<I, O>(&self, id: TypeId, mut input: I) -> MockResult<I, O> {
pub unsafe fn call<I, O>(&self, id: TypeId, mut input: I) -> MockResult<I, O>
where
I: Tuple,
{
// Do not hold RefCell borrow while calling mock, it can try to modify mocks
let layer_count = self.layers.borrow().len();
for layer_idx in (0..layer_count).rev() {
Expand Down Expand Up @@ -118,7 +122,10 @@ struct ErasedStoredMock {
}

impl ErasedStoredMock {
unsafe fn call<I, O>(self, input: I) -> MockLayerResult<I, O> {
unsafe fn call<I, O>(self, input: I) -> MockLayerResult<I, O>
where
I: Tuple,
{
let unerased: StoredMock<I, O> = transmute(self.mock);
unerased.call(input)
}
Expand All @@ -137,7 +144,10 @@ impl<I, O> StoredMock<I, O> {
}
}

fn call(&self, input: I) -> MockLayerResult<I, O> {
fn call(&self, input: I) -> MockLayerResult<I, O>
where
I: Tuple,
{
match self.mock.try_borrow_mut() {
Ok(mut mock) => MockLayerResult::Handled(mock.call_mut(input)),
Err(_) => MockLayerResult::Unhandled(input),
Expand Down
17 changes: 13 additions & 4 deletions src/mocking.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::mock_store::{MockLayer, MockStore};
use std::any::{Any, TypeId};
use std::marker::PhantomData;
use std::marker::{PhantomData, Tuple};
use std::mem::transmute;

/// Trait for setting up mocks
Expand Down Expand Up @@ -42,7 +42,9 @@ pub trait Mockable<T, O> {
/// assert_eq!("mocked", get_string(&Context::default()));
/// }
/// ```
unsafe fn mock_raw<M: FnMut<T, Output = MockResult<T, O>>>(&self, mock: M);
unsafe fn mock_raw<M: FnMut<T, Output = MockResult<T, O>>>(&self, mock: M)
where
T: Tuple;

/// A safe variant of [mock_raw](#tymethod.mock_raw) for static closures
///
Expand All @@ -62,7 +64,9 @@ pub trait Mockable<T, O> {
/// assert_eq!("mocked", get_string());
/// }
/// ```
fn mock_safe<M: FnMut<T, Output = MockResult<T, O>> + 'static>(&self, mock: M);
fn mock_safe<M: FnMut<T, Output = MockResult<T, O>> + 'static>(&self, mock: M)
where
T: Tuple;

/// Stop mocking this function.
///
Expand Down Expand Up @@ -97,7 +101,10 @@ pub fn clear_mocks() {
MOCK_STORE.with(|mock_store| mock_store.clear())
}

impl<T, O, F: FnOnce<T, Output = O>> Mockable<T, O> for F {
impl<T, O, F: FnOnce<T, Output = O>> Mockable<T, O> for F
where
T: Tuple,
{
unsafe fn mock_raw<M: FnMut<T, Output = MockResult<T, O>>>(&self, mock: M) {
let id = self.get_mock_id();
let boxed = Box::new(mock) as Box<dyn FnMut<_, Output = _>>;
Expand Down Expand Up @@ -190,6 +197,7 @@ impl<'a> MockContext<'a> {
where
F: Mockable<I, O>,
M: FnMut<I, Output = MockResult<I, O>> + 'a,
I: Tuple,
{
unsafe { self.mock_raw(mockable, mock) }
}
Expand All @@ -202,6 +210,7 @@ impl<'a> MockContext<'a> {
where
F: Mockable<I, O>,
M: FnMut<I, Output = MockResult<I, O>>,
I: Tuple,
{
let mock_box = Box::new(mock) as Box<dyn FnMut<_, Output = _>>;
let mock_box_static: Box<dyn FnMut<I, Output = MockResult<I, O>> + 'static> =
Expand Down
2 changes: 1 addition & 1 deletion tests/injecting.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(const_fn, proc_macro_hygiene)]
#![feature(proc_macro_hygiene)]

// Test if injecting works even if mocktopus is aliased
extern crate mocktopus as mocktopus_aliased;
Expand Down