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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
use tvm_ffi::{Any, AnyView, Dispatcher, IntoDispatcher, Result};
#[test]
fn into_dispatcher_supports_owned_borrowed_and_callbacks() -> Result<()> {
type Outcome = Option<Result<Any>>;
struct Add {
calls: usize,
}
impl<'v> Dispatcher<(AnyView<'v>, i64), Outcome> for &mut Add {
fn dispatch_tuple(self, (value, offset): (AnyView<'v>, i64)) -> Outcome {
value.downcast_ref::<i64>().copied().map(|value| {
self.calls += 1;
Ok(Any::from(value + offset))
})
}
}
// An owned dispatcher keeps its exact argument and result signature.
let mut owned =
IntoDispatcher::<(AnyView<'_>, i64), Outcome, _>::into_dispatcher(Add { calls: 0 });
assert_eq!(
owned
.dispatch_tuple((AnyView::from(&40_i64), 2))
.unwrap()?
.downcast_ref::<i64>()
.copied(),
Some(42)
);
// A borrowed dispatcher updates the original instance.
let mut source = Add { calls: 0 };
{
let borrowed =
IntoDispatcher::<(AnyView<'_>, i64), Outcome, _>::into_dispatcher(&mut source);
assert_eq!(
borrowed
.dispatch_tuple((AnyView::from(&40_i64), 3))
.unwrap()?
.downcast_ref::<i64>()
.copied(),
Some(43)
);
}
assert_eq!(source.calls, 1);
// Typed misses fall through; matched callbacks receive only their prefix.
let callbacks = (
|_: bool| Any::from(-1_i64),
|value: i64, offset: i64| Any::from(value + offset),
|_: AnyView<'_>| -> Any { panic!("a matched callback must stop the tuple") },
);
let mut tuple =
IntoDispatcher::<(AnyView<'_>, i64, &str), Outcome, _>::into_dispatcher(callbacks);
assert_eq!(
Dispatcher::<_, Outcome>::dispatch_tuple(&mut tuple, (AnyView::from(&40_i64), 4, "unused"))
.unwrap()?
.downcast_ref::<i64>()
.copied(),
Some(44)
);
assert_eq!(
Dispatcher::<_, Outcome>::dispatch_tuple(&mut tuple, (AnyView::from(&true), 4, "unused"))
.unwrap()?
.downcast_ref::<i64>()
.copied(),
Some(-1)
);
Ok(())
}