Skip to content

Not able to mock unsafe function #48

@tobiaskrause

Description

@tobiaskrause

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<_, _>`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions