Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug, Copy, Clone)]
- struct Session {session_id: i8} //< A type that may be contained by many others.
- // Anything that implements SessionDetails, reveals that you may retrieve it.
- trait SessionDetails {
- fn get_session_details(&self) -> Session;
- }
- struct NCConnected{sid: Session} //< Our state for a successful connection
- impl SessionDetails for NCConnected {
- fn get_session_details(&self) -> Session {
- self.sid // return a copy of session that a connection contains.
- }
- }
- // Trait bound: Anything that is passed this function must implement SessionDetails.
- fn new_edit_session<T: SessionDetails>(generic_object: T) {
- let sid: Session = generic_object.get_session_details();
- println!("Retrieved session id: {:?}", sid);
- }
- // A quick example of instantiation:
- impl NCConnected {
- pub fn connect() -> NCConnected {
- NCConnected{sid: Session{session_id: 4}}
- }
- }
- fn main() {
- new_edit_session(NCConnected::connect());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement