Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(try_from)]
- use std::convert::TryInto;
- struct Foo {
- s: String,
- }
- impl Foo {
- fn to_bar<'foo>(&'foo self) -> Option<Bar<'foo>> {
- Some(Bar { s: &self.s[1..] })
- }
- }
- impl<'foo> TryInto<Baz<'foo>> for &'foo Foo {
- type Error = ();
- fn try_into(self) -> Result<Baz<'foo>, ()> {
- if let Some(bar) = self.to_bar() {
- (&bar).try_into()
- } else {
- Err(())
- }
- }
- }
- struct Bar<'foo> {
- s: &'foo str,
- }
- impl<'a, 'foo> TryInto<Baz<'foo>> for &'a Bar<'foo> {
- type Error = ();
- fn try_into(self) -> Result<Baz<'foo>, ()> {
- if self.s.ends_with("!") {
- Ok(Baz { s: &self.s[1..] })
- } else {
- Err(())
- }
- }
- }
- #[derive(Debug)]
- struct Baz<'foo> {
- s: &'foo str,
- }
- fn main() {
- let f = Foo { s: "Foo!".into() };
- let b: Result<Baz, ()> = (&f).try_into();
- println!("{:?}", b);
- }
Add Comment
Please, Sign In to add comment