Added future support for paged ring-buffers (mod page) (TODO: not started impl yet.) Fortune for mapped-file's current commit: Small blessing − 小吉master
parent
eca5ac5350
commit
4f427e88e4
@ -0,0 +1,87 @@
|
|||||||
|
//! Extensions
|
||||||
|
use super::*;
|
||||||
|
use std::{
|
||||||
|
ops,
|
||||||
|
borrow::{
|
||||||
|
Borrow, BorrowMut
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Defer an expression call
|
||||||
|
macro_rules! defer {
|
||||||
|
(move => $expr:expr) => {
|
||||||
|
$crate::ext::Deferred(move || {
|
||||||
|
$expr
|
||||||
|
})
|
||||||
|
};
|
||||||
|
(ref => $expr:expr) => {
|
||||||
|
$crate::ext::Deferred(|| {
|
||||||
|
$expr
|
||||||
|
})
|
||||||
|
};
|
||||||
|
(move $value:expr => $expr:expr) => {
|
||||||
|
$crate::ext::DeferredDrop($value, move |a| {
|
||||||
|
$expr(a)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
(ref $value:expr => $expr:expr) => {
|
||||||
|
$crate::ext::DeferredDrop($value, |a| {
|
||||||
|
$expr(a)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
pub(crate) use defer;
|
||||||
|
|
||||||
|
/// Defer calling `F` until the destructor is ran
|
||||||
|
pub struct Deferred<F: ?Sized + FnOnce() -> ()>(F);
|
||||||
|
|
||||||
|
/// Defer dropping this value until the container is dropped. The function `F` will be called on the value at drop time.
|
||||||
|
pub struct DeferredDrop<T, F: ?Sized + FnOnce(T) -> ()>(T,F);
|
||||||
|
|
||||||
|
impl<F: ?Sized+ FnOnce() -> ()> ops::Drop for Deferred<F>
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.0();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, F: ?Sized+ FnOnce(T) -> ()> ops::Drop for DeferredDrop<T, F>
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.1(self.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, F: ?Sized + FnOnce(T) -> ()> ops::DerefMut for DeferredDrop<T,F>
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T, F: ?Sized + FnOnce(T) -> ()> ops::Deref for DeferredDrop<T,F>
|
||||||
|
{
|
||||||
|
type Target = T;
|
||||||
|
#[inline]
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, F: ?Sized + FnOnce(T) -> ()> Borrow<T> for DeferredDrop<T,F>
|
||||||
|
{
|
||||||
|
#[inline(always)]
|
||||||
|
fn borrow(&self) -> &T {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, F: ?Sized + FnOnce(T) -> ()> BorrowMut<T> for DeferredDrop<T,F>
|
||||||
|
{
|
||||||
|
#[inline(always)]
|
||||||
|
fn borrow_mut(&mut self) -> &mut T {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
//! `mmap()` based (regular or huge) page-sized ring-buffers over arbitrary files
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
//TODO: Implement this w/ MAP_FIXED
|
||||||
|
|
||||||
|
pub mod buffer;
|
@ -0,0 +1,78 @@
|
|||||||
|
//! Traits and types used for mapping a R^W send-recv buffer `(tx, rx)`
|
||||||
|
//!
|
||||||
|
//! See `MappedFile::try_new_buffer()`
|
||||||
|
use super::*;
|
||||||
|
use std::{
|
||||||
|
borrow::Borrow,
|
||||||
|
ops,
|
||||||
|
sync,
|
||||||
|
rc,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub trait TwoBufferProvider<T: ?Sized>
|
||||||
|
{
|
||||||
|
type ControlWrapper: Borrow<T>;
|
||||||
|
|
||||||
|
fn as_wrapper(&self) -> &Self::ControlWrapper;
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn inner(&self) -> &T
|
||||||
|
{
|
||||||
|
self.as_wrapper().borrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_boxed(value: Box<T>) -> Box<Self>;
|
||||||
|
//TODO: How do we give enough info to caller to create this?
|
||||||
|
}
|
||||||
|
|
||||||
|
/// For thread-sharable buffer holds
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Shared<T: ?Sized>(sync::Arc<T>);
|
||||||
|
|
||||||
|
/// For non thread-sharable buffer holds
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Private<T: ?Sized>(rc::Rc<T>);
|
||||||
|
|
||||||
|
impl<T: ?Sized> TwoBufferProvider<T> for Shared<T> {
|
||||||
|
type ControlWrapper = sync::Arc<T>;
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn as_wrapper(&self) -> &Self::ControlWrapper {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn from_boxed(value: Box<T>) ->Box<Self> {
|
||||||
|
Box::new(Self(value.into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized + AsRawFd> AsRawFd for Shared<T>
|
||||||
|
{
|
||||||
|
#[inline(always)]
|
||||||
|
fn as_raw_fd(&self) -> RawFd {
|
||||||
|
self.as_wrapper().as_raw_fd()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized> TwoBufferProvider<T> for Private<T> {
|
||||||
|
type ControlWrapper = rc::Rc<T>;
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn as_wrapper(&self) -> &Self::ControlWrapper {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn from_boxed(value: Box<T>) ->Box<Self> {
|
||||||
|
Box::new(Self(value.into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized + AsRawFd> AsRawFd for Private<T>
|
||||||
|
{
|
||||||
|
#[inline(always)]
|
||||||
|
fn as_raw_fd(&self) -> RawFd {
|
||||||
|
self.as_wrapper().as_raw_fd()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue