Skip to content

Commit

Permalink
Add rwlock macro
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed May 22, 2020
1 parent e956da8 commit 06b8e8d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changes

## Unreleased ~ master ~ 2.0-dev
- Add `rwlock!` macro to create `RwLock` types
- Remove haskell-like and python-like comprehension syntax
- Re-implement comprehensions based on the `c!` macro
- Add a comprehension macro for lazy iterators: `c!`
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ exclude = ["/.travis.yml", "/.github"]
edition = "2018"

[badges]
travis-ci = { repository = "GrayJack/sugars" }
maintenance = { status = "actively-developed" }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ on Rust ecosystem.
* **mutex**: A simple macro to make a new `Mutex` value.²
* **refcell**: A simple macro to make a new `RefCell` value.²
* **rc**: A simple macro to make a new `Rc` value.²
* **rwlock**: A simple macro to make a new [`RwLock`] value.²
* **Time/Duration:**
* **dur**: Creates a `Duration` object following a time pattern¹
* **sleep**: Makes a thread sleep a amount following a time pattern¹
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
//! * **mutex**: A simple macro to make a new [`Mutex`] value.²
//! * **refcell**: A simple macro to make a new [`RefCell`] value.²
//! * **rc**: A simple macro to make a new [`Rc`] value.²
//! * **rwlock**: A simple macro to make a new [`RwLock`] value.²
//! * **Time/Duration:**
//! * **dur**: Creates a [`Duration`] object following a time pattern¹
//! * **sleep**: Makes a thread sleep a amount following a time pattern¹
Expand Down Expand Up @@ -133,6 +134,7 @@
//! [`Mutex`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html
//! [`Rc`]: https://doc.rust-lang.org/std/rc/struct.Rc.html
//! [`RefCell`]: https://doc.rust-lang.org/std/cell/struct.RefCell.html
//! [`RwLock`]: https://doc.rust-lang.org/std/sync/struct.RwLock.html
//!
//! [`Duration`]: https://doc.rust-lang.org/std/time/struct.Duration.html
Expand Down
43 changes: 41 additions & 2 deletions src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,35 @@ macro_rules! mutex {
};
}

/// A simple macro to make a new [`RwLock`] value.
///
/// It is also able to create tuples if given more than one parameter.
///
/// # Example
/// ```
/// use std::sync::RwLock;
/// use sugars::rwlock;
/// # fn main() {
/// let rwlk = rwlock!(String::new());
/// let mut read = rwlk.read().unwrap();
/// println("{}", read);
/// # }
/// ```
///
/// [`RwLock`]: https://doc.rust-lang.org/std/sync/struct.RwLock.html
#[macro_export]
macro_rules! rwlock {
($e:expr) => {
std::sync::RwLock::new($e)
};
($e:expr,) => {
$crate::mutex!($e)
};
($($e:expr),+ $(,)?) => {
($($crate::mutex!($e)),+,)
};
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -319,7 +348,7 @@ mod tests {
}

#[test]
fn mutex() -> Result<(), Box<dyn std::error::Error>> {
fn mutex() {
use std::sync::Mutex;

let mutex_expected = Mutex::new(Some("String"));
Expand All @@ -328,7 +357,17 @@ mod tests {
let expected = mutex_expected.lock().unwrap();
let test = mutex_test.lock().unwrap();
assert_eq!(expected.is_some(), test.is_some());
}

#[test]
fn rwlock() {
use std::sync::RwLock;

let rwlk_expected = RwLock::new(Some("String"));
let rwlk_test = rwlock!(Some("String"));

Ok(())
let expected = rwlk_expected.read().unwrap();
let test = rwlk_test.read().unwrap();
assert_eq!(expected.is_some(), test.is_some());
}
}

0 comments on commit 06b8e8d

Please sign in to comment.