Skip to content

Commit

Permalink
Merge pull request #313 from asomers/unsafe_trait
Browse files Browse the repository at this point in the history
Support unsafe traits
  • Loading branch information
asomers committed Jul 24, 2021
2 parents 7bc82a9 + 091bbdd commit 66d9934
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
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

0 comments on commit 66d9934

Please sign in to comment.