You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.9 KiB

//! Memory holding types
use super::*;
use ::bytes::{
Bytes,
BytesMut,
BufMut,
Buf,
};
use std::{
pin::Pin,
task::{Context, Poll},
io,
};
use tokio::io::AsyncWrite;
// TODO: For when `MemoryMut` is vectorised, this will be the max size for each allocation
pub const PAGE_SIZE: usize = 4096;
/// The memory hold of a cahce entry.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Memory(pub(super) Bytes);
/// Unfrozen memory hold of a cache entry.
//TODO: Allow for non-resizing writes by making this `LinkedList<BytesMut>` or something, then we can implement `BufMut` for this
#[derive(Debug, PartialEq, Eq, Hash, Default)]
pub struct MemoryMut(pub(super) BytesMut); //TODO: Type will be `SmallVec<[BytesMut; 1]>` or `LinkedList<BytesMut>` or maybe a `smolset` type (probably linkedlist is best actually...).
impl MemoryMut
{
/// Freeze this mutable memory into an immutable one
pub fn freeze(self) -> Memory
{
Memory(self.0.freeze())
}
/// Create a new, empty mutable memory pool
pub fn new() -> Self
{
Self(BytesMut::new())
}
}
impl AsyncWrite for MemoryMut
{
#[inline(always)] fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
//TODO: When vectorised (see above TODO), this method will make it a single contiguous `BytesMut`
Poll::Ready(Ok(()))
}
#[inline(always)] fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
//TODO: When vectorised (see above TODO), this method will make it a single contiguous `BytesMut`
Poll::Ready(Ok(()))
}
fn poll_write(self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, io::Error>> {
//TODO: When vectorised, this will either: fill the previous allocation with enough out of `buf`; then create a new allocation and write the rest there; repeat.
// This is kinda paging
self.get_mut().0.extend_from_slice(buf);
Poll::Ready(Ok(buf.len()))
}
}