{"id":15566998,"url":"https://github.com/clucompany/enclose","last_synced_at":"2025-04-05T13:02:23.284Z","repository":{"id":57624945,"uuid":"141712616","full_name":"clucompany/Enclose","owner":"clucompany","description":"[stable] A convenient macro, for cloning values into a closure.","archived":false,"fork":false,"pushed_at":"2025-04-03T12:54:57.000Z","size":81,"stargazers_count":29,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T13:02:15.107Z","etag":null,"topics":["clucompany","enclose","enclose-macro","macro","no-std","rust","rustlang"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/clucompany.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE_APACHE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-20T13:01:43.000Z","updated_at":"2025-04-03T12:55:01.000Z","dependencies_parsed_at":"2024-05-18T22:31:15.913Z","dependency_job_id":"f1afbfe5-80bb-4ac5-9b5b-af55c5378fe2","html_url":"https://github.com/clucompany/Enclose","commit_stats":{"total_commits":59,"total_committers":1,"mean_commits":59.0,"dds":0.0,"last_synced_commit":"ddd83aff745ff29a3be9e23768dd0679a8dea373"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clucompany%2FEnclose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clucompany%2FEnclose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clucompany%2FEnclose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clucompany%2FEnclose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/clucompany","download_url":"https://codeload.github.com/clucompany/Enclose/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247339148,"owners_count":20923013,"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":["clucompany","enclose","enclose-macro","macro","no-std","rust","rustlang"],"created_at":"2024-10-02T17:09:32.556Z","updated_at":"2025-04-05T13:02:23.276Z","avatar_url":"https://github.com/clucompany.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv id=\"header\" align=\"center\"\u003e\n\n  \u003cb\u003e[enclose]\u003c/b\u003e\n  \n  (A convenient macro, for cloning values into a closure.)\n  \u003c/br\u003e\u003c/br\u003e\n\n\u003cdiv id=\"badges\"\u003e\n  \u003ca href=\"./LICENSE_MIT\"\u003e\n    \u003cimg src=\"https://github.com/UlinProject/img/blob/main/short_32/mit.png?raw=true\" alt=\"mit\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"./LICENSE_APACHE\"\u003e\n    \u003cimg src=\"https://github.com/UlinProject/img/blob/main/short_32/apache2.png?raw=true\" alt=\"apache2\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://crates.io/crates/enclose\"\u003e\n    \u003cimg src=\"https://github.com/UlinProject/img/blob/main/short_32/cratesio.png?raw=true\" alt=\"cratesio\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://docs.rs/enclose\"\u003e\n    \u003cimg src=\"https://github.com/UlinProject/img/blob/main/short_32/docrs.png?raw=true\" alt=\"docrs\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/denisandroid\"\u003e\n    \u003cimg src=\"https://github.com/UlinProject/img/blob/main/short_32/uproject.png?raw=true\" alt=\"uproject\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/clucompany\"\u003e\n    \u003cimg src=\"https://github.com/UlinProject/img/blob/main/short_32/clulab.png?raw=true\" alt=\"clulab\"/\u003e\n  \u003c/a\u003e\n\t\n  [![CI](https://github.com/clucompany/enclose/actions/workflows/CI.yml/badge.svg?event=push)](https://github.com/clucompany/enclose/actions/workflows/CI.yml) \n\n\n\u003c/div\u003e\n\u003c/div\u003e\n\n## Usage\n\nAdd this to your Cargo.toml:\n\n```toml\n[dependencies]\nenclose = \"1.2.1\"\n```\n\nand this to your source code:\n\n```rust\nuse enclose::enclose;\n```\n\n## Example\n\n### EasyUse\n\nJust use it!\n\n```rust\nuse enclose::enclose;\n\nfn main() {\n\tlet clone_data = 0;\n\tlet add_data = 100;\n\t\n\tmy_enclose( enclose!((mut clone_data, add_data) || {\n\t\t// (mut clone_data, add_data) -\u003e\n\t\t// let mut clone_data = clone_data.clone();\n\t\t// let add_data = add_data.clone();\n\t\t\n\t\tprintln!(\"#0 {:?}\", clone_data);\n\t\tclone_data += add_data;\n\t\tprintln!(\"#1 {:?}\", clone_data);\n\t\t\n\t\tassert_eq!(clone_data, 100);\n\t}));\n\t\n\tassert_eq!(clone_data, 0);\n}\n\nfn my_enclose\u003cF: FnOnce() -\u003e R, R\u003e(a: F) -\u003e R {\n\ta()\n}\n```\n\n### MutexUse\n\nCreating closures for a multi-threaded environment, no extra lines!\n\n```rust\nuse std::sync::Arc;\nuse std::sync::Mutex;\nuse std::thread;\n\nuse enclose::enclose;\n\nfn main() {\n\tlet mutex_data = Arc::new(Mutex::new( 0 ));\n\tlet thread = thread::spawn( enclose!((mutex_data =\u003e d) move || {\n\t\t// (mutex_data =\u003e d) -\u003e\n\t\t// let d = mutex_data.clone();\n\t\t\n\t\tlet mut lock = match d.lock() {\n\t\t\tOk(a) =\u003e a,\n\t\t\tErr(e) =\u003e e.into_inner(),\n\t\t};\n\t\t*lock += 1;\n\t}));\n\n\tthread.join().unwrap();\n\t{\n\t\tlet lock = match mutex_data.lock() {\n\t\t\tOk(a) =\u003e a,\n\t\t\tErr(e) =\u003e e.into_inner(),\n\t\t};\n\t\tassert_eq!(*lock, 1);\n\t}\n}\n```\n\n### ArcMutexUse\n\nA more complex example of using an enclose macro in a multi-threaded environment.\n\n```rust\nuse std::sync::Arc;\nuse std::sync::Mutex;\nuse std::sync::RwLock;\nuse std::thread;\n\nuse enclose::enclose;\n\nfn main() {\n\tlet data1 = Arc::new(Mutex::new( 0 ));\n\tlet data2 = Arc::new(RwLock::new( (0, 2, 3, 4) ));\n\n\tlet count_thread = 5;\n\tlet mut waits = Vec::with_capacity(count_thread);\n\n\tfor _a in 0..count_thread {\n\t\twaits.push({\n\t\t\tthread::spawn( enclose!((data1, data2) move || {\n\t\t\t\t// (data1, data2) -\u003e \n\t\t\t\t// let data1 = data1.clone();\n\t\t\t\t// let data2 = data2.clone();\n\t\t\t\t\n\t\t\t\tlet mut v_lock = match data1.lock() {\n\t\t\t\t\tOk(a) =\u003e a,\n\t\t\t\t\tErr(e) =\u003e e.into_inner(),\n\t\t\t\t};\n\t\t\t\t*v_lock += 1;\n\n\t\t\t\tdrop( data2 ); // ignore warning\n\t\t\t}))\n\t\t});\n\t}\n\tfor a in waits {\n\t\ta.join().unwrap();\n\t}\n\t\n\t\n\t{\t\n\t\t// Check data1_lock\n\t\tlet data1_lock = match data1.lock() {\n\t\t\tOk(a) =\u003e a,\n\t\t\tErr(e) =\u003e e.into_inner(),\n\t\t};\n\t\tassert_eq!(*data1_lock, 5);\n\t}\n\t\n\t{\t\n\t\t// Check data2_lock\n\t\tlet data2_lock = match data2.write() {\n\t\t\tOk(a) =\u003e a,\n\t\t\tErr(e) =\u003e e.into_inner(),\n\t\t};\n\t\tassert_eq!(*data2_lock, (0, 2, 3, 4));\n\t}\n}\n```\n\n### EasyCopy\n\nUsing copy instead of clone.\n\n```rust\nuse enclose::enclose;\nuse std::sync::Arc;\n\nfn main() {\n\tlet clone_data = Arc::new(0);\n\tlet add_data = Arc::new(100);\n\t\n\tmy_enclose( enclose!((mut *clone_data, *add_data) || {\n\t\t// (mut *clone_data, *add_data)\n\t\t// let mut clone_data = *clone_data;\n\t\t// let add_data = *add_data;\n\t\t\n\t\tprintln!(\"#0 {:?}\", clone_data);\n\t\tclone_data += add_data;\n\t\tprintln!(\"#1 {:?}\", clone_data);\n\t\t\n\t\tassert_eq!(clone_data, 100);\n\t}));\n\t\n\tassert_eq!(*clone_data, 0);\n}\n\nfn my_enclose\u003cF: FnOnce() -\u003e R, R\u003e(a: F) -\u003e R {\n\ta()\n}\n```\n\n\u003ca href=\"./examples\"\u003e\n  See all\n\u003c/a\u003e\n\n## License\n\nThis project has a dual license according to (LICENSE-MIT) and (LICENSE-APACHE-2-0).\n\n\u003cdiv align=\"left\"\u003e\n  \u003ca href=\"https://github.com/denisandroid\"\u003e\n    \u003cimg align=\"left\" src=\"https://github.com/UlinProject/img/blob/main/block_220_100/uproject.png?raw=true\" alt=\"uproject\"/\u003e\n  \u003c/a\u003e\n  \u003cb\u003e\u0026nbsp;Copyright (c) 2019-2025 #UlinProject\u003c/b\u003e\n\t\n  \u003cb\u003e\u0026nbsp;(Denis Kotlyarov).\u003c/b\u003e\n  \u003c/br\u003e\u003c/br\u003e\u003c/br\u003e\n\u003c/div\u003e\n\n### Apache License\n\n\u003cdiv align=\"left\"\u003e\n  \u003ca href=\"./LICENSE_APACHE\"\u003e\n    \u003cimg align=\"left\" src=\"https://github.com/UlinProject/img/blob/main/block_220_100/apache2.png?raw=true\" alt=\"apache2\"/\u003e\n    \n  \u003c/a\u003e\n  \u003cb\u003e\u0026nbsp;Licensed under the Apache License, Version 2.0.\u003c/b\u003e\n  \u003c/br\u003e\u003c/br\u003e\u003c/br\u003e\u003c/br\u003e\n\u003c/div\u003e\n\n### MIT License\n\n\u003cdiv align=\"left\"\u003e\n  \u003ca href=\"./LICENSE_MIT\"\u003e\n    \u003cimg align=\"left\" src=\"https://github.com/UlinProject/img/blob/main/block_220_100/mit.png?raw=true\" alt=\"mit\"/\u003e\n  \u003c/a\u003e\n  \u003cb\u003e\u0026nbsp;Licensed under the MIT License.\u003c/b\u003e\n  \u003c/br\u003e\u003c/br\u003e\u003c/br\u003e\u003c/br\u003e\n\u003c/div\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclucompany%2Fenclose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclucompany%2Fenclose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclucompany%2Fenclose/lists"}