{"action":"created","issue":{"url":"https://api.github.com/repos/Hoverbear/getset/issues/14","repository_url":"https://api.github.com/repos/Hoverbear/getset","labels_url":"https://api.github.com/repos/Hoverbear/getset/issues/14/labels{/name}","comments_url":"https://api.github.com/repos/Hoverbear/getset/issues/14/comments","events_url":"https://api.github.com/repos/Hoverbear/getset/issues/14/events","html_url":"https://github.com/Hoverbear/getset/pull/14","id":343850145,"node_id":"MDExOlB1bGxSZXF1ZXN0MjAzMzg1Mjc4","number":14,"title":"Add replacers","user":{"login":"qtlis","id":15247429,"node_id":"MDQ6VXNlcjE1MjQ3NDI5","avatar_url":"https://avatars0.githubusercontent.com/u/15247429?v=4","gravatar_id":"","url":"https://api.github.com/users/qtlis","html_url":"https://github.com/qtlis","followers_url":"https://api.github.com/users/qtlis/followers","following_url":"https://api.github.com/users/qtlis/following{/other_user}","gists_url":"https://api.github.com/users/qtlis/gists{/gist_id}","starred_url":"https://api.github.com/users/qtlis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qtlis/subscriptions","organizations_url":"https://api.github.com/users/qtlis/orgs","repos_url":"https://api.github.com/users/qtlis/repos","events_url":"https://api.github.com/users/qtlis/events{/privacy}","received_events_url":"https://api.github.com/users/qtlis/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-07-24T01:23:29Z","updated_at":"2018-07-26T16:00:20Z","closed_at":null,"author_association":"NONE","pull_request":{"url":"https://api.github.com/repos/Hoverbear/getset/pulls/14","html_url":"https://github.com/Hoverbear/getset/pull/14","diff_url":"https://github.com/Hoverbear/getset/pull/14.diff","patch_url":"https://github.com/Hoverbear/getset/pull/14.patch"},"body":"Replacers are setters that return the old value instead of dropping it. They provide a much simpler way (compared to using `*_mut` and `std::mem::{replace, swap}`) to retrieve fields by value.\r\n\r\n```rust\r\nuse std::mem;\r\n\r\n#[derive(Getters, Replacers)\r\nstruct Foo {\r\n    a: String\r\n}\r\n\r\nlet mut foo = Foo { \"Hello world!\".into() };\r\n\r\n// old\r\nlet retrieved = mem::replace(foo.a_mut(), String::new());\r\n\r\n// old\r\nlet mut retrieved = String::new();\r\nmem::swap(&mut retrieved, foo.a_mut());\r\n\r\n// new\r\nlet retrieved = foo.replace_a(String::new());\r\n```\r\n\r\n## Open questions:\r\n\r\n- [ ] `replace_*` seems a bit long, should the methods be called `swap_*` instead? But they take a value like `replace`, not a mutable reference like `swap`…\r\n- [ ] Alternatively, they *could* take a mutable reference and work like `swap`. This would make them more flexible and make it possible to chain them like `set_*`."},"comment":{"url":"https://api.github.com/repos/Hoverbear/getset/issues/comments/408147472","html_url":"https://github.com/Hoverbear/getset/pull/14#issuecomment-408147472","issue_url":"https://api.github.com/repos/Hoverbear/getset/issues/14","id":408147472,"node_id":"MDEyOklzc3VlQ29tbWVudDQwODE0NzQ3Mg==","user":{"login":"qtlis","id":15247429,"node_id":"MDQ6VXNlcjE1MjQ3NDI5","avatar_url":"https://avatars0.githubusercontent.com/u/15247429?v=4","gravatar_id":"","url":"https://api.github.com/users/qtlis","html_url":"https://github.com/qtlis","followers_url":"https://api.github.com/users/qtlis/followers","following_url":"https://api.github.com/users/qtlis/following{/other_user}","gists_url":"https://api.github.com/users/qtlis/gists{/gist_id}","starred_url":"https://api.github.com/users/qtlis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qtlis/subscriptions","organizations_url":"https://api.github.com/users/qtlis/orgs","repos_url":"https://api.github.com/users/qtlis/repos","events_url":"https://api.github.com/users/qtlis/events{/privacy}","received_events_url":"https://api.github.com/users/qtlis/received_events","type":"User","site_admin":false},"created_at":"2018-07-26T16:00:20Z","updated_at":"2018-07-26T16:00:20Z","author_association":"NONE","body":"Just because I haven't implemented swappers yet doesn't mean I don't want to have them. Originally, I thought that swappers could replace replacers since they are equally powerful, but there are cases where they aren't:\r\n\r\n```rust\r\nlet mut foo = Foo { a: String::from(\"Hello!\") };\r\n\r\nlet new_a = String::from(\"Bye!\");\r\nlet old_a = foo.replace_a(new_a);\r\n```\r\n\r\nIf you wanted to replicate this with swappers, you'd have to essentially reimplement `mem::replace`:\r\n\r\n```rust\r\nuse std::mem;\r\n\r\nlet mut foo = Foo { a: String::from(\"Hello!\") };\r\n\r\nlet mut new_a = String::from(\"Bye!\");\r\nfoo.swap_a(&mut new_a);\r\nlet old_a = new_a;\r\n```\r\n\r\nVice-versa, there are cases where you can't use replacers; for example, when you don't own the new value, but only have a `&mut` to it."}}
{"actor":{"display_login":"qtlis"}}