Skip to content

Commit

Permalink
Some minor updates to the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
skogseth committed Sep 26, 2023
1 parent 0d8b45c commit ecda041
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<T> HzrdCell<T> {
}

/**
Get a handle to read the value held by the `HzrdCell`
Get a handle holding a reference to the current value held by the `HzrdCell`
The functionality of this is somewhat similar to a [`MutexGuard`](std::sync::MutexGuard), except the [`RefHandle`] only accepts reading the value. There is no locking mechanism needed to grab this handle, although there might be a short wait if the read overlaps with a write.
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<T> HzrdCell<T> {
}

/**
Read contained value and map it
Read the contained value and map it
```
# use hzrd::HzrdCell;
Expand Down
38 changes: 30 additions & 8 deletions src/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This pair is the most primitive constructs found in this crate, as they contain
let ready_writer = HzrdWriter::new(false);
std::thread::scope(|s| {
let ready_reader = ready_writer.reader();
let ready_reader = ready_writer.new_reader();
s.spawn(move || {
while !ready_reader.get() {
std::hint::spin_loop();
Expand Down Expand Up @@ -65,7 +65,7 @@ impl<T> HzrdWriter<T> {
#
let writer = HzrdWriter::new(0);
#
# assert_eq!(writer.reader().get(), 0);
# assert_eq!(writer.new_reader().get(), 0);
```
*/
pub fn new(value: T) -> Self {
Expand All @@ -79,12 +79,12 @@ impl<T> HzrdWriter<T> {
# use hzrd::pair::HzrdWriter;
#
let writer = HzrdWriter::new(0);
let reader = writer.reader();
let reader = writer.new_reader();
writer.set(1);
assert_eq!(reader.get(), 1);
```
*/
pub fn reader(&self) -> HzrdReader<T> {
pub fn new_reader(&self) -> HzrdReader<T> {
// SAFETY:
// - Only the writer can access this
// - Writer is not Sync, so only this thread can write
Expand All @@ -98,6 +98,18 @@ impl<T> HzrdWriter<T> {
}
}

/**
Set the value of the container
```
# use hzrd::pair::HzrdWriter;
#
let writer = HzrdWriter::new(0);
let reader = writer.new_reader();
writer.set(1);
assert_eq!(reader.get(), 1);
```
*/
pub fn set(&self, value: T) {
let old_ptr = self.core.swap(value);

Expand Down Expand Up @@ -151,24 +163,34 @@ unsafe impl<T> crate::core::Read for HzrdReader<'_, T> {
}

impl<T> HzrdReader<'_, T> {
/**
Get a handle holding a reference to the current value of the container
See [`HzrdCell::read`] for a more detailed description
*/
pub fn read(&mut self) -> RefHandle<T> {
<Self as crate::core::Read>::read(self)
}

/// Get the value of the container (requires the type to be [`Copy`])
pub fn get(&self) -> T
where
T: Copy,
{
<Self as crate::core::Read>::get(self)
}

/// Read the contained value and clone it (requires type to be [`Clone`])
pub fn cloned(&self) -> T
where
T: Clone,
{
<Self as crate::core::Read>::cloned(self)
}

/// Read the contained value and map it
///
/// See [`HzrdCell::read_and_map`] for a more detailed description
pub fn read_and_map<U, F: FnOnce(&T) -> U>(&self, f: F) -> U {
<Self as crate::core::Read>::read_and_map(self, f)
}
Expand Down Expand Up @@ -198,7 +220,7 @@ mod tests {
let writer = HzrdWriter::new('a');

std::thread::spawn(move || {
let val: char = writer.reader().get();
let val: char = writer.new_reader().get();
assert_eq!(val, 'a');
});
}
Expand All @@ -214,21 +236,21 @@ mod tests {
let writer = HzrdWriter::new(0);

std::thread::scope(|s| {
let mut reader = writer.reader();
let mut reader = writer.new_reader();
s.spawn(move || {
let handle = HzrdReader::read(&mut reader);
assert!(matches!(*handle, 0 | 1));
});

let mut reader = writer.reader();
let mut reader = writer.new_reader();
s.spawn(move || {
let handle = HzrdReader::read(&mut reader);
assert!(matches!(*handle, 0 | 1));
});

writer.set(1);

let mut reader = writer.reader();
let mut reader = writer.new_reader();
s.spawn(move || {
let handle = HzrdReader::read(&mut reader);
assert_eq!(*handle, 1);
Expand Down

0 comments on commit ecda041

Please sign in to comment.