1 // Copyright 2022 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 /// Trait that can be used to associate events with arbitrary enums when using 6 /// EventContext. 7 /// 8 /// Simple enums that have no or primitive variant data data can use the `#[derive(PollToken)]` 9 /// custom derive to implement this trait. See 10 /// [poll_token_derive::poll_token](../poll_token_derive/fn.poll_token.html) for details. 11 pub trait PollToken { 12 /// Converts this token into a u64 that can be turned back into a token via `from_raw_token`. as_raw_token(&self) -> u6413 fn as_raw_token(&self) -> u64; 14 15 /// Converts a raw token as returned from `as_raw_token` back into a token. 16 /// 17 /// It is invalid to give a raw token that was not returned via `as_raw_token` from the same 18 /// `Self`. The implementation can expect that this will never happen as a result of its usage 19 /// in `EventContext`. from_raw_token(data: u64) -> Self20 fn from_raw_token(data: u64) -> Self; 21 } 22 23 impl PollToken for usize { as_raw_token(&self) -> u6424 fn as_raw_token(&self) -> u64 { 25 *self as u64 26 } 27 from_raw_token(data: u64) -> Self28 fn from_raw_token(data: u64) -> Self { 29 data as Self 30 } 31 } 32 33 impl PollToken for u64 { as_raw_token(&self) -> u6434 fn as_raw_token(&self) -> u64 { 35 *self as u64 36 } 37 from_raw_token(data: u64) -> Self38 fn from_raw_token(data: u64) -> Self { 39 data as Self 40 } 41 } 42 43 impl PollToken for u32 { as_raw_token(&self) -> u6444 fn as_raw_token(&self) -> u64 { 45 u64::from(*self) 46 } 47 from_raw_token(data: u64) -> Self48 fn from_raw_token(data: u64) -> Self { 49 data as Self 50 } 51 } 52 53 impl PollToken for u16 { as_raw_token(&self) -> u6454 fn as_raw_token(&self) -> u64 { 55 u64::from(*self) 56 } 57 from_raw_token(data: u64) -> Self58 fn from_raw_token(data: u64) -> Self { 59 data as Self 60 } 61 } 62 63 impl PollToken for u8 { as_raw_token(&self) -> u6464 fn as_raw_token(&self) -> u64 { 65 u64::from(*self) 66 } 67 from_raw_token(data: u64) -> Self68 fn from_raw_token(data: u64) -> Self { 69 data as Self 70 } 71 } 72 73 impl PollToken for () { as_raw_token(&self) -> u6474 fn as_raw_token(&self) -> u64 { 75 0 76 } 77 from_raw_token(_data: u64) -> Self78 fn from_raw_token(_data: u64) -> Self {} 79 } 80 81 #[cfg(test)] 82 mod tests { 83 use super::*; 84 use base_poll_token_derive::PollToken; 85 86 #[test] 87 #[allow(dead_code)] poll_token_derive()88 fn poll_token_derive() { 89 #[derive(PollToken)] 90 enum EmptyToken {} 91 92 #[derive(PartialEq, Debug, PollToken)] 93 enum Token { 94 Alpha, 95 Beta, 96 // comments 97 Gamma(u32), 98 Delta { index: usize }, 99 Omega, 100 } 101 102 assert_eq!( 103 Token::from_raw_token(Token::Alpha.as_raw_token()), 104 Token::Alpha 105 ); 106 assert_eq!( 107 Token::from_raw_token(Token::Beta.as_raw_token()), 108 Token::Beta 109 ); 110 assert_eq!( 111 Token::from_raw_token(Token::Gamma(55).as_raw_token()), 112 Token::Gamma(55) 113 ); 114 assert_eq!( 115 Token::from_raw_token(Token::Delta { index: 100 }.as_raw_token()), 116 Token::Delta { index: 100 } 117 ); 118 assert_eq!( 119 Token::from_raw_token(Token::Omega.as_raw_token()), 120 Token::Omega 121 ); 122 } 123 } 124