Skip to content

Commit 58cc2cb

Browse files
committed
Add more documentation.
Adds more information on how to obtain the ThreadPriorityValue object, as it wasn't as clear as it could have been.
1 parent bc4d249 commit 58cc2cb

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "thread-priority"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
authors = ["Victor Polevoy <fx@thefx.co>"]
55
description = "Library for managing threads priority and schedule policies"
66
repository = "https://github.com/vityafx/thread-priority"

src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
116142
pub struct ThreadPriorityValue(u8);
143+
117144
impl 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)]
131167
pub struct ThreadPriorityOsValue(u32);

0 commit comments

Comments
 (0)