hurry
Convenient macros for creating pointer types (Box, Rc, Arc, etc.) in Rust.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
To use the procedural macro for generating custom shorthand macros, enable the macros feature:
[]
= { = "0.1", = ["macros"] }
Usage
The hurry crate provides convenient macros for creating common pointer types:
Basic Pointer Types
use *;
// Create a Box
let boxed = boxx!;
assert_eq!;
// Create an Rc
let shared = rc!;
let clone = shared.clone;
assert_eq!;
// Create an Arc
let atomic = arc!;
let thread_clone = atomic.clone;
// Can be safely shared across threads
Interior Mutability Types
use *;
// Rc<RefCell<T>> for single-threaded interior mutability
let cell = rc_refcell!;
*cell.borrow_mut += 10;
assert_eq!;
// Arc<Mutex<T>> for thread-safe interior mutability
let mutex = arc_mutex!;
*x.lock.unwrap += 20;
assert_eq!;
// Arc<RwLock<T>> for thread-safe read-write access
let rwlock = arc_rwlock!;
*x.write.unwrap += 30;
assert_eq!;
Procedural Macros
The hurry crate also provides a procedural macro for generating shorthand macros for your own types:
use shorthand;
// This generates a `my_type!` macro for creating instances
let instance = my_type!;
assert_eq!;
The #[shorthand] attribute generates a macro named after the type in snake_case that calls the new method.
Available Macros
Declarative Macros
boxx!(value)→Box::new(value)rc!(value)→Rc::new(value)arc!(value)→Arc::new(value)rc_refcell!(value)→Rc<RefCell<T>>::new(RefCell::new(value))arc_mutex!(value)→Arc<Mutex<T>>::new(Mutex::new(value))arc_rwlock!(value)→Arc<RwLock<T>>::new(RwLock::new(value))mutex!(value)→Mutex::new(value)rwlock!(value)→RwLock::new(value)cell!(value)→Cell::new(value)refcell!(value)→RefCell::new(value)pin_box!(value)→Box::pin(value)vec_box!(values...)→vec![Box::new(value), ...]vec_rc!(values...)→vec![Rc::new(value), ...]vec_arc!(values...)→vec![Arc::new(value), ...]cow_owned!(value)→Cow::Owned(value)cow_borrowed!(value)→Cow::Borrowed(value)
Procedural Macros
#[shorthand]→ Generates a shorthand macro for types with anewmethod
Documentation
Full API documentation is available at docs.rs/hurry.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.