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

Not able to mock unsafe function #48

Open
tobiaskrause opened this issue Jul 29, 2019 · 1 comment
Open

Not able to mock unsafe function #48

tobiaskrause opened this issue Jul 29, 2019 · 1 comment

Comments

@tobiaskrause
Copy link

Trying to mock an unsafe function results in an error. Is there any particular reason my unsafe function cannot be mocked?

Minimal test:

    unsafe fn count() -> i32 {
        1
    }

    #[test]
    fn unsafe_test() {
            unsafe {
                count.mock_raw(|| MockResult::Return(0));
                assert_eq!(0, count());
            }

    }

Output:

error[E0599]: no method named `mock_raw` found for type `unsafe fn() -> i32 {test::count}` in the current scope
   --> src\example.rs:10:23
    |
    |                 count.mock_raw(|| MockResult::Return(0));
    |                       ^^^^^^^^
    |
    = note: count is a function, perhaps you wish to call it
    = note: the method `mock_raw` exists but the following trait bounds were not satisfied:
            `unsafe fn() -> i32 {test::count} : mocktopus::mocking::Mockable<_, _>`

@CodeSandwich
Copy link
Owner

Yes, unsafe funcions don't implement Fn.

One solution could be to switch whole API to using fn, but it lacks some first class citizenshipness. For example you can't implement a trait for fn item and you can't cast fn item to fn pointer without manually writing its signature. Also fn pointers are not unique, two functions can have the same address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants