{"id":16312388,"url":"https://github.com/kiranandcode/dependent-view","last_synced_at":"2025-04-22T11:53:12.387Z","repository":{"id":57617918,"uuid":"143114223","full_name":"kiranandcode/dependent-view","owner":"kiranandcode","description":"A rust library for weak dependent views.","archived":false,"fork":false,"pushed_at":"2018-08-06T14:36:21.000Z","size":28,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T07:42:03.032Z","etag":null,"topics":["rust","trait-object","weak-references"],"latest_commit_sha":null,"homepage":null,"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/kiranandcode.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":"2018-08-01T06:37:15.000Z","updated_at":"2022-11-09T03:21:33.000Z","dependencies_parsed_at":"2022-09-12T20:52:00.606Z","dependency_job_id":null,"html_url":"https://github.com/kiranandcode/dependent-view","commit_stats":null,"previous_names":["kiranandcode/dependent-view","gopiandcode/dependent-view"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiranandcode%2Fdependent-view","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiranandcode%2Fdependent-view/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiranandcode%2Fdependent-view/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiranandcode%2Fdependent-view/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kiranandcode","download_url":"https://codeload.github.com/kiranandcode/dependent-view/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250237801,"owners_count":21397399,"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":["rust","trait-object","weak-references"],"created_at":"2024-10-10T21:47:56.982Z","updated_at":"2025-04-22T11:53:12.368Z","avatar_url":"https://github.com/kiranandcode.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dependent_view [![Build status](https://ci.appveyor.com/api/projects/status/e7l5c4xm4r0e8nc4?svg=true)](https://ci.appveyor.com/project/Gopiandcode/dependent-view) [![Build Status](https://travis-ci.org/Gopiandcode/dependent-view.svg?branch=master)](https://travis-ci.org/Gopiandcode/dependent-view)\n\n\ndependent_view is a rust library providing simple wrappers around the `Rc` and `Arc` types, imbuing them with the capability to provide \"views\" of non-owned structs to separate components of a system. \n\n## Usage\nAdd this to your `Cargo.toml`\n```\n[dependencies]\ndependent_view=\"1\"\n```\nand this to your crate root:\n```\n#[macro_use]\nextern crate dependent_view;\n```\n\n\nThe library provides two main structs `DependentRc` and `DependentArc` for normal and thread-safe views.\n\nThese change the result of the view type (between `std::rc::Weak` or `std::sync::Weak`).\n\nTo obtain a `Weak\u003cTrait\u003e` from these objects, use the macros `to_view!()` or `to_view_sync()` respectively.\n\nIt is checked at compile time that the type `T` wihtin `DependentRc\u003cT\u003e` impl's the trait you want to obtain a view for (see example).\n\nThese dependent types provide a different kind of ownership delegation as compared to standard `Rc`'s or `Box`'s.\n\nA `DependentRc` should be viewed as the single owner of it's contained type, however unlike a `Box`, it allows users to generate multiple runtime managed `Weak\u003cTrait\u003e` references to the object (for each `Trait` impl'd by the contained entity) - these `Weak` references cease to be upgradable once the source `DependantRc` is dropped.\n\n\n## Example\nAssume we have the following traits:\n```\ntrait Dance {\n    fn dance(\u0026self);\n}\n\ntrait Prance {\n    fn prance(\u0026self);\n}\n```\nand some structs which impl the traits:\n```\nstruct Dancer {id: usize}\nimpl Dance for Dancer {fn dance(\u0026self) {println!(\"D{:?}\", self.id);}}\nimpl Prance for Dancer {fn prance(\u0026self)  {println!(\"P{:?}\", self.id);}}\n\nstruct Prancer {id: usize}\nimpl Dance for Prancer {fn dance(\u0026self) {println!(\"D{:?}\", self.id);}}\nimpl Prance for Prancer {fn prance(\u0026self)  {println!(\"P{:?}\", self.id);}}\n```\nWe can create `DependentRc` using the new function:\n```\nuse dependent_view::rc::*;\n\nlet mut dancer = DependentRc::new(Dancer { id: 0 });\nlet mut prancer = DependentRc::new(Prancer { id: 0 });\n```\n\nWe can use these `DependentRc`'s to create non-owned views of our structs:\n\n```\nlet dancer_dance_view : Weak\u003cDance\u003e = to_view!(dancer);\nlet dancer_prance_view : Weak\u003cPrance\u003e = to_view!(dancer);\n\nlet prancer_dance_view : Weak\u003cDance\u003e = to_view!(prancer);\nlet prancer_prance_view : Weak\u003cPrance\u003e = to_view!(prancer);\n```\n\nWe can then share these views to other components, and not have to worry about managing their deletion:\n```\n    let mut dancers : Vec\u003cWeak\u003cDance\u003e\u003e = Vec::new();\n    let mut prancers : Vec\u003cWeak\u003cPrance\u003e\u003e = Vec::new();\n\n    {\n        let mut dancer = DependentRc::new(Dancer { id: 0 });\n        let mut prancer = DependentRc::new(Prancer { id: 0 });\n\n        dancers.push(to_view!(dancer));\n        prancers.push(to_view!(dancer));\n        dancers.push(to_view!(prancer));\n        prancers.push(to_view!(prancer));\n\n        for (dancer_ref, prancer_ref) in dancers.iter().zip(prancers.iter()) {\n             dancer_ref.upgrade().unwrap().dance(); \n             prancer_ref.upgrade().unwrap().prance(); \n        }\n\n       // at this point, dancer and prancer are dropped, invalidating the views\n    }\n\n\n    for (dancer_ref, prancer_ref) in dancers.iter().zip(prancers.iter()) {\n       assert!(dancer_ref.upgrade().is_none());\n       assert!(prancer_ref.upgrade().is_none());\n    }\n```\nAlso, it is a compile time error to attempt to produce a trait view of a struct when the underlying struct doesn't implement the trait:\n```\nstruct Bad { id: usize }\nlet bad = DependentRc::new(Bad { id: 0 });\nlet bad_view : Weak\u003cDance\u003e = to_view!(bad); // compile time error\n```\nSee `example.rs` for the full source.\n\n\nDue to the way the internals work, if the compiler can not infer the type of the result of `to_view!`, it complains about `std::mem::transmute` being called on types of different sizes. This usually only happens if you don't actually use the view - and can often be avoided by simply adding type annotations.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkiranandcode%2Fdependent-view","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkiranandcode%2Fdependent-view","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkiranandcode%2Fdependent-view/lists"}