|
@@ -5,6 +5,7 @@ use tokio::time::{timeout, Duration};
|
|
|
// #[derive(Clone)] See issue #2
|
|
|
pub struct Shared<T> {
|
|
|
pub data: Arc<Mutex<T>>,
|
|
|
+ /// Number of seconds to consider a dead lock
|
|
|
pub lock_timeout: u64,
|
|
|
}
|
|
|
|
|
@@ -19,16 +20,17 @@ impl<T> Clone for Shared<T> {
|
|
|
|
|
|
impl<T> Shared<T> {
|
|
|
/// Creates a new Shared of data T
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// A lock timeout of 30 seconds is assigned by default (Use Shared::with_timeout to set the timeout yourself, or just edit the field as it is public)
|
|
|
///
|
|
|
/// > Want to see a complete example, look at doc_example in the examples directory
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// Example:
|
|
|
/// ```
|
|
|
/// use shared::Shared;
|
|
|
///
|
|
|
- /// #[derive(Clone)]
|
|
|
+ /// // #[derive(Clone)]
|
|
|
+ /// // Clone is no longer needed as of v0.2.0
|
|
|
/// struct Person {
|
|
|
/// pub name: String,
|
|
|
/// pub age: u16
|
|
@@ -57,26 +59,26 @@ impl<T> Shared<T> {
|
|
|
}
|
|
|
|
|
|
/// Acquire lock
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// Will wait so many seconds as defined by lock_timeout (This will panic once the timeout is reached)
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// > Want to see a complete example, look at doc_example in the examples directory
|
|
|
///
|
|
|
/// Using:
|
|
|
/// ```
|
|
|
/// // See Shared::new for creating a Shared<Person>
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// { // Direct access
|
|
|
/// let person = shared_data.lock().await;
|
|
|
/// println!("Person{{name: {}, age: {}}}", person.name, person.age);
|
|
|
/// }
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// // Using it as a parameter
|
|
|
/// async fn get_older(shared: Shared<Person>) {
|
|
|
/// let mut person = shared.lock().await;
|
|
|
/// person.age += 1;
|
|
|
/// }
|
|
|
- ///
|
|
|
+ ///
|
|
|
/// // Calling
|
|
|
/// get_older(shared_data.clone()).await;
|
|
|
/// ```
|