Giter Site home page Giter Site logo

kijewski / scoped-futures Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tlowerison/scoped-futures

0.0 0.0 0.0 31 KB

A utility crate for imposing upper bounds on Future lifetimes.

Home Page: https://crates.io/crates/scoped-futures

License: Apache License 2.0

Rust 100.00%

scoped-futures's Introduction

scoped-futures

A utility crate for imposing upper bounds on Future lifetimes. This is especially useful for callbacks that use higher-ranked lifetimes in their return type, where it can prevent 'static bounds from being placed on a returned Future.

This crate is effectively a port of Sabrina Jewson's better alternative to lifetime GATs for Futures.

Example

use core::pin::Pin;
use scoped_futures::{ScopedBoxFuture, ScopedFutureExt};

pub struct Db {
    count: u8,
}

impl Db {
    async fn transaction<'a, F, T, E>(&mut self, callback: F) -> Result<T, E>
    where
        // ScopedBoxFuture imposes a lifetime bound on 'b which prevents the hrtb below needing
        // to be satisfied for all lifetimes (including 'static) and instead only lifetimes
        // which live at most as long as 'a
        F: for<'b /* where 'a: 'b */> FnOnce(&'b mut Self) -> ScopedBoxFuture<'a, 'b, Result<T, E>> + Send + 'a,
        T: 'a,
        E: 'a,
    {
        callback(self).await
    }
}`

pub async fn test_transaction<'a, 'b>(
    db: &mut Db,
    ok: &'a str,
    err: &'b str,
    is_ok: bool,
) -> Result<&'a str, &'b str> {
    // note the lack of `move` or any cloning in front of the closure
    db.transaction(|db| async move {
        db.count += 1;
        if is_ok {
            Ok(ok)
        } else {
            Err(err)
        }
    }.scope_boxed()).await?;

    // note that `async` can be used instead of `async move` since the callback param is unused
    db.transaction(|_| async {
        if is_ok {
            Ok(ok)
        } else {
            Err(err)
        }
    }.scope_boxed()).await
}

#[test]
fn test_transaction_works() {
    futures::executor::block_on(async {
        let mut db = Db { count: 0 };
        let ok = String::from("ok");
        let err = String::from("err");
        let result = test_transaction(&mut db, &ok, &err, true).await;
        assert_eq!(ok, result.unwrap());
        assert_eq!(1, db.count);
    })
}

scoped-futures's People

Contributors

treysidechain avatar tlowerison avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.