-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdc86e6
commit 76d9c4c
Showing
4 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#![allow(dead_code, unused)] | ||
|
||
use std::{error::Error, future::Future, pin::Pin}; | ||
|
||
/// function that accepts an async function handler | ||
fn higher_order_function(f: FunctionHandler, data: Data) -> Pin<Box<dyn Future<Output = Response>>> { | ||
f(data) | ||
} | ||
|
||
pub type FunctionHandler = &'static (dyn Fn(Data) -> Pin<Box<dyn Future<Output = Response>>>); | ||
pub type Response = Result<ResponseData, Box<dyn Error>>; | ||
|
||
fn my_function_handler(data: Data) -> Pin<Box<dyn Future<Output = Response>>> { | ||
Box::pin(async move { async_dummy().await }) | ||
} | ||
|
||
/// Dummy async function to demonstrate calling a async function from a async higher order function | ||
async fn async_dummy() -> Result<ResponseData, Box<dyn Error>> { | ||
Ok(ResponseData { processed: true }) | ||
} | ||
|
||
pub struct Data { | ||
title: String, | ||
} | ||
|
||
pub struct ResponseData { | ||
processed: bool, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_call_it() { | ||
let response = higher_order_function( | ||
&my_function_handler, | ||
Data { | ||
title: "Hello".to_string(), | ||
}, | ||
) | ||
.await; | ||
assert!(response.is_ok()); | ||
assert!(response.unwrap().processed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod async_higher_order_fn; | ||
pub mod from_str; | ||
pub mod graphemes; | ||
pub mod mutate_in_closure; | ||
|