Created
April 8, 2019 00:09
-
-
Save rust-play/400149a0a6d1dfcc7c3557bf67cb898d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
unsafe { | |
Bar.mock_unsafe(foo); | |
// Bar.mock_unsafe(foo_no); // MUST FAIL | |
BarNo.mock_unsafe(foo); | |
BarNo.mock_unsafe(foo_no); | |
} | |
// Bar.mock(foo); // MUST FAIL | |
// Bar.mock_unsafe(foo_no); // MUST FAIL | |
BarNo.mock(foo); | |
BarNo.mock(foo_no); | |
} | |
fn foo(a: u8, b: u8) -> u8 { | |
a + b | |
} | |
fn foo_no() -> u8 { | |
1 | |
} | |
struct Bar; | |
impl MockUnsafe<(u8, u8)> for Bar { | |
type Output = u8; | |
} | |
struct BarNo; | |
impl MockUnsafe<()> for BarNo { | |
type Output = u8; | |
} | |
trait MockUnsafe<I>: Sized { | |
type Output; | |
unsafe fn mock_unsafe(self, _: impl Mockable<I, Output = Self::Output>) { | |
// own registration | |
} | |
} | |
impl <I, J, O, T: MockUnsafe<(), Output = O>> MockUnsafe<(I, J)> for T { | |
type Output = O; | |
} | |
trait Mock: Sized + MockImplGuard { | |
type Output; | |
fn mock<I>(self, f: impl Mockable<I, Output = Self::Output>); | |
} | |
impl<O: 'static, T: MockUnsafe<(), Output = O> + 'static> Mock for T { | |
type Output = O; | |
fn mock<I>(self, _: impl Mockable<I, Output = Self::Output>) { | |
// own registration | |
} | |
} | |
// To be made private | |
trait MockImplGuard {} | |
impl<O: 'static, T: MockUnsafe<(), Output = O> + 'static> MockImplGuard for T {} | |
trait Mockable<I> { | |
type Output; | |
} | |
impl<O, T: Fn() -> O> Mockable<()> for T { | |
type Output = O; | |
} | |
impl<I, J, O, T: Fn(I, J) -> O> Mockable<(I, J)> for T { | |
type Output = O; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment