{"id":15673335,"url":"https://github.com/koivunej/derive-into-owned","last_synced_at":"2025-04-11T08:18:09.693Z","repository":{"id":21473811,"uuid":"92837560","full_name":"koivunej/derive-into-owned","owner":"koivunej","description":"Procedural macro for deriving helper methods for types with Cow fields","archived":false,"fork":false,"pushed_at":"2024-05-07T17:56:34.000Z","size":34,"stargazers_count":16,"open_issues_count":6,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T15:05:55.935Z","etag":null,"topics":["derive","procedural-macros","rust"],"latest_commit_sha":null,"homepage":"","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/koivunej.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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":"2017-05-30T13:47:36.000Z","updated_at":"2024-12-24T17:29:05.000Z","dependencies_parsed_at":"2022-09-16T12:10:59.891Z","dependency_job_id":"15a97aa5-8ed3-4c4d-a5be-e54b05d39111","html_url":"https://github.com/koivunej/derive-into-owned","commit_stats":{"total_commits":41,"total_committers":4,"mean_commits":10.25,"dds":0.2195121951219512,"last_synced_commit":"84adb29abe06645c1f1da3a0701f07a29d91dbad"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koivunej%2Fderive-into-owned","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koivunej%2Fderive-into-owned/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koivunej%2Fderive-into-owned/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koivunej%2Fderive-into-owned/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koivunej","download_url":"https://codeload.github.com/koivunej/derive-into-owned/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248360718,"owners_count":21090746,"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":["derive","procedural-macros","rust"],"created_at":"2024-10-03T15:39:41.266Z","updated_at":"2025-04-11T08:18:09.671Z","avatar_url":"https://github.com/koivunej.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# derive-into-owned\n\n[![Build Status](https://travis-ci.org/koivunej/derive-into-owned.svg?branch=master)](https://travis-ci.org/koivunej/derive-into-owned)\n[![crates.io](https://img.shields.io/crates/v/derive-into-owned.svg)](https://crates.io/crates/derive-into-owned)\n[![docs.rs](https://docs.rs/derive-into-owned/badge.svg)](https://docs.rs/derive-into-owned/)\n\nRust procedural macros for deriving methods to help with working with types that contain [`Cow`](https://doc.rust-lang.org/std/borrow/enum.Cow.html) fields.\nPlease note that this derive somewhat strangely works with duck-typing, at least for now.\nIt was originally created to help me reduce the boilerplate with `Cow` types.\n\n`[derive(IntoOwned)]` generates a method similar to:\n\n```rust\nuse std::borrow::Cow;\n\nstruct Foo\u003c'a\u003e {\n\tfield: Cow\u003c'a, str\u003e,\n}\n\nimpl\u003c'a\u003e Foo\u003c'a\u003e {\n\t/// This method would be derived using #[derive(IntoOwned)]\n\tpub fn into_owned(self) -\u003e Foo\u003c'static\u003e {\n\t\tFoo {\n\t\t\tfield: Cow::Owned(self.field.into_owned()),\n\t\t}\n\t}\n}\n```\n\nOriginally based off of [deep-clone-derive](https://github.com/asajeffrey/deep-clone/blob/master/deep-clone-derive/lib.rs) example but supports:\n\n * [tuple structs](./tests/tuple_struct.rs)\n * normal [structs](./tests/struct.rs)\n * enums with tuple variants [tuple enums](./tests/simple_enum.rs)\n * `IntoOwned` alike fields (actually assumes all fields with types with lifetimes are `IntoOwned` alike)\n * [options of Cow or Cow-like types](./tests/opt_field.rs) `Option\u003cCow\u003c'a, str\u003e\u003e` and `Option\u003cFoo\u003c'a\u003e\u003e`\n * [vectors of Cow or Cow-like types](./tests/vec.rs)\n\nBut wait there is even more! `[derive(Borrowed)]` generates a currently perhaps a bit limited version of a method like:\n\n```rust\nimpl\u003c'a\u003e Foo\u003c'a\u003e {\n\tpub fn borrowed\u003c'b\u003e(\u0026'b self) -\u003e Foo\u003c'b\u003e {\n\t\tFoo {\n\t\t\tfield: Cow::Borrowed(self.field.as_ref()),\n\t\t}\n\t}\n}\n```\n\n## Types with lifetimes\n\nIf your struct has a field with type `Bar\u003c'a\u003e` then `Bar` is assumed to have a method `fn into_owned(self) -\u003e Bar\u003c'static\u003e`.\n\nNote, there's no trait implementation expected because I didn't find one at the time and didn't think to create my own, assumed the `Cow::into_owned` might be getting an extension in standard library which never happened and so on.\n\n## Limitations\n\nCurrently deriving will fail miserably for at least but not limited to:\n\n * `IntoOwned`: borrowed fields like `\u0026'a str`\n * `Borrowed`: struct/enum has more than one lifetime\n * both: arrays not supported\n * both: into_owned/borrowed types inside tuples inside vectors\n\nUsing with incompatible types results in not so understandable error messages. For example, given a struct:\n\n```rust\n#[derive(IntoOwned)]\nstruct Foo\u003c'a\u003e {\n\tfield: \u0026'a str,\n}\n```\n\nThe compiler error will be:\n\n```\nerror[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements\n --\u003e tests/does_not_compile.rs:4:10\n  |\n4 | #[derive(IntoOwned)]\n  |          ^^^^^^^^^\n  |\nnote: first, the lifetime cannot outlive the lifetime 'a as defined on the impl at 4:10...\n --\u003e tests/does_not_compile.rs:4:10\n  |\n4 | #[derive(IntoOwned)]\n  |          ^^^^^^^^^\nnote: ...so that reference does not outlive borrowed content\n --\u003e tests/does_not_compile.rs:4:10\n  |\n4 | #[derive(IntoOwned)]\n  |          ^^^^^^^^^\n  = note: but, the lifetime must be valid for the static lifetime...\nnote: ...so that expression is assignable (expected Foo\u003c'static\u003e, found Foo\u003c'_\u003e)\n --\u003e tests/does_not_compile.rs:4:10\n  |\n4 | #[derive(IntoOwned)]\n  |          ^^^^^^^^^\nerror: aborting due to previous error(s)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoivunej%2Fderive-into-owned","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoivunej%2Fderive-into-owned","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoivunej%2Fderive-into-owned/lists"}