{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rfcs/issues/1265","labels_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1265/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1265/comments","events_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1265/events","html_url":"https://github.com/rust-lang/rfcs/issues/1265","id":102954216,"number":1265,"title":"Add a new ToOpt trait and have bool implement it","user":{"login":"tafia","id":7351502,"avatar_url":"https://avatars.githubusercontent.com/u/7351502?v=3","gravatar_id":"","url":"https://api.github.com/users/tafia","html_url":"https://github.com/tafia","followers_url":"https://api.github.com/users/tafia/followers","following_url":"https://api.github.com/users/tafia/following{/other_user}","gists_url":"https://api.github.com/users/tafia/gists{/gist_id}","starred_url":"https://api.github.com/users/tafia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tafia/subscriptions","organizations_url":"https://api.github.com/users/tafia/orgs","repos_url":"https://api.github.com/users/tafia/repos","events_url":"https://api.github.com/users/tafia/events{/privacy}","received_events_url":"https://api.github.com/users/tafia/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":18,"created_at":"2015-08-25T05:38:11Z","updated_at":"2015-09-13T18:09:13Z","closed_at":null,"body":"## Motivation\r\n\r\nIt would be nice if we could have a one-liner function to convert a `bool` into an `Option`.\r\n\r\ni.e. Instead of writing:\r\n\r\n```rust\r\nif c1 {\r\n    Some(a)\r\n} else if c2 {\r\n    // expensive function returning Option<T>\r\n    f()\r\n} else {\r\n    None\r\n}\r\n```\r\n\r\nI'd like to write:\r\n```rust\r\nc1.to_opt(a).or(c2.to_opt_lazy(f))\r\n```\r\n\r\n## Implementation\r\n\r\nAdd a new ToOpt trait and have bool implement it\r\n\r\n```rust\r\ntrait ToOpt {\r\n    fn to_opt<T>(&self, item: T) -> Option<T>;\r\n    fn to_opt_lazy<F, T>(&self, f: F) -> Option<T> \r\n    where F: FnOnce() -> T {\r\n        // default implementation (not lazy)\r\n        self.to_opt(f())\r\n    }\r\n}\r\n\r\nimpl ToOpt for bool {\r\n\r\n    fn to_opt<T>(&self, item: T) -> Option<T> {\r\n        if *self {\r\n            Some(item)\r\n        } else {\r\n            None\r\n        }\r\n    }\r\n    \r\n    fn to_opt_lazy<F, T>(&self, f: F) -> Option<T> \r\n    where F: FnOnce() -> T {\r\n        if *self {\r\n            Some(f())\r\n        } else {\r\n            None\r\n        }\r\n    }\r\n}\r\n```\r\n\r\n## Alternative\r\n\r\nAs mentioned by @seppo0010 we could also have a simpler implementation using `()` instead of generic `T`\r\n\r\n```rust\r\ntrait ToOpt {\r\n    fn to_opt(&self) -> Option<()>;\r\n}\r\n\r\nimpl ToOpt for bool {\r\n    fn to_opt(&self) -> Option<()> {\r\n        if *self {\r\n            Some(())\r\n        } else {\r\n            None\r\n        }\r\n    }\r\n}\r\n\r\n// then have a little longer call using `Option` functions\r\nc1.to_opt().and(Some(a)).or(c2.to_opt().map(|| f())\r\n```\r\n\r\n- pros\r\n  - simpler implementation\r\n  - kind of symmetric to Option<()>.is_some() -> bool \r\n- cons\r\n  - longer calls\r\n  - perhaps rely more on compiler optimizations"},"comment":{"url":"https://api.github.com/repos/rust-lang/rfcs/issues/comments/139900555","html_url":"https://github.com/rust-lang/rfcs/issues/1265#issuecomment-139900555","issue_url":"https://api.github.com/repos/rust-lang/rfcs/issues/1265","id":139900555,"user":{"login":"fenhl","id":641386,"avatar_url":"https://avatars.githubusercontent.com/u/641386?v=3","gravatar_id":"","url":"https://api.github.com/users/fenhl","html_url":"https://github.com/fenhl","followers_url":"https://api.github.com/users/fenhl/followers","following_url":"https://api.github.com/users/fenhl/following{/other_user}","gists_url":"https://api.github.com/users/fenhl/gists{/gist_id}","starred_url":"https://api.github.com/users/fenhl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fenhl/subscriptions","organizations_url":"https://api.github.com/users/fenhl/orgs","repos_url":"https://api.github.com/users/fenhl/repos","events_url":"https://api.github.com/users/fenhl/events{/privacy}","received_events_url":"https://api.github.com/users/fenhl/received_events","type":"User","site_admin":false},"created_at":"2015-09-13T18:09:13Z","updated_at":"2015-09-13T18:09:13Z","body":"Conversion traits between `bool` and `Option<()>` should be implemented, as the correspondence is intuitive and because the traits are in the prelude already."}}