mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 08:06:32 +00:00
55
kernel/crates/intertrait/tests/castable_to.rs
Normal file
55
kernel/crates/intertrait/tests/castable_to.rs
Normal file
@ -0,0 +1,55 @@
|
||||
use intertrait::cast::*;
|
||||
use intertrait::*;
|
||||
|
||||
struct Data;
|
||||
|
||||
trait Source: CastFrom {}
|
||||
|
||||
trait Greet {
|
||||
fn greet(&self);
|
||||
}
|
||||
|
||||
impl Greet for Data {
|
||||
fn greet(&self) {
|
||||
println!("Hello");
|
||||
}
|
||||
}
|
||||
|
||||
trait Greet1 {
|
||||
fn greet1(&self);
|
||||
}
|
||||
|
||||
impl Greet1 for Data {
|
||||
fn greet1(&self) {
|
||||
println!("Hello1");
|
||||
}
|
||||
}
|
||||
|
||||
trait Greet2 {
|
||||
fn greet2(&self);
|
||||
}
|
||||
|
||||
impl Greet2 for Data {
|
||||
fn greet2(&self) {
|
||||
println!("Hello2");
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for Data {}
|
||||
|
||||
castable_to! { Data => crate::Greet, Greet1, Greet2 }
|
||||
|
||||
#[test]
|
||||
fn test_multi_traits_on_struct() {
|
||||
let data = Data;
|
||||
let source: &dyn Source = &data;
|
||||
|
||||
let greet = source.cast::<dyn Greet>();
|
||||
greet.unwrap().greet();
|
||||
|
||||
let greet1 = source.cast::<dyn Greet1>();
|
||||
greet1.unwrap().greet1();
|
||||
|
||||
let greet2 = source.cast::<dyn Greet2>();
|
||||
greet2.unwrap().greet2();
|
||||
}
|
31
kernel/crates/intertrait/tests/on-enum.rs
Normal file
31
kernel/crates/intertrait/tests/on-enum.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use intertrait::cast::*;
|
||||
use intertrait::*;
|
||||
|
||||
#[cast_to(Greet)]
|
||||
#[allow(dead_code)]
|
||||
enum Data {
|
||||
Var1,
|
||||
Var2(u32),
|
||||
}
|
||||
|
||||
trait Source: CastFrom {}
|
||||
|
||||
trait Greet {
|
||||
fn greet(&self);
|
||||
}
|
||||
|
||||
impl Greet for Data {
|
||||
fn greet(&self) {
|
||||
println!("Hello");
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for Data {}
|
||||
|
||||
#[test]
|
||||
fn test_cast_to_on_enum() {
|
||||
let data = Data::Var2(1);
|
||||
let source: &dyn Source = &data;
|
||||
let greet = source.cast::<dyn Greet>();
|
||||
greet.unwrap().greet();
|
||||
}
|
27
kernel/crates/intertrait/tests/on-struct.rs
Normal file
27
kernel/crates/intertrait/tests/on-struct.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use intertrait::cast::*;
|
||||
use intertrait::*;
|
||||
|
||||
#[cast_to(Greet)]
|
||||
struct Data;
|
||||
|
||||
trait Source: CastFrom {}
|
||||
|
||||
trait Greet {
|
||||
fn greet(&self);
|
||||
}
|
||||
|
||||
impl Greet for Data {
|
||||
fn greet(&self) {
|
||||
println!("Hello");
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for Data {}
|
||||
|
||||
#[test]
|
||||
fn test_cast_to_on_struct() {
|
||||
let data = Data;
|
||||
let source: &dyn Source = &data;
|
||||
let greet = source.cast::<dyn Greet>();
|
||||
greet.unwrap().greet();
|
||||
}
|
33
kernel/crates/intertrait/tests/on-trait-impl-assoc-type1.rs
Normal file
33
kernel/crates/intertrait/tests/on-trait-impl-assoc-type1.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use intertrait::cast::*;
|
||||
use intertrait::*;
|
||||
|
||||
struct I32Data(i32);
|
||||
|
||||
trait Source: CastFrom {}
|
||||
|
||||
trait Producer {
|
||||
type Output: Debug;
|
||||
|
||||
fn produce(&self) -> Self::Output;
|
||||
}
|
||||
|
||||
#[cast_to]
|
||||
impl Producer for I32Data {
|
||||
type Output = i32;
|
||||
|
||||
fn produce(&self) -> Self::Output {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for I32Data {}
|
||||
|
||||
#[test]
|
||||
fn test_cast_to_on_trait_impl_with_assoc_type1() {
|
||||
let data = I32Data(100);
|
||||
let source: &dyn Source = &data;
|
||||
let producer = source.cast::<dyn Producer<Output = i32>>();
|
||||
assert_eq!(producer.unwrap().produce(), data.0);
|
||||
}
|
35
kernel/crates/intertrait/tests/on-trait-impl-assoc-type2.rs
Normal file
35
kernel/crates/intertrait/tests/on-trait-impl-assoc-type2.rs
Normal file
@ -0,0 +1,35 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use intertrait::cast::*;
|
||||
use intertrait::*;
|
||||
|
||||
struct Data;
|
||||
|
||||
trait Source: CastFrom {}
|
||||
|
||||
trait Concat {
|
||||
type I1: Debug;
|
||||
type I2: Debug;
|
||||
|
||||
fn concat(&self, a: Self::I1, b: Self::I2) -> String;
|
||||
}
|
||||
|
||||
#[cast_to]
|
||||
impl Concat for Data {
|
||||
type I1 = i32;
|
||||
type I2 = &'static str;
|
||||
|
||||
fn concat(&self, a: Self::I1, b: Self::I2) -> String {
|
||||
format!("Data: {} - {}", a, b)
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for Data {}
|
||||
|
||||
#[test]
|
||||
fn test_cast_to_on_trait_impl_with_assoc_type2() {
|
||||
let data = Data;
|
||||
let source: &dyn Source = &data;
|
||||
let concat = source.cast::<dyn Concat<I1 = i32, I2 = &'static str>>();
|
||||
assert_eq!(concat.unwrap().concat(101, "hello"), "Data: 101 - hello");
|
||||
}
|
38
kernel/crates/intertrait/tests/on-trait-impl-assoc-type3.rs
Normal file
38
kernel/crates/intertrait/tests/on-trait-impl-assoc-type3.rs
Normal file
@ -0,0 +1,38 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use intertrait::cast::*;
|
||||
use intertrait::*;
|
||||
|
||||
struct Data;
|
||||
|
||||
trait Source: CastFrom {}
|
||||
|
||||
trait Concat<T: Debug> {
|
||||
type I1: Debug;
|
||||
type I2: Debug;
|
||||
|
||||
fn concat(&self, prefix: T, a: Self::I1, b: Self::I2) -> String;
|
||||
}
|
||||
|
||||
#[cast_to]
|
||||
impl Concat<String> for Data {
|
||||
type I1 = i32;
|
||||
type I2 = &'static str;
|
||||
|
||||
fn concat(&self, prefix: String, a: Self::I1, b: Self::I2) -> String {
|
||||
format!("{}: {} - {}", prefix, a, b)
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for Data {}
|
||||
|
||||
#[test]
|
||||
fn test_cast_to_on_trait_impl_with_assoc_type3() {
|
||||
let data = Data;
|
||||
let source: &dyn Source = &data;
|
||||
let concat = source.cast::<dyn Concat<String, I1 = i32, I2 = &'static str>>();
|
||||
assert_eq!(
|
||||
concat.unwrap().concat("Data".to_owned(), 101, "hello"),
|
||||
"Data: 101 - hello"
|
||||
);
|
||||
}
|
27
kernel/crates/intertrait/tests/on-trait-impl.rs
Normal file
27
kernel/crates/intertrait/tests/on-trait-impl.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use intertrait::cast::*;
|
||||
use intertrait::*;
|
||||
|
||||
struct Data;
|
||||
|
||||
trait Source: CastFrom {}
|
||||
|
||||
trait Greet {
|
||||
fn greet(&self);
|
||||
}
|
||||
|
||||
#[cast_to]
|
||||
impl Greet for Data {
|
||||
fn greet(&self) {
|
||||
println!("Hello");
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for Data {}
|
||||
|
||||
#[test]
|
||||
fn test_cast_to_on_trait_impl() {
|
||||
let data = Data;
|
||||
let source: &dyn Source = &data;
|
||||
let greet = source.cast::<dyn Greet>();
|
||||
greet.unwrap().greet();
|
||||
}
|
54
kernel/crates/intertrait/tests/on-type-multi-traits.rs
Normal file
54
kernel/crates/intertrait/tests/on-type-multi-traits.rs
Normal file
@ -0,0 +1,54 @@
|
||||
use intertrait::cast::*;
|
||||
use intertrait::*;
|
||||
|
||||
#[cast_to(Greet, Greet1, Greet2)]
|
||||
struct Data;
|
||||
|
||||
trait Source: CastFrom {}
|
||||
|
||||
trait Greet {
|
||||
fn greet(&self);
|
||||
}
|
||||
|
||||
impl Greet for Data {
|
||||
fn greet(&self) {
|
||||
println!("Hello");
|
||||
}
|
||||
}
|
||||
|
||||
trait Greet1 {
|
||||
fn greet1(&self);
|
||||
}
|
||||
|
||||
impl Greet1 for Data {
|
||||
fn greet1(&self) {
|
||||
println!("Hello1");
|
||||
}
|
||||
}
|
||||
|
||||
trait Greet2 {
|
||||
fn greet2(&self);
|
||||
}
|
||||
|
||||
impl Greet2 for Data {
|
||||
fn greet2(&self) {
|
||||
println!("Hello2");
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for Data {}
|
||||
|
||||
#[test]
|
||||
fn test_multi_traits_on_struct() {
|
||||
let data = Data;
|
||||
let source: &dyn Source = &data;
|
||||
|
||||
let greet = source.cast::<dyn Greet>();
|
||||
greet.unwrap().greet();
|
||||
|
||||
let greet1 = source.cast::<dyn Greet1>();
|
||||
greet1.unwrap().greet1();
|
||||
|
||||
let greet2 = source.cast::<dyn Greet2>();
|
||||
greet2.unwrap().greet2();
|
||||
}
|
5
kernel/crates/intertrait/tests/run.rs
Normal file
5
kernel/crates/intertrait/tests/run.rs
Normal file
@ -0,0 +1,5 @@
|
||||
#[test]
|
||||
fn tests() {
|
||||
let t = trybuild::TestCases::new();
|
||||
t.compile_fail("tests/ui/*.rs");
|
||||
}
|
27
kernel/crates/intertrait/tests/ui/duplicate-flags.rs
Normal file
27
kernel/crates/intertrait/tests/ui/duplicate-flags.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use intertrait::cast::*;
|
||||
use intertrait::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cast_to([sync, sync] Greet)]
|
||||
struct Data;
|
||||
|
||||
trait Source: CastFromSync {}
|
||||
|
||||
trait Greet {
|
||||
fn greet(&self);
|
||||
}
|
||||
|
||||
impl Greet for Data {
|
||||
fn greet(&self) {
|
||||
println!("Hello");
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for Data {}
|
||||
|
||||
fn main() {
|
||||
let data = Arc::new(Data);
|
||||
let source: Arc<dyn Source> = data;
|
||||
let greet = source.cast::<dyn Greet>();
|
||||
greet.unwrap_or_else(|_| panic!("can't happen")).greet();
|
||||
}
|
5
kernel/crates/intertrait/tests/ui/duplicate-flags.stderr
Normal file
5
kernel/crates/intertrait/tests/ui/duplicate-flags.stderr
Normal file
@ -0,0 +1,5 @@
|
||||
error: Duplicated flag: sync
|
||||
--> $DIR/duplicate-flags.rs:5:18
|
||||
|
|
||||
5 | #[cast_to([sync, sync] Greet)]
|
||||
| ^^^^
|
31
kernel/crates/intertrait/tests/ui/on-generic-type.rs
Normal file
31
kernel/crates/intertrait/tests/ui/on-generic-type.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use intertrait::*;
|
||||
use intertrait::cast::*;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
#[cast_to(Greet)]
|
||||
struct Data<T: 'static> {
|
||||
phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
trait Source: CastFrom {}
|
||||
|
||||
trait Greet {
|
||||
fn greet(&self);
|
||||
}
|
||||
|
||||
impl<T: 'static> Greet for Data<T> {
|
||||
fn greet(&self) {
|
||||
println!("Hello");
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> Source for Data<T> {}
|
||||
|
||||
fn main() {
|
||||
let data = Data::<i32> {
|
||||
phantom: PhantomData,
|
||||
};
|
||||
let source: &dyn Source = &data;
|
||||
let greet = source.cast::<dyn Greet>();
|
||||
greet.unwrap().greet();
|
||||
}
|
5
kernel/crates/intertrait/tests/ui/on-generic-type.stderr
Normal file
5
kernel/crates/intertrait/tests/ui/on-generic-type.stderr
Normal file
@ -0,0 +1,5 @@
|
||||
error: #[cast_to(..)] can't be used on a generic type definition
|
||||
--> tests/ui/on-generic-type.rs:6:12
|
||||
|
|
||||
6 | struct Data<T: 'static> {
|
||||
| ^^^^^^^^^^^^
|
14
kernel/crates/intertrait/tests/ui/on-type-impl.rs
Normal file
14
kernel/crates/intertrait/tests/ui/on-type-impl.rs
Normal file
@ -0,0 +1,14 @@
|
||||
use intertrait::*;
|
||||
|
||||
struct Data;
|
||||
|
||||
#[cast_to]
|
||||
impl Data {
|
||||
fn hello() {
|
||||
println!("hello!");
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = Data;
|
||||
}
|
5
kernel/crates/intertrait/tests/ui/on-type-impl.stderr
Normal file
5
kernel/crates/intertrait/tests/ui/on-type-impl.stderr
Normal file
@ -0,0 +1,5 @@
|
||||
error: #[cast_to] should only be on an impl of a trait
|
||||
--> $DIR/on-type-impl.rs:6:6
|
||||
|
|
||||
6 | impl Data {
|
||||
| ^^^^
|
27
kernel/crates/intertrait/tests/ui/unknown-flag.rs
Normal file
27
kernel/crates/intertrait/tests/ui/unknown-flag.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use intertrait::cast::*;
|
||||
use intertrait::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cast_to([sync, send] Greet)]
|
||||
struct Data;
|
||||
|
||||
trait Source: CastFromSync {}
|
||||
|
||||
trait Greet {
|
||||
fn greet(&self);
|
||||
}
|
||||
|
||||
impl Greet for Data {
|
||||
fn greet(&self) {
|
||||
println!("Hello");
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for Data {}
|
||||
|
||||
fn main() {
|
||||
let data = Arc::new(Data);
|
||||
let source: Arc<dyn Source> = data;
|
||||
let greet = source.cast::<dyn Greet>();
|
||||
greet.unwrap_or_else(|_| panic!("can't happen")).greet();
|
||||
}
|
5
kernel/crates/intertrait/tests/ui/unknown-flag.stderr
Normal file
5
kernel/crates/intertrait/tests/ui/unknown-flag.stderr
Normal file
@ -0,0 +1,5 @@
|
||||
error: Unknown flag: send
|
||||
--> $DIR/unknown-flag.rs:5:18
|
||||
|
|
||||
5 | #[cast_to([sync, send] Greet)]
|
||||
| ^^^^
|
Reference in New Issue
Block a user