{"id":16017317,"url":"https://github.com/robertohuertasm/futurify","last_synced_at":"2025-03-18T03:31:01.122Z","repository":{"id":57632758,"uuid":"211735141","full_name":"robertohuertasm/futurify","owner":"robertohuertasm","description":"Converts sync closures into non-thread blocking futures. Supports futures 0.1 \u0026 0.3","archived":false,"fork":false,"pushed_at":"2019-11-24T13:49:31.000Z","size":11,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-15T16:20:09.185Z","etag":null,"topics":["async","asynchronous-programming","crate","futures"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/futurify","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robertohuertasm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-29T22:39:30.000Z","updated_at":"2023-06-04T18:40:03.000Z","dependencies_parsed_at":"2022-08-31T13:01:46.469Z","dependency_job_id":null,"html_url":"https://github.com/robertohuertasm/futurify","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robertohuertasm%2Ffuturify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robertohuertasm%2Ffuturify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robertohuertasm%2Ffuturify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robertohuertasm%2Ffuturify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robertohuertasm","download_url":"https://codeload.github.com/robertohuertasm/futurify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221705395,"owners_count":16866957,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","asynchronous-programming","crate","futures"],"created_at":"2024-10-08T16:05:01.971Z","updated_at":"2024-10-27T16:37:56.921Z","avatar_url":"https://github.com/robertohuertasm.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Futurify\n\n[![ActionsStatus](https://github.com/robertohuertasm/futurify/workflows/Build/badge.svg)](https://github.com/robertohuertasm/futurify/actions) [![Crates.io](https://img.shields.io/crates/v/futurify.svg)](https://crates.io/crates/futurify)\n\nConvert your sync functions into non-blocking thread futures.\n\nSupport for both `Futures 0.1` and `Futures 0.3`. By default, `Futures 0.3` will be used.\n\n## When to use it\n\nThis is just an example to illustrate when this library could be useful.\n\nImagine you're creating an [actix-web](https://github.com/actix/actix-web) API and you have and endpoint that has to execute a very long synchronous task.\n\nNormally, most of the modern crates will provide some `async` variants but still there are some of them that only provide a `sync` implementation. \n\nThis could be problematic as this endpoint could block the main thread and avoid other endpoints from being executed.\n\nTake a look at this code:\n\n```rust\nuse actix_web::{web, App, HttpServer, Responder};\nuse std::time::Duration;\n\nasync fn index_async() -\u003e impl Responder {\n    futures::future::ready(\"Hello world\").await\n}\n\nasync fn index_async_blocking() -\u003e impl Responder {\n    futures::future::lazy(|_| {\n        std::thread::sleep(Duration::from_secs(5));\n        \"Hello blocking world\"\n    })\n    .await\n}\n\nfn main() -\u003e std::io::Result\u003c()\u003e {\n    HttpServer::new(|| {\n        App::new()\n            .route(\"/\", web::get().to_async(index_async))\n            .route(\"/blocking\", web::get().to_async(index_async_blocking))\n    })\n    .workers(1)\n    .bind(\"localhost:8080\")?\n    .run()\n}\n```\n\nIf you try to get `localhost:8080/blocking` and then get `localhost:8080`, you will be able to see that you won't get any response until the `blocking` endpoint has returned.\n\nWith just a slight change we could get rid of this issue:\n\n```rust\nasync fn index_async_non_blocking() -\u003e impl Responder {\n    futurify::wrap(|| {\n        std::thread::sleep(Duration::from_secs(5));\n        \"Hello non blocking world\"\n    })\n}\n\nfn main() -\u003e std::io::Result\u003c()\u003e {\n    HttpServer::new(|| {\n        App::new()\n            .route(\"/\", web::get().to(index_async))\n            .route(\"/blocking\", web::get().to(index_async_blocking))\n            .route(\"/non-blocking\", web::get().to(index_async_non_blocking))\n    })\n    .workers(1)\n    .bind(\"localhost:8080\")?\n    .run()\n}\n````\n\nJust wrap a closure with `futurify::wrap` and you'll get a `futures 0.3` future ready to be polled.\n\n## Futures 0.1 support\n\nIf you need support for `Futures 0.1` just import the library like this in your `Cargo.toml`:\n\n```toml\nfuturify = { version = \"0.3\", features = \"futures_01\", default-features = false }\n```\n\nThat should do the trick!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobertohuertasm%2Ffuturify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobertohuertasm%2Ffuturify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobertohuertasm%2Ffuturify/lists"}