Guest User

Untitled

a guest
Apr 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. use std::borrow::Borrow;
  2. use std::fmt::Debug;
  3. use std::fmt::Error;
  4. use std::fmt::Formatter;
  5.  
  6. #[derive(PartialEq, Eq)]
  7. struct MyString(String);
  8.  
  9. impl Debug for MyString {
  10. fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
  11. write!(f, "MyString")
  12. }
  13. }
  14.  
  15. impl From<String> for MyString {
  16. fn from(s: String) -> MyString {
  17. MyString(s)
  18. }
  19. }
  20.  
  21. #[derive(PartialEq, Eq)]
  22. struct MyStr(str);
  23.  
  24. impl<'a> Debug for &'a MyStr {
  25. fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
  26. write!(f, "MyStr")
  27. }
  28. }
  29.  
  30. impl<'a> From<&'a str> for &'a MyStr {
  31. fn from(s: &'a str) -> &'a MyStr {
  32. unsafe { std::mem::transmute(s) }
  33. }
  34. }
  35.  
  36. impl<'a> Borrow<MyStr> for MyString {
  37. fn borrow<'b>(&'b self) -> &'b MyStr {
  38. self.0.as_str().into()
  39. }
  40. }
  41.  
  42. fn main() {
  43. let s: MyString = "foo".to_string().into();
  44. println!("{:?}", s);
  45. let s: &MyStr = "foo".into();
  46. println!("{:?}", s);
  47. }
Add Comment
Please, Sign In to add comment