chore: reorg
This commit is contained in:
5
__std/into/Cargo.lock
generated
Normal file
5
__std/into/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "into"
|
||||
version = "0.1.0"
|
||||
9
__std/into/Cargo.toml
Normal file
9
__std/into/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "into"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang@Pixelbook <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
53
__std/into/src/main.rs
Normal file
53
__std/into/src/main.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
use std::convert::{
|
||||
TryInto,
|
||||
TryFrom,
|
||||
};
|
||||
|
||||
type BoxError = Box<dyn std::error::Error>;
|
||||
type XResult<T> = Result<T, BoxError>;
|
||||
|
||||
fn do_print<T>(t: T) -> XResult<()> where T: TryInto<String>, T::Error: Into<BoxError> {
|
||||
println!("--> {}", t.try_into().map_err(Into::into)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn do_print2<T>(t: T) where T: Into<String> {
|
||||
println!("==> {}", t.into())
|
||||
}
|
||||
|
||||
struct StructX {
|
||||
name: String,
|
||||
}
|
||||
|
||||
struct StructY {
|
||||
name: String,
|
||||
}
|
||||
|
||||
// auto have: TryInto<String> for StructX
|
||||
impl TryFrom<StructX> for String {
|
||||
type Error = BoxError;
|
||||
|
||||
fn try_from(sx: StructX) -> XResult<Self> {
|
||||
Ok(format!("NAME: {}", sx.name))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StructY> for String {
|
||||
|
||||
fn from(sx: StructY) -> Self {
|
||||
format!("NAME:: {}", sx.name)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> XResult<()> {
|
||||
let x = StructX {
|
||||
name: "hater".to_owned(),
|
||||
};
|
||||
do_print(x)?;
|
||||
let y = StructY {
|
||||
name: "hater".to_owned(),
|
||||
};
|
||||
do_print2(y);
|
||||
Ok(())
|
||||
}
|
||||
5
__std/iter/Cargo.lock
generated
Normal file
5
__std/iter/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "iter"
|
||||
version = "0.1.0"
|
||||
9
__std/iter/Cargo.toml
Normal file
9
__std/iter/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "iter"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
77
__std/iter/src/main.rs
Normal file
77
__std/iter/src/main.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
struct Seq {
|
||||
start: i32,
|
||||
end: i32,
|
||||
}
|
||||
|
||||
impl Seq {
|
||||
pub fn new(start: i32, end: i32) -> Self {
|
||||
Self { start, end, }
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Seq {
|
||||
type Item = i32;
|
||||
|
||||
fn next(&mut self) -> Option<i32> {
|
||||
if self.start < self.end {
|
||||
let a = self.start;
|
||||
self.start += 1;
|
||||
Some(a)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
test_001();
|
||||
test_002();
|
||||
test_003();
|
||||
test_004();
|
||||
}
|
||||
|
||||
// -> 10 11 12 13 14
|
||||
fn test_001() {
|
||||
println!("## test001");
|
||||
let seq = Seq::new(10, 15);
|
||||
print!("->");
|
||||
for a in seq {
|
||||
print!(" {}", a);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
// -> 10 11 12 13 14
|
||||
fn test_002() {
|
||||
println!("## test002");
|
||||
let mut seq = Seq::new(10, 15);
|
||||
print!("->");
|
||||
while let Some(a) = seq.next() {
|
||||
print!(" {}", a);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
// -> 10 11 12
|
||||
fn test_003() {
|
||||
println!("## test003");
|
||||
let mut seq = Seq::new(10, 15).take(3);
|
||||
print!("->");
|
||||
while let Some(a) = seq.next() {
|
||||
print!(" {}", a);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
// -> 10
|
||||
// -> 10 11 12 13 14
|
||||
fn test_004() {
|
||||
println!("## test004");
|
||||
let mut seq = Seq::new(10, 15).peekable();
|
||||
println!("-> {}", seq.peek().unwrap());
|
||||
print!("->");
|
||||
while let Some(a) = seq.next() {
|
||||
print!(" {}", a);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
6
__std/ops/Cargo.lock
generated
Normal file
6
__std/ops/Cargo.lock
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "ops-test"
|
||||
version = "0.1.0"
|
||||
|
||||
9
__std/ops/Cargo.toml
Normal file
9
__std/ops/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "ops-test"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
64
__std/ops/src/main.rs
Normal file
64
__std/ops/src/main.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
#[derive(Debug, Clone)]
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
impl std::ops::Add<Point> for Point {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Self) -> Self {
|
||||
Point {
|
||||
x: self.x + rhs.x,
|
||||
y: self.y + rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub<Point> for Point {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self {
|
||||
Point {
|
||||
x: self.x - rhs.x,
|
||||
y: self.y - rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Mul<Point> for Point {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, rhs: Self) -> Self {
|
||||
Point {
|
||||
x: self.x * rhs.x,
|
||||
y: self.y * rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Div<Point> for Point {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, rhs: Self) -> Self {
|
||||
Point {
|
||||
x: self.x / rhs.x,
|
||||
y: self.y / rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = Point { x: 2, y: 3, };
|
||||
let b = Point { x: 4, y: 5, };
|
||||
let c = a.clone() + b.clone();
|
||||
let d = a.clone() - b.clone();
|
||||
let e = a.clone() * b.clone();
|
||||
let f = a.clone() / b.clone();
|
||||
println!("{:?}", a);
|
||||
println!("{:?}", b);
|
||||
println!("{:?}", c);
|
||||
println!("{:?}", d);
|
||||
println!("{:?}", e);
|
||||
println!("{:?}", f);
|
||||
}
|
||||
5
__std/thread/Cargo.lock
generated
Normal file
5
__std/thread/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "thread-test"
|
||||
version = "0.1.0"
|
||||
9
__std/thread/Cargo.toml
Normal file
9
__std/thread/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "thread-test"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang@Pixelbook <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
38
__std/thread/src/main.rs
Normal file
38
__std/thread/src/main.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use std::{
|
||||
thread,
|
||||
sync::{ Arc, Mutex, },
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
fn sleep_sort(in_vec: Vec<u8>) -> Vec<u8> {
|
||||
let out_vec = Arc::new(Mutex::new(vec![]));
|
||||
|
||||
let mut all_threads = vec![];
|
||||
for i in in_vec {
|
||||
let o_vec = out_vec.clone();
|
||||
let child = thread::spawn(move || {
|
||||
thread::sleep(Duration::from_millis(i as u64));
|
||||
let mut v = o_vec.lock().unwrap();
|
||||
v.push(i);
|
||||
});
|
||||
all_threads.push(child);
|
||||
}
|
||||
for t in all_threads {
|
||||
t.join().ok();
|
||||
}
|
||||
|
||||
let v = out_vec.lock().unwrap();
|
||||
v.to_vec()
|
||||
}
|
||||
|
||||
// sleep sort
|
||||
fn main() {
|
||||
let in_vec = vec![2, 1, 5, 10, 7];
|
||||
let out_vec = sleep_sort(in_vec);
|
||||
|
||||
out_vec.iter().for_each(|i| println!("{}", i));
|
||||
|
||||
println!("Finished!");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user