Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
27
Task/Delegates/Rust/delegates-1.rust
Normal file
27
Task/Delegates/Rust/delegates-1.rust
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
trait Thingable {
|
||||
fn thing(&self) -> &str;
|
||||
}
|
||||
|
||||
struct Delegator<T>(Option<T>);
|
||||
|
||||
struct Delegate {}
|
||||
|
||||
impl Thingable for Delegate {
|
||||
fn thing(&self) -> &'static str {
|
||||
"Delegate implementation"
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Thingable> Thingable for Delegator<T> {
|
||||
fn thing(&self) -> &str {
|
||||
self.0.as_ref().map(|d| d.thing()).unwrap_or("Default implmementation")
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let d: Delegator<Delegate> = Delegator(None);
|
||||
println!("{}", d.thing());
|
||||
|
||||
let d: Delegator<Delegate> = Delegator(Some(Delegate {}));
|
||||
println!("{}", d.thing());
|
||||
}
|
||||
41
Task/Delegates/Rust/delegates-2.rust
Normal file
41
Task/Delegates/Rust/delegates-2.rust
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#![feature(specialization)]
|
||||
|
||||
trait Thingable {
|
||||
fn thing(&self) -> &str;
|
||||
}
|
||||
|
||||
struct Delegator<T>(Option<T>);
|
||||
|
||||
struct Delegate {}
|
||||
|
||||
impl Thingable for Delegate {
|
||||
fn thing(&self) -> &'static str {
|
||||
"Delegate implementation"
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Thingable for Delegator<T> {
|
||||
default fn thing(&self) -> &str {
|
||||
"Default implementation"
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Thingable> Thingable for Delegator<T> {
|
||||
fn thing(&self) -> &str {
|
||||
self.0.as_ref().map(|d| d.thing()).unwrap_or("Default implmementation")
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let d: Delegator<i32> = Delegator(None);
|
||||
println!("{}", d.thing());
|
||||
|
||||
let d: Delegator<i32> = Delegator(Some(42));
|
||||
println!("{}", d.thing());
|
||||
|
||||
let d: Delegator<Delegate> = Delegator(None);
|
||||
println!("{}", d.thing());
|
||||
|
||||
let d: Delegator<Delegate> = Delegator(Some(Delegate {}));
|
||||
println!("{}", d.thing());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue