{"id":21358643,"url":"https://github.com/alinuxperson/try-drop","last_synced_at":"2025-07-13T00:34:07.067Z","repository":{"id":43808150,"uuid":"456342741","full_name":"ALinuxPerson/try-drop","owner":"ALinuxPerson","description":"Batteries included error handling mechanisms for drops which can fail","archived":false,"fork":false,"pushed_at":"2022-02-18T01:55:25.000Z","size":544,"stargazers_count":10,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-14T18:11:43.784Z","etag":null,"topics":["batteries-included","crate","drop","drops","error","error-handling","fallible-drop","fallible-drops","library","no-std","rust","rust-crate","rust-lang","rust-library","try-drop","try-drops","utilities"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/try-drop","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/ALinuxPerson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-07T03:17:52.000Z","updated_at":"2023-04-15T12:11:22.000Z","dependencies_parsed_at":"2022-09-26T20:41:14.428Z","dependency_job_id":null,"html_url":"https://github.com/ALinuxPerson/try-drop","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ALinuxPerson%2Ftry-drop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ALinuxPerson%2Ftry-drop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ALinuxPerson%2Ftry-drop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ALinuxPerson%2Ftry-drop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ALinuxPerson","download_url":"https://codeload.github.com/ALinuxPerson/try-drop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225846553,"owners_count":17533482,"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":["batteries-included","crate","drop","drops","error","error-handling","fallible-drop","fallible-drops","library","no-std","rust","rust-crate","rust-lang","rust-library","try-drop","try-drops","utilities"],"created_at":"2024-11-22T05:20:54.078Z","updated_at":"2024-11-22T05:20:54.757Z","avatar_url":"https://github.com/ALinuxPerson.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n    \u003ch1\u003e\u003cb\u003etry-drop\u003c/b\u003e\u003c/h1\u003e\n    \u003ca href=\"https://www.crates.io/crates/try-drop\"\u003e\n        \u003cimg src=\"https://img.shields.io/crates/v/try-drop\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://www.docs.rs/try-drop\"\u003e\n        \u003cimg src=\"https://docs.rs/try-drop/badge.svg\"\u003e\n    \u003c/a\u003e\n    \u003cp\u003eBatteries included error handling mechanisms for drops which can fail\u003c/p\u003e\n\u003c/div\u003e\n\n# Quick Usage\n## For Clients\n...where clients mean structures that may fail dropping,\n\nImplement `TryDrop` for your type and `adapt` it like so:\n\n```rust\nuse try_drop::TryDrop;\n\npub struct Foo { /* fields */ }\n\nimpl TryDrop for Foo {\n    type Error = Error;\n    \n    unsafe fn try_drop(\u0026mut self) -\u003e Result\u003c(), Self::Error\u003e {\n        // do stuff\n        Ok(())\n    }\n}\n\nlet foo = Foo.adapt();\n```\n\n...or, if you want to avoid the `adapt` boilerplate:\n\n```rust\nuse try_drop::{TryDrop, adapters::DropAdapter};\n\npub struct FooInner { /* fields */ }\n\nimpl TryDrop for FooInner {\n    type Error = Error;\n    \n    unsafe fn try_drop(\u0026mut self) -\u003e Result\u003c(), Self::Error\u003e {\n        // do stuff\n        Ok(())\n    }\n}\n\npub struct Foo(pub DropAdapter\u003cFooInner\u003e);\n\nimpl Foo {\n    pub fn from_inner(inner: FooInner) -\u003e Self {\n        Foo(DropAdapter(inner))\n    }\n}\n```\n\nWe must adapt it because implementing `TryDrop` doesn't also implement `Drop` with your type. This is due to the orphan \nrules and the fact that `Drop` can only be implemented for structs, enums, and unions, but not generics.  With this, if\ndropping `Foo` fails, it will automatically print the error to standard error.\n\n## For Servers\n...where servers mean how to handle the drop errors (also means drop strategies),\n\nImplement `TryDropStrategy` for your structure.\n\n```rust\nuse try_drop::TryDropStrategy;\n\npub struct Strategy { /* fields */ }\n\nimpl TryDropStrategy for Strategy {\n    fn handle_error(\u0026self, error: try_drop::Error) {\n        // handle the error here\n    }\n}\n```\n\n...then install either for this thread,\n\n```rust\ntry_drop::install_thread_local_handlers(Strategy, /* other strategy, use the `PanicDropStrategy` if you don't know */)\n```\n\n...install it globally (meaning if no thread local strategies are set, use this),\n\n```rust\ntry_drop::install_global_handlers(Strategy, /* other strategy */)\n```\n\n...or, if possible, install it for a structure.\n\n```rust\nstruct Sample\u003cD = ShimPrimaryHandler, DD = ShimFallbackHandler\u003e\nwhere\n    D: FallibleTryDropStrategy,\n    DD: TryDropStrategy,\n{\n    primary: D,\n    fallback: DD,\n    /* other fields */\n}\n\nimpl\u003cD, DD\u003e Sample\u003cD, DD\u003e\nwhere\n    D: FallibleTryDropStrategy,\n    DD: TryDropStrategy,\n{\n    pub fn new_with(/* other arguments */ primary: D, fallback: DD) -\u003e Self {\n        Self {\n            // filled arguments\n            primary,\n            fallback,\n        }\n    }\n}\n\nlet sample = Sample::new_with(Strategy, /* other strategy */)\n```\n\n# License\n\nThis project is licensed under the MIT License.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falinuxperson%2Ftry-drop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falinuxperson%2Ftry-drop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falinuxperson%2Ftry-drop/lists"}