Skip to content

Commit

Permalink
feat: async higher order fn
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandervantrijffel committed Oct 24, 2024
1 parent fdc86e6 commit 76d9c4c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "src/lib.rs"
doctest = false

[dependencies]
tokio = { version = "1.40", default-features= false, features = ["macros", "rt-multi-thread" ] }
unicode-segmentation = "1"

[profile.release]
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Rust gems

This repository offers some Rust snippets that might be useful when studying the language.
This repository offers some Rust snippets that can be useful when studying the language.

- [vec_any](src/vec_any.rs) Collect trait objects in a vector and use downcast_ref to access the concrete type instances of the items
- [mutate_in_closure](src/mutate_in_closure.rs) Mutate a value in a closure without copy and clone
- [mutate_in_closure](src/mutate_in_closure.rs) Mutate a value in a closure without copy and clone by using Rc
- [async_higher_order_fn](src/async_higher_order_fn.rs) Implement an async higher order function that accept an async closures and returns an async result
- [from_str](src/from_str.rs) Thou shall not implement From\<str'> but instead implement the FromStr trait
- [graphemes](src/graphemes.rs) Trim an unicode string to a maximum length with

Expand Down
46 changes: 46 additions & 0 deletions src/async_higher_order_fn.rs
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>>> {

Check warning on line 13 in src/async_higher_order_fn.rs

View workflow job for this annotation

GitHub Actions / validate

this argument is passed by value, but not consumed in the function body
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>> {

Check warning on line 18 in src/async_higher_order_fn.rs

View workflow job for this annotation

GitHub Actions / validate

unused `async` for function with no await statements
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);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
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;
Expand Down

0 comments on commit 76d9c4c

Please sign in to comment.