{"id":17887768,"url":"https://github.com/jmacd/rusttest","last_synced_at":"2025-04-03T02:41:42.088Z","repository":{"id":256287290,"uuid":"854398818","full_name":"jmacd/rusttest","owner":"jmacd","description":null,"archived":false,"fork":false,"pushed_at":"2024-09-10T06:24:01.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-08T16:43:35.465Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jmacd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-09-09T05:20:14.000Z","updated_at":"2024-09-10T06:24:04.000Z","dependencies_parsed_at":"2024-09-10T02:23:31.230Z","dependency_job_id":"dceddb97-0183-40fe-98d7-01a60308f6a2","html_url":"https://github.com/jmacd/rusttest","commit_stats":null,"previous_names":["jmacd/rusttest"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmacd%2Frusttest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmacd%2Frusttest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmacd%2Frusttest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmacd%2Frusttest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmacd","download_url":"https://codeload.github.com/jmacd/rusttest/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246927810,"owners_count":20856193,"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":[],"created_at":"2024-10-28T13:35:22.053Z","updated_at":"2025-04-03T02:41:42.057Z","avatar_url":"https://github.com/jmacd.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"Rust learner asks for help!\n\n## Problem\n\nIn [src/main.rs](./src/main.rs), see a mock-up of the problem I ran into (from https://github.com/jmacd/duckpond).\n\nThe types are renamed for clarity.\n\n- *System* represents a file system, representing the global state and containing a root directory. The program is single-threaded and control-flow always has an associated `\u0026mut System`.\n- *Folder* is a trait for directories, real or dynamic, with a `subdir()` method\n- *Realdir* is a real directory\n- *Dynadir* is a dynamic directory.\n- *WD* is a working directory a single handle to a pair of `\u0026mut System` and `Rc\u003cRefCell\u003cdyn Folder\u003e\u003e` with a `lookup` method\n\nThe problem is that I do not understand how to return a `Rc\u003cRefCell\u003cdyn Folder\u003e\u003e` from `subdir()` with proper lifetime annotations (with or without `'_`, as suggested by the compiler, or a variety of other rearrangements I've tried).\n\nThe error is:\n\n```rust\nerror: lifetime may not live long enough\n  --\u003e src/main.rs:43:9\n   |\n42 |     fn subdir(\u0026mut self, sys: \u0026mut System, _name: \u0026str) -\u003e Option\u003cRc\u003cRefCell\u003cdyn Folder\u003e\u003e\u003e {\n   |                               - let's call the lifetime of this reference `'1`\n43 |         Some(sys.wd().lookup(Path::new(\"d/e/f\")))\n   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`\n   |\n   = note: requirement occurs because of the type `RefCell\u003cdyn Folder\u003e`, which makes the generic argument `dyn Folder` invariant\n   = note: the struct `RefCell\u003cT\u003e` is invariant over the parameter `T`\n   = help: see \u003chttps://doc.rust-lang.org/nomicon/subtyping.html\u003e for more information about variance\nhelp: to declare that the trait object captures data from argument `sys`, you can add an explicit `'_` lifetime bound\n   |\n42 |     fn subdir(\u0026mut self, sys: \u0026mut System, _name: \u0026str) -\u003e Option\u003cRc\u003cRefCell\u003cdyn Folder + '_\u003e\u003e\u003e {\n   |                                                                                         ++++\n\n```\n\n\n```\n    fn lookup\u003c'b\u003e(\u0026'b mut self, path: \u0026'_ Path) -\u003e Option\u003cWD\u003c'a\u003e\u003e\n    where\n        'a: 'b,\n```\n\ngives\n```\nerror: lifetime may not live long enough\n   --\u003e src/main.rs:101:13\n    |\n82  | impl\u003c'a\u003e WD\u003c'a\u003e {\n    |      -- lifetime `'a` defined here\n...\n86  |     fn lookup\u003c'b\u003e(\u0026'b mut self, path: \u0026'_ Path) -\u003e Option\u003cWD\u003c'a\u003e\u003e\n    |               -- lifetime `'b` defined here\n...\n101 |             Some(child)\n    |             ^^^^^^^^^^^ method was supposed to return data with lifetime `'a` but it is returning data with lifetime `'b`\n    |\n    = help: consider adding the following bound: `'b: 'a`\n    = note: requirement occurs because of the type `WD\u003c'_\u003e`, which makes the generic argument `'_` invariant\n    = note: the struct `WD\u003c'a\u003e` is invariant over the parameter `'a`\n    = help: see \u003chttps://doc.rust-lang.org/nomicon/subtyping.html\u003e for more information about variance\n```\n\n```\n    fn lookup\u003c'b\u003e(\u0026'b mut self, path: \u0026'_ Path) -\u003e Option\u003cWD\u003c'a\u003e\u003e\n    where\n        'b: 'a,\n```\n\ngives e.g.,\n\n```\nerror[E0515]: cannot return value referencing temporary value\n  --\u003e src/main.rs:46:9\n   |\n46 |         sys.wd().lookup(Path::new(\"d/e/f\")).unwrap()\n   |         --------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n   |         |\n   |         returns a value referencing data owned by the current function\n   |         temporary value created here\n\n```\n\n(and didn't seem right)\n\nlastly\n\n```\n    fn lookup(\u0026'_ mut self, path: \u0026'_ Path) -\u003e Option\u003cWD\u003c'a\u003e\u003e {\n```\n\n```\nerror: lifetime may not live long enough\n  --\u003e src/main.rs:95:13\n   |\n82 | impl\u003c'a\u003e WD\u003c'a\u003e {\n   |      -- lifetime `'a` defined here\n83 |     fn lookup(\u0026'_ mut self, path: \u0026'_ Path) -\u003e Option\u003cWD\u003c'a\u003e\u003e {\n   |               - let's call the lifetime of this reference `'1`\n...\n95 |             Some(child)\n   |             ^^^^^^^^^^^ method was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`\n   |\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmacd%2Frusttest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmacd%2Frusttest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmacd%2Frusttest/lists"}