-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Bind handles to `ClientFlow` and `ServerFlow` * Refactor `Handle` * Introduce `HandleGeneratorGenerator` * Make `HandleGenerator` more type safe * Introduce `Handle` trait * Add comment
- Loading branch information
1 parent
e2d9823
commit b7fec97
Showing
4 changed files
with
128 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::{ | ||
marker::PhantomData, | ||
sync::atomic::{AtomicU64, Ordering}, | ||
}; | ||
|
||
pub trait Handle { | ||
fn from_raw(raw_handle: RawHandle) -> Self; | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] | ||
pub struct RawHandle { | ||
generator_id: u64, | ||
handle_id: u64, | ||
} | ||
|
||
impl RawHandle { | ||
pub fn generator_id(&self) -> u64 { | ||
self.generator_id | ||
} | ||
|
||
pub fn handle_id(&self) -> u64 { | ||
self.handle_id | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct HandleGenerator<H: Handle> { | ||
/// This ID is used to bind the handles to the generator instance, i.e. it's possible to | ||
/// distinguish handles generated by different generators. We hope that this might | ||
/// prevent bugs when the library user is dealing with handles from different sources. | ||
generator_id: u64, | ||
next_handle_id: u64, | ||
_h: PhantomData<H>, | ||
} | ||
|
||
impl<H: Handle> HandleGenerator<H> { | ||
pub fn generate(&mut self) -> H { | ||
let handle_id = self.next_handle_id; | ||
self.next_handle_id += self.next_handle_id.wrapping_add(1); | ||
|
||
H::from_raw(RawHandle { | ||
generator_id: self.generator_id, | ||
handle_id, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct HandleGeneratorGenerator<H: Handle> { | ||
next_handle_generator_id: AtomicU64, | ||
_h: PhantomData<H>, | ||
} | ||
|
||
impl<H: Handle> HandleGeneratorGenerator<H> { | ||
pub const fn new() -> Self { | ||
Self { | ||
next_handle_generator_id: AtomicU64::new(0), | ||
_h: PhantomData, | ||
} | ||
} | ||
|
||
pub fn generate(&self) -> HandleGenerator<H> { | ||
// There is no synchronization required and we only care about each thread seeing a | ||
// unique value. | ||
let generator_id = self | ||
.next_handle_generator_id | ||
.fetch_add(1, Ordering::Relaxed); | ||
|
||
HandleGenerator { | ||
generator_id, | ||
next_handle_id: 0, | ||
_h: PhantomData, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters