@@ -112,8 +112,35 @@ pub enum Error {
112112/// Platform-independent thread priority value.
113113/// Should be in `[0; 100)` range. The higher the number is - the higher
114114/// the priority.
115+ ///
116+ /// The only way to create such a value is a safe conversion from an 8-byte
117+ /// unsigned integer ([`u8`]):
118+ ///
119+ /// ```rust
120+ /// use thread_priority::*;
121+ /// use std::convert::{TryFrom, TryInto};
122+ ///
123+ /// // Create the lowest possible priority value.
124+ /// assert!(ThreadPriorityValue::try_from(0u8).is_ok());
125+ /// // Create it implicitly via `TryInto`:
126+ /// let _priority = ThreadPriority::Crossplatform(0u8.try_into().unwrap());
127+ /// ```
128+ ///
129+ /// In case you need to get the raw value out of it, use the `Into<u8>` trait:
130+ ///
131+ /// ```rust
132+ /// use thread_priority::*;
133+ /// use std::convert::TryFrom;
134+ ///
135+ /// // Create the lowest possible priority value.
136+ /// let priority = ThreadPriorityValue::try_from(0u8).unwrap();
137+ /// // Create it implicitly via `TryInto`:
138+ /// let raw_value: u8 = priority.into();
139+ /// assert_eq!(raw_value, 0);
140+ /// ```
115141#[ derive( Copy , Clone , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
116142pub struct ThreadPriorityValue ( u8 ) ;
143+
117144impl std:: convert:: TryFrom < u8 > for ThreadPriorityValue {
118145 type Error = & ' static str ;
119146
@@ -126,6 +153,15 @@ impl std::convert::TryFrom<u8> for ThreadPriorityValue {
126153 }
127154}
128155
156+ // The From<u8> is unsafe, so there is a TryFrom instead.
157+ // For this reason we silent the warning from clippy.
158+ #[ allow( clippy:: from_over_into) ]
159+ impl std:: convert:: Into < u8 > for ThreadPriorityValue {
160+ fn into ( self ) -> u8 {
161+ self . 0
162+ }
163+ }
164+
129165/// Platform-specific thread priority value.
130166#[ derive( Copy , Clone , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
131167pub struct ThreadPriorityOsValue ( u32 ) ;
0 commit comments