Guest User

Untitled

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