Compare commits
2 Commits
4b16a23712
...
main
Author | SHA1 | Date | |
---|---|---|---|
32595d41bb | |||
d16f8e333e |
@ -1,3 +1,6 @@
|
|||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
// Sealed with Magistamp.
|
||||||
|
|
||||||
//! An example job execution framework.
|
//! An example job execution framework.
|
||||||
//!
|
//!
|
||||||
//! It includes:
|
//! It includes:
|
||||||
@ -8,6 +11,7 @@
|
|||||||
//! Jobs are run concurrently using Tokio, and results are gathered
|
//! Jobs are run concurrently using Tokio, and results are gathered
|
||||||
//! with proper error handling.
|
//! with proper error handling.
|
||||||
|
|
||||||
|
use std::io::Write;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
use tokio::time::{sleep, Duration};
|
use tokio::time::{sleep, Duration};
|
||||||
@ -159,7 +163,16 @@ impl Job for FileJob
|
|||||||
where Self: Sync + 'a
|
where Self: Sync + 'a
|
||||||
{
|
{
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
Ok(String::from("Reading file"))
|
let mut write_target = std::env::temp_dir();
|
||||||
|
write_target.push("file_job");
|
||||||
|
write_target.set_extension("txt");
|
||||||
|
|
||||||
|
let mut file = std::fs::File::create(write_target)
|
||||||
|
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?;
|
||||||
|
file.write_all(b"Jason is an awesome programmer!!")
|
||||||
|
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?;
|
||||||
|
|
||||||
|
Ok(String::from("File written"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -229,6 +242,7 @@ impl Job for MathJob
|
|||||||
where Self: Sync + 'a
|
where Self: Sync + 'a
|
||||||
{
|
{
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
|
let _ans = 10 * 4 + 2;
|
||||||
Ok(String::from("Math stuff"))
|
Ok(String::from("Math stuff"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -251,7 +265,7 @@ mod tests
|
|||||||
|
|
||||||
let result = job.run().await;
|
let result = job.run().await;
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert_eq!(result.unwrap(), "Reading file");
|
assert_eq!(result.unwrap(), "File written");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@ -299,7 +313,7 @@ mod tests
|
|||||||
assert_eq!(results.len(), 3);
|
assert_eq!(results.len(), 3);
|
||||||
|
|
||||||
assert_eq!(results[0].0, "File Job");
|
assert_eq!(results[0].0, "File Job");
|
||||||
assert_eq!(results[0].1.as_ref().unwrap(), "Reading file");
|
assert_eq!(results[0].1.as_ref().unwrap(), "File written");
|
||||||
|
|
||||||
assert_eq!(results[1].0, "Math Job");
|
assert_eq!(results[1].0, "Math Job");
|
||||||
assert_eq!(results[1].1.as_ref().unwrap(), "Math stuff");
|
assert_eq!(results[1].1.as_ref().unwrap(), "Math stuff");
|
||||||
|
28
src/basic.rs
28
src/basic.rs
@ -52,20 +52,26 @@ pub fn longest<'a>(strings: &'a [String]) -> Option<&'a str>
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Imperative programming style.
|
||||||
|
//
|
||||||
|
// ```Rust
|
||||||
// This is the current longest string. (index, length)
|
// This is the current longest string. (index, length)
|
||||||
|
// let mut longest: (usize, usize) = (0, 0);
|
||||||
|
// for (index, string) in strings.iter().enumerate()
|
||||||
|
// {
|
||||||
|
// if string.len() > longest.1
|
||||||
|
// {
|
||||||
|
// longest = (index, string.len());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Some(&strings[longest.0])
|
||||||
|
// ```
|
||||||
//
|
//
|
||||||
// This can also be done with functional style:
|
// This can also be done with functional style:
|
||||||
// strings.iter().max_by_key(|s| s.len()).map(|s| s.as_str())
|
// When coding prefer the functional, declarative style.
|
||||||
let mut longest: (usize, usize) = (0, 0);
|
// It is just as fast or faster than the imperative style.
|
||||||
for (index, string) in strings.iter().enumerate()
|
strings.iter().max_by_key(|s| s.len()).map(|s| s.as_str())
|
||||||
{
|
|
||||||
if string.len() > longest.1
|
|
||||||
{
|
|
||||||
longest = (index, string.len());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(&strings[longest.0])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user