-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathblock_lifetimes_independent.rs
74 lines (65 loc) · 2.38 KB
/
block_lifetimes_independent.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! Test that lifetimes in blocks are not bound to each other.
//!
//! These tests will succeed if there are `'a: 'b`-like bounds on the closure.
use block2::{ManualBlockEncoding, RcBlock};
use std::ffi::CStr;
use std::marker::PhantomData;
fn args<'a, 'b>(
f: impl Fn(&'a i32, &'b i32) + 'static,
) -> RcBlock<dyn Fn(&'b i32, &'a i32) + 'static> {
RcBlock::new(f)
}
fn args_return<'a, 'b>(
f: impl Fn(&'a i32) -> &'b i32 + 'static,
) -> RcBlock<dyn Fn(&'b i32) -> &'a i32 + 'static> {
RcBlock::new(f)
}
fn args_entire<'a, 'b>(f: impl Fn(&'a i32) + 'b) -> RcBlock<dyn Fn(&'b i32) + 'a> {
RcBlock::new(f)
}
fn return_entire<'a, 'b>(f: impl Fn() -> &'a i32 + 'b) -> RcBlock<dyn Fn() -> &'b i32 + 'a> {
RcBlock::new(f)
}
fn args_with_encoding<'a, 'b>(
f: impl Fn(&'a i32, &'b i32) + 'static,
) -> RcBlock<dyn Fn(&'b i32, &'a i32) + 'static> {
struct Enc<'a, 'b>(PhantomData<&'a i32>, PhantomData<&'b i32>);
unsafe impl<'a, 'b> ManualBlockEncoding for Enc<'a, 'b> {
type Arguments = (&'a i32, &'b i32);
type Return = ();
const ENCODING_CSTR: &'static CStr = c"v24@?0^i8^i16";
}
RcBlock::with_encoding::<_, _, _, Enc<'a, 'b>>(f)
}
fn args_return_with_encoding<'a, 'b>(
f: impl Fn(&'a i32) -> &'b i32 + 'static,
) -> RcBlock<dyn Fn(&'b i32) -> &'a i32 + 'static> {
struct Enc<'a, 'b>(PhantomData<&'a i32>, PhantomData<&'b i32>);
unsafe impl<'a, 'b> ManualBlockEncoding for Enc<'a, 'b> {
type Arguments = (&'a i32,);
type Return = &'b i32;
const ENCODING_CSTR: &'static CStr = c"^i816@?0^i8";
}
RcBlock::with_encoding::<_, _, _, Enc<'a, 'b>>(f)
}
fn args_entire_with_encoding<'a, 'b>(f: impl Fn(&'a i32) + 'b) -> RcBlock<dyn Fn(&'b i32) + 'a> {
struct Enc<'a>(PhantomData<&'a i32>);
unsafe impl<'a> ManualBlockEncoding for Enc<'a> {
type Arguments = (&'a i32,);
type Return = ();
const ENCODING_CSTR: &'static CStr = c"v16@?0^i8";
}
RcBlock::with_encoding::<_, _, _, Enc<'a>>(f)
}
fn return_entire_with_encoding<'a, 'b>(
f: impl Fn() -> &'a i32 + 'b,
) -> RcBlock<dyn Fn() -> &'b i32 + 'a> {
struct Enc<'a>(PhantomData<&'a i32>);
unsafe impl<'a> ManualBlockEncoding for Enc<'a> {
type Arguments = ();
type Return = &'a i32;
const ENCODING_CSTR: &'static CStr = c"^i8@?0";
}
RcBlock::with_encoding::<_, _, _, Enc<'a>>(f)
}
fn main() {}