{"id":27535633,"url":"https://github.com/fujiapple852/fromage","last_synced_at":"2025-09-03T03:36:36.745Z","repository":{"id":286116944,"uuid":"960447151","full_name":"fujiapple852/fromage","owner":"fujiapple852","description":"A cheesy Rust hack for converting between non-local types","archived":false,"fork":false,"pushed_at":"2025-04-06T10:45:21.000Z","size":13,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-26T18:54:21.511Z","etag":null,"topics":["cheese","coherence","conversion","hack","orphan","rust","rust-lang","trait"],"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/fujiapple852.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,"zenodo":null}},"created_at":"2025-04-04T12:59:32.000Z","updated_at":"2025-04-06T10:47:47.000Z","dependencies_parsed_at":"2025-04-04T13:17:14.838Z","dependency_job_id":"9c84465b-32f5-4181-a9fe-9604baee7e84","html_url":"https://github.com/fujiapple852/fromage","commit_stats":null,"previous_names":["fujiapple852/fromage"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/fujiapple852/fromage","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fujiapple852%2Ffromage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fujiapple852%2Ffromage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fujiapple852%2Ffromage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fujiapple852%2Ffromage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fujiapple852","download_url":"https://codeload.github.com/fujiapple852/fromage/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fujiapple852%2Ffromage/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273386175,"owners_count":25096242,"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","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cheese","coherence","conversion","hack","orphan","rust","rust-lang","trait"],"created_at":"2025-04-18T19:08:54.026Z","updated_at":"2025-09-03T03:36:36.737Z","avatar_url":"https://github.com/fujiapple852.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"![ci](https://github.com/fujiapple852/fromage/actions/workflows/ci.yml/badge.svg)\n[![Documentation](https://docs.rs/fromage/badge.svg)](https://docs.rs/fromage/0.1.1)\n[![Crate](https://img.shields.io/crates/v/fromage.svg)](https://crates.io/crates/fromage/0.1.1)\n\n# Fromage 🧀\n\nA cheesy Rust hack for converting between _non-local_ types.\n\nTL;DR: Allows implementing `From` and `TryFrom` like traits for _non-local_ types without violating\nthe [orphan rules](https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules).\n\nThis crate has no dependencies or macros, is `no_std` and forbids `unsafe` code.\n\n## Example\n\nConvert between the two non-local types `String` and `usize`:\n\n```rust\nuse fromage::{Fromage, TryFromage};\n\nstruct X;\nimpl Fromage\u003cString, X\u003e for usize {\n    fn fromage(value: String) -\u003e Self {\n        value.len()\n    }\n}\nimpl TryFromage\u003cString, X\u003e for usize {\n    type Error = ();\n\n    fn try_fromage(value: String) -\u003e Result\u003cSelf, Self::Error\u003e {\n        Ok(value.len())\n    }\n}\n\n#[test]\nfn test() {\n    assert_eq!(5_usize, usize::fromage(String::from(\"hello\")));\n    assert_eq!(5_usize, usize::try_fromage(String::from(\"world\")).unwrap());\n}\n```\n\n## Status\n\nExperimental.\n\n## How it works\n\nThe [orphan rules](https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules) state that:\n\n```\n\u003e Given impl\u003cP1..=Pn\u003e Trait\u003cT1..=Tn\u003e for T0, an impl is valid only if at least one of the following is true:\n\u003e\n\u003e - `Trait` is a local trait\n\u003e - All of:\n\u003e   - At least one of the types T0..=Tn must be a local type. Let Ti be the first such type.\n\u003e   - No uncovered type parameters P1..=Pn may appear in T0..Ti (excluding Ti)\n```\n\nFromage defines the `Fromage` and `TryFromage` traits that mirror the traits in the standard library, except that they\nrequire an `impl` defined additional type parameter `X` and therefore fulfil the _\"At least one of the types `T0..=Tn`\nmust be a local type\"_ clause above.\n\nThe type parameter `X` is not used in the trait methods. It may be\nany type provided it is both [local](https://doc.rust-lang.org/reference/glossary.html#local-type)\nand [uncovered](https://doc.rust-lang.org/reference/glossary.html#uncovered-type), typically\na [unit struct](https://doc.rust-lang.org/book/ch05-01-defining-structs.html#unit-like-structs-without-any-fields). It\nmay be reused for all `Fromage` and `TryFromage` implementations within a crate.\n\nThe `Fromage` and `TryFromage` traits define methods named `fromage` and `try_fromage` respectively to avoid conflicting\nwith the standard library's `From` and `TryFrom` traits.\n\n## Limitations\n\nThe `Fromage` and `TryFromage` traits defined in this crate are distinct from the standard library's `From` and\n`TryFrom` traits and are not interchangeable. Therefore, if a crate uses the standard library's `From` and `TryFrom`\ntraits in its public API, you cannot use the `Fromage` and `TryFromage` traits to implement conversions for the\ntypes required by that crate.\n\nFor example, if a crate exposes the following public API then you cannot use an `impl Fromage\u003cFoo\u003e for Bar` to\nimplement the conversion.\n\n```rust\npub fn foo(value: impl Into\u003cBar\u003e) { ... }\n```\n\n## Alternatives\n\n### Newtype\n\nTypically, a\nlocal [newtype](https://doc.rust-lang.org/book/ch20-02-advanced-traits.html#using-the-newtype-pattern-to-implement-external-traits-on-external-typesl)\nis used to implement the standard library's `From` and `TryFrom` traits when both types are non-local.\n\nEither:\n\n```rust\nstruct FooWrapper(Foo);\n\nimpl From\u003cFooWrapper\u003e for Bar {\n    fn from(_value: FooWrapper) -\u003e Self {\n        Bar\n    }\n}\n```\n\nOr:\n\n```rust\nstruct BarWrapper(Bar);\n\nimpl From\u003cFoo\u003e for BarWrapper {\n    fn from(_value: Foo) -\u003e Self {\n        BarWrapper(Bar)\n    }\n}\n```\n\n### Conversion function\n\nSimple conversions functions can be used instead of the `From` and `TryFrom` traits.\n\n```rust\nfn convert(_value: Foo) -\u003e Bar {\n    Bar\n}\nfn try_convert(_value: Foo) -\u003e Result\u003cBar, ()\u003e {\n    Ok(Bar)\n}\n```\n\n### Local traits\n\nFor completeness, the orphan rules allow implementing local `MyFrom` and `MyTryFrom` traits which may then be used with\nnon-local types.\n\nThis approach is not recommended as it requires a significant amount of boilerplate code whilst sharing the same\nlimitations as the Fromage approach.\n\n## License\n\nFromage is distributed under the terms of the Apache License (Version 2.0).\n\nSee [LICENSE](LICENSE) for details.\n\nCopyright 2025","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffujiapple852%2Ffromage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffujiapple852%2Ffromage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffujiapple852%2Ffromage/lists"}