multiple_blanket.md
This commit is contained in:
parent
71b8825d18
commit
e8ec6c2780
13
.gitignore
vendored
13
.gitignore
vendored
@ -1 +1,14 @@
|
||||
book
|
||||
|
||||
|
||||
# Added by cargo
|
||||
|
||||
/target
|
||||
|
||||
|
||||
# Added by cargo
|
||||
#
|
||||
# already existing elements were commented out
|
||||
|
||||
#/target
|
||||
/Cargo.lock
|
||||
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "exercises"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
@ -1,3 +1,4 @@
|
||||
# Summary
|
||||
|
||||
- [Chapter 1](./chapter_1.md)
|
||||
- [`A`/`A1`/`A2`](./exercises/multiple_blanket.md)
|
||||
|
64
src/exercises/multiple_blanket.md
Normal file
64
src/exercises/multiple_blanket.md
Normal file
@ -0,0 +1,64 @@
|
||||
```rust
|
||||
trait A {
|
||||
fn a_ref(&self) -> u64;
|
||||
fn a_mut(&mut self) -> bool;
|
||||
fn a_move(self) -> Self;
|
||||
}
|
||||
|
||||
fn test(x: impl A) {
|
||||
x.a_ref();
|
||||
let mut x = x.a_move();
|
||||
x.a_mut();
|
||||
}
|
||||
|
||||
fn test1(x: impl A1) {
|
||||
test(x)
|
||||
}
|
||||
|
||||
fn test2(x: impl A2) {
|
||||
test(x)
|
||||
}
|
||||
# use std::marker::PhantomData;
|
||||
# trait Wrapper<T>: Sized {}
|
||||
# #[repr(transparent)]
|
||||
# struct Wrapped<T, V: ?Sized>(PhantomData<V>, T);
|
||||
# trait AProxy {
|
||||
# type T;
|
||||
# fn ap_ref(t: &Self::T) -> u64;
|
||||
# fn ap_mut(t: &mut Self::T) -> bool;
|
||||
# fn ap_move(t: Self::T) -> Self::T;
|
||||
# }
|
||||
# trait AWrapper: Wrapper<Self::AWrapped> { type AWrapped; }
|
||||
# impl<T: AWrapper> A for T where T::AWrapped: AProxy<T = T>,
|
||||
# {
|
||||
# fn a_ref(&self) -> u64 { <T::AWrapped as AProxy>::ap_ref(self) }
|
||||
# fn a_mut(&mut self) -> bool { <T::AWrapped as AProxy>::ap_mut(self) }
|
||||
# fn a_move(self) -> Self { <T::AWrapped as AProxy>::ap_move(self) }
|
||||
# }
|
||||
# trait A1: AWrapper<AWrapped = Aw1<Self>> {
|
||||
# fn a1_ref(&self) -> u64;
|
||||
# fn a1_mut(&mut self) -> bool;
|
||||
# fn a1_move(self) -> Self;
|
||||
# }
|
||||
# trait A2: AWrapper<AWrapped = Aw2<Self>> {
|
||||
# fn a2_ref(&self) -> u64;
|
||||
# fn a2_mut(&mut self) -> bool;
|
||||
# fn a2_move(self) -> Self;
|
||||
# }
|
||||
# struct Av1;
|
||||
# struct Av2;
|
||||
# type Aw1<T> = Wrapped<T, Av1>;
|
||||
# type Aw2<T> = Wrapped<T, Av2>;
|
||||
# impl<T: ?Sized + A1> AProxy for Aw1<T> {
|
||||
# type T = T;
|
||||
# fn ap_ref(t: &Self::T) -> u64 { t.a1_ref() }
|
||||
# fn ap_mut(t: &mut Self::T) -> bool { t.a1_mut() }
|
||||
# fn ap_move(t: Self::T) -> Self::T { t.a1_move() }
|
||||
# }
|
||||
# impl<T: ?Sized + A2> AProxy for Aw2<T> {
|
||||
# type T = T;
|
||||
# fn ap_ref(t: &Self::T) -> u64 { t.a2_ref() }
|
||||
# fn ap_mut(t: &mut Self::T) -> bool { t.a2_mut() }
|
||||
# fn ap_move(t: Self::T) -> Self::T { t.a2_move() }
|
||||
# }
|
||||
```
|
14
src/lib.rs
Normal file
14
src/lib.rs
Normal file
@ -0,0 +1,14 @@
|
||||
pub fn add(left: usize, right: usize) -> usize {
|
||||
left + right
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user