Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #[derive(Debug, Copy, Clone)]
  2. struct Session {session_id: i8} //< A type that may be contained by many others.
  3. // Anything that implements SessionDetails, reveals that you may retrieve it.
  4. trait SessionDetails {
  5. fn get_session_details(&self) -> Session;
  6. }
  7.  
  8. struct NCConnected{sid: Session} //< Our state for a successful connection
  9.  
  10. impl SessionDetails for NCConnected {
  11. fn get_session_details(&self) -> Session {
  12. self.sid // return a copy of session that a connection contains.
  13. }
  14. }
  15.  
  16. // Trait bound: Anything that is passed this function must implement SessionDetails.
  17. fn new_edit_session<T: SessionDetails>(generic_object: T) {
  18. let sid: Session = generic_object.get_session_details();
  19. println!("Retrieved session id: {:?}", sid);
  20. }
  21.  
  22. // A quick example of instantiation:
  23. impl NCConnected {
  24. pub fn connect() -> NCConnected {
  25. NCConnected{sid: Session{session_id: 4}}
  26. }
  27. }
  28.  
  29. fn main() {
  30. new_edit_session(NCConnected::connect());
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement