Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::borrow::Borrow;
- use std::fmt::Debug;
- use std::fmt::Error;
- use std::fmt::Formatter;
- use std::borrow::ToOwned;
- #[derive(PartialEq, Eq)]
- struct MyString(String);
- impl Debug for MyString {
- fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
- write!(f, "MyString")
- }
- }
- impl From<String> for MyString {
- fn from(s: String) -> MyString {
- MyString(s)
- }
- }
- #[derive(PartialEq, Eq)]
- struct MyStr(str);
- impl<'a> Debug for &'a MyStr {
- fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
- write!(f, "MyStr")
- }
- }
- impl<'a> From<&'a str> for &'a MyStr {
- fn from(s: &'a str) -> &'a MyStr {
- unsafe { std::mem::transmute(s) }
- }
- }
- impl<'a> Borrow<MyStr> for MyString {
- fn borrow<'b>(&'b self) -> &'b MyStr {
- self.0.as_str().into()
- }
- }
- impl ToOwned for MyStr {
- type Owned = MyString;
- fn to_owned(&self) -> Self::Owned {
- MyString(self.0.to_owned())
- }
- }
- fn main() {
- let s: MyString = "foo".to_string().into();
- println!("{:?}", s);
- let s: &MyStr = "foo".into();
- println!("{:?}", s);
- }
Add Comment
Please, Sign In to add comment