{"id":15628096,"url":"https://github.com/maciejhirsz/beef","last_synced_at":"2025-05-15T15:04:57.682Z","repository":{"id":39710167,"uuid":"246857667","full_name":"maciejhirsz/beef","owner":"maciejhirsz","description":"Faster, more compact implementation of std::borrow::Cow","archived":false,"fork":false,"pushed_at":"2023-04-13T18:42:07.000Z","size":68,"stargazers_count":345,"open_issues_count":14,"forks_count":16,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-31T19:09:53.928Z","etag":null,"topics":["cursed","no-std","rust","rust-lang"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/beef","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/maciejhirsz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE-APACHE","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},"funding":{"github":["maciejhirsz"]}},"created_at":"2020-03-12T14:39:45.000Z","updated_at":"2025-03-30T19:48:02.000Z","dependencies_parsed_at":"2024-06-18T18:23:00.254Z","dependency_job_id":"657b8e12-b998-4bb1-99d7-e45353db40ed","html_url":"https://github.com/maciejhirsz/beef","commit_stats":{"total_commits":47,"total_committers":15,"mean_commits":"3.1333333333333333","dds":0.574468085106383,"last_synced_commit":"1cc0a4a944b9ed01b190560cd02fd6bdc5a0f9e6"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maciejhirsz%2Fbeef","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maciejhirsz%2Fbeef/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maciejhirsz%2Fbeef/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maciejhirsz%2Fbeef/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maciejhirsz","download_url":"https://codeload.github.com/maciejhirsz/beef/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247721898,"owners_count":20985084,"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":["cursed","no-std","rust","rust-lang"],"created_at":"2024-10-03T10:20:50.172Z","updated_at":"2025-04-07T20:10:13.029Z","avatar_url":"https://github.com/maciejhirsz.png","language":"Rust","funding_links":["https://github.com/sponsors/maciejhirsz"],"categories":[],"sub_categories":[],"readme":"# beef\n\n[![Travis shield](https://travis-ci.org/maciejhirsz/beef.svg)](https://travis-ci.org/maciejhirsz/beef)\n[![Crates.io version shield](https://img.shields.io/crates/v/beef.svg)](https://crates.io/crates/beef)\n[![Crates.io license shield](https://img.shields.io/crates/l/beef.svg)](https://crates.io/crates/beef)\n\nFaster, more compact implementation of `Cow`.\n\n**[Changelog](https://github.com/maciejhirsz/beef/releases) -**\n**[Documentation](https://docs.rs/beef/) -**\n**[Cargo](https://crates.io/crates/beef) -**\n**[Repository](https://github.com/maciejhirsz/beef)**\n\n```rust\nuse beef::Cow;\n\nlet borrowed: Cow\u003cstr\u003e = Cow::borrowed(\"Hello\");\nlet owned: Cow\u003cstr\u003e = Cow::owned(String::from(\"World\"));\n\nassert_eq!(\n    format!(\"{} {}!\", borrowed, owned),\n    \"Hello World!\",\n);\n```\n\nThere are two versions of `Cow` exposed by this crate:\n\n+ `beef::Cow` is 3 words wide: pointer, length, and capacity. It stores the ownership tag in capacity.\n+ `beef::lean::Cow` is 2 words wide, storing length, capacity, and the ownership tag all in one word.\n\nBoth versions are leaner than the `std::borrow::Cow`:\n\n```rust\nuse std::mem::size_of;\n\nconst WORD: usize = size_of::\u003cusize\u003e();\n\nassert_eq!(size_of::\u003cstd::borrow::Cow\u003cstr\u003e\u003e(), 4 * WORD);\nassert_eq!(size_of::\u003cbeef::Cow\u003cstr\u003e\u003e(), 3 * WORD);\nassert_eq!(size_of::\u003cbeef::lean::Cow\u003cstr\u003e\u003e(), 2 * WORD);\n```\n\n## How does it work?\n\nThe standard library `Cow` is an enum with two variants:\n\n```rust\npub enum Cow\u003c'a, B\u003e where\n    B: 'a + ToOwned + ?Sized,\n{\n    Borrowed(\u0026'a B),\n    Owned(\u003cB as ToOwned\u003e::Owned),\n}\n```\n\nFor the most common pairs of values - `\u0026str` and `String`, or `\u0026[u8]` and `Vec\u003cu8\u003e` - this\nmeans that the entire enum is 4 words wide:\n\n```text\n                                                 Padding\n                                                    |\n                                                    v\n          +-----------+-----------+-----------+-----------+\nBorrowed: | Tag       | Pointer   | Length    | XXXXXXXXX |\n          +-----------+-----------+-----------+-----------+\n\n          +-----------+-----------+-----------+-----------+\nOwned:    | Tag       | Pointer   | Length    | Capacity  |\n          +-----------+-----------+-----------+-----------+\n```\n\nInstead of being an enum with a tag, `beef::Cow` uses capacity to determine whether the\nvalue it's holding is owned (capacity is greater than 0), or borrowed (capacity is 0).\n\n`beef::lean::Cow` goes even further and puts length and capacity on a single 64 word.\n\n```text\n                 +-----------+-----------+-----------+\nbeef::Cow        | Pointer   | Length    | Capacity? |\n                 +-----------+-----------+-----------+\n\n                 +-----------+-----------+\nbeef::lean::Cow  | Pointer   | Cap | Len |\n                 +-----------+-----------+\n```\n\nAny owned `Vec` or `String` that has 0 capacity is effectively treated as a borrowed\nvalue. Since having no capacity means there is no actual allocation behind the pointer,\nthis is safe.\n\n## Benchmarks\n\n```\ncargo +nightly bench\n```\n\nMicrobenchmarking obtaining a `\u0026str` reference is rather flaky and you can have widely different results. In general the following seems to hold true:\n\n+ `beef::Cow` and `beef::lean::Cow` are faster than `std::borrow::Cow` at obtaining a reference `\u0026T`. This makes sense since we avoid the enum tag branching.\n+ The 3-word `beef::Cow` is faster at creating borrowed variants, but slower at creating owned variants than `std::borrow::Cow`.\n+ The 2-word `beef::lean::Cow` is faster at both.\n\n```\nrunning 9 tests\ntest beef_as_ref            ... bench:          57 ns/iter (+/- 15)\ntest beef_create            ... bench:         135 ns/iter (+/- 5)\ntest beef_create_mixed      ... bench:         659 ns/iter (+/- 52)\ntest lean_beef_as_ref       ... bench:          50 ns/iter (+/- 2)\ntest lean_beef_create       ... bench:          77 ns/iter (+/- 3)\ntest lean_beef_create_mixed ... bench:         594 ns/iter (+/- 52)\ntest std_as_ref             ... bench:          70 ns/iter (+/- 6)\ntest std_create             ... bench:         142 ns/iter (+/- 7)\ntest std_create_mixed       ... bench:         663 ns/iter (+/- 32)\n```\n\n## License\n\nThis crate is distributed under the terms of both the MIT license\nand the Apache License (Version 2.0). Choose whichever one works best for you.\n\nSee [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaciejhirsz%2Fbeef","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaciejhirsz%2Fbeef","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaciejhirsz%2Fbeef/lists"}