~jplatte/oref

976462aa0673b1e4ef0f05bc61de52c1828f2402 — Jonas Platte 9 months ago 8691aa4 master
Add stuff (doesn't seem like this will work)
1 files changed, 46 insertions(+), 0 deletions(-)

M src/lib.rs
M src/lib.rs => src/lib.rs +46 -0
@@ 1,5 1,7 @@
#![feature(generic_associated_types)]

use std::mem;

use stable_deref_trait::StableDeref;

pub mod handle;


@@ 51,4 53,48 @@ where
            handle: f(self.handle),
        }
    }

    pub fn get<'a>(&'a self) -> &'a H::Handle<'a> {
        // TODO: This is probably unsound in the face of contravariant lifetime parameters
        unsafe { mem::transmute(&self.handle) }
    }
}

#[cfg(test)]
mod tests {
    use super::{GetHandle, ORef};

    #[test]
    fn new_string() {
        let x = ORef::new("test".to_owned());
        assert_eq!(*x.get(), "test");
    }

    struct SplitStr<'a> {
        fst: &'a str,
        snd: &'a str,
    }

    impl<'a> SplitStr<'a> {
        fn new(s: &'a str, split_c: char) -> Option<Self> {
            s.find(split_c).map(|idx| Self {
                fst: &s[..idx],
                snd: &s[idx + split_c.len_utf8()..],
            })
        }
    }

    struct GetSplitStr;

    impl GetHandle for GetSplitStr {
        type Handle<'a> = SplitStr<'a>;
    }

    #[test]
    fn new_split_str() {
        let x: ORef<_, GetSplitStr> =
            ORef::new("domain.tld".to_owned()).map(|s| SplitStr::new(s, '.').unwrap());
        assert_eq!(x.get().fst, "domain");
        assert_eq!(x.get().snd, "tld");
    }
}