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

Support unsafe traits #313

Merged
merged 2 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).


## [ Unreleased ] - ReleaseDate

### Added

- `mock!` and `#[automock]` now support `unsafe` traits.
([#313](https://github.com/asomers/mockall/pull/313))

## [ 0.10.2 ] - 2021-07-12

### Fixed
Expand All @@ -15,6 +23,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
regression in v0.10.0.
([#312](https://github.com/asomers/mockall/pull/312))


## [ 0.10.1 ] - 2021-07-01

### Fixed
Expand Down
36 changes: 36 additions & 0 deletions mockall/tests/automock_unsafe_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// vim: ts=80
#![deny(warnings)]

use mockall::*;

#[automock]
pub unsafe trait Foo {
fn foo(&self) -> i32;
}

struct Baz{}

#[automock]
unsafe impl Foo for Baz {
fn foo(&self) -> i32 {
unimplemented!()
}
}

#[test]
fn automock_trait() {
let mut mock = MockFoo::new();
mock.expect_foo()
.return_const(42);

assert_eq!(42, mock.foo());
}

#[test]
fn automock_trait_impl() {
let mut mock = MockBaz::new();
mock.expect_foo()
.return_const(42);

assert_eq!(42, mock.foo());
}
24 changes: 24 additions & 0 deletions mockall/tests/mock_unsafe_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// vim: ts=80
#![deny(warnings)]

use mockall::*;

pub unsafe trait Bar {
fn bar(&self) -> i32;
}

mock! {
pub Foo{}
unsafe impl Bar for Foo {
fn bar(&self) -> i32;
}
}

#[test]
fn return_const() {
let mut mock = MockFoo::new();
mock.expect_bar()
.return_const(42);

assert_eq!(42, mock.bar());
}
9 changes: 6 additions & 3 deletions mockall_derive/src/mock_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub(crate) struct MockTrait {
/// Path on which the trait is implemented. Usually will be the same as
/// structname, but might include concrete generic parameters.
self_path: PathSegment,
pub types: Vec<ImplItemType>
pub types: Vec<ImplItemType>,
pub unsafety: Option<Token![unsafe]>
}

impl MockTrait {
Expand Down Expand Up @@ -116,7 +117,8 @@ impl MockTrait {
ss_name,
trait_path,
self_path,
types
types,
unsafety: impl_.unsafety
}
}

Expand Down Expand Up @@ -156,9 +158,10 @@ impl MockTrait {
let trait_path = &self.trait_path;
let self_path = &self.self_path;
let types = &self.types;
let unsafety = &self.unsafety;
quote!(
#(#trait_impl_attrs)*
impl #ig #trait_path for #self_path #wc {
#unsafety impl #ig #trait_path for #self_path #wc {
#(#consts)*
#(#types)*
#(#calls)*
Expand Down