Skip to content

Commit

Permalink
feat: better constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alxandr committed Nov 3, 2024
1 parent 13ac13d commit 6f0a917
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
28 changes: 16 additions & 12 deletions crates/aligned-buffer-pool/src/buffer_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
};

pub type UniquePooledAlignedBuffer<
P,
P = RetainAllRetentionPolicy,
const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT,
A = Global,
> = UniqueAlignedBuffer<
Expand All @@ -20,7 +20,7 @@ pub type UniquePooledAlignedBuffer<
>;

pub type SharedPooledAlignedBuffer<
P,
P = RetainAllRetentionPolicy,
const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT,
A = Global,
> = SharedAlignedBuffer<
Expand Down Expand Up @@ -231,28 +231,32 @@ impl<

/// A pool for allocating and recycling aligned buffers.
pub struct AlignedBufferPool<
P: BufferRetentionPolicy,
const ALIGNMENT: usize,
P: BufferRetentionPolicy = RetainAllRetentionPolicy,
const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT,
A: Allocator + Clone = Global,
> {
inner: Arc<AlignedBufferPoolInner<P, ALIGNMENT, WeakAlignedBufferPool<P, ALIGNMENT, A>, A>>,
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize> AlignedBufferPool<P, ALIGNMENT, Global> {
pub fn new(policy: P, capacity: usize) -> Self {
Self::new_in(policy, capacity, Global)
}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize> AlignedBufferPool<P, ALIGNMENT, Global>
where
P: Default,
{
pub fn new(capacity: usize) -> Self {
Self::with_policy(P::default(), capacity)
}

pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_in(capacity, Global)
}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize> AlignedBufferPool<P, ALIGNMENT, Global> {
pub fn with_policy(policy: P, capacity: usize) -> Self {
Self::new_in(policy, capacity, Global)
}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize, A: Allocator + Clone>
AlignedBufferPool<P, ALIGNMENT, A>
where
Expand Down Expand Up @@ -292,8 +296,8 @@ impl<P: BufferRetentionPolicy, const ALIGNMENT: usize, A: Allocator + Clone>
}

pub struct WeakAlignedBufferPool<
P: BufferRetentionPolicy,
const ALIGNMENT: usize,
P: BufferRetentionPolicy = RetainAllRetentionPolicy,
const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT,
A: Allocator + Clone = Global,
> {
inner: Weak<AlignedBufferPoolInner<P, ALIGNMENT, WeakAlignedBufferPool<P, ALIGNMENT, A>, A>>,
Expand Down
55 changes: 36 additions & 19 deletions crates/aligned-buffer-pool/src/serializer_pool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
buffer_pool::{AlignedBufferPoolInner, BufferPoolAllocator, WeakAlignedBufferPoolRef},
BufferRetentionPolicy,
BufferRetentionPolicy, RetainAllRetentionPolicy,
};
use aligned_buffer::{
alloc::{Allocator, Global},
Expand All @@ -18,11 +18,14 @@ use std::{
sync::{Arc, Weak},
};

pub type SerializerAlignedBuffer<P, const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT, A = Global> =
SharedAlignedBuffer<
ALIGNMENT,
BufferPoolAllocator<P, ALIGNMENT, SerializerWeakRef<P, ALIGNMENT, A>, A>,
>;
pub type SerializerAlignedBuffer<
P = RetainAllRetentionPolicy,
const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT,
A = Global,
> = SharedAlignedBuffer<
ALIGNMENT,
BufferPoolAllocator<P, ALIGNMENT, SerializerWeakRef<P, ALIGNMENT, A>, A>,
>;

pub type SerializerPoolAllocator<P, const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT, A = Global> =
BufferPoolAllocator<P, ALIGNMENT, SerializerWeakRef<P, ALIGNMENT, A>, A>;
Expand Down Expand Up @@ -68,7 +71,7 @@ where
}

pub struct SerializerPool<
P: BufferRetentionPolicy,
P: BufferRetentionPolicy = RetainAllRetentionPolicy,
const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT,
A = Global,
> where
Expand All @@ -77,12 +80,35 @@ pub struct SerializerPool<
inner: Arc<Inner<P, ALIGNMENT, A>>,
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize> SerializerPool<P, ALIGNMENT> {
pub fn new(policy: P, capacity: usize) -> Self {
impl<P: BufferRetentionPolicy, const ALIGNMENT: usize> SerializerPool<P, ALIGNMENT, Global>
where
P: Default,
{
pub fn new(capacity: usize) -> Self {
Self::with_policy(P::default(), capacity)
}

pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_in(capacity, Global)
}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize> SerializerPool<P, ALIGNMENT, Global> {
pub fn with_policy(policy: P, capacity: usize) -> Self {
Self::new_in(policy, capacity, Global)
}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize, A: Allocator + Clone>
SerializerPool<P, ALIGNMENT, A>
where
P: Default,
{
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
Self::new_in(P::default(), capacity, alloc)
}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize, A> SerializerPool<P, ALIGNMENT, A>
where
A: Allocator + Clone,
Expand Down Expand Up @@ -136,17 +162,8 @@ where
}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize> SerializerPool<P, ALIGNMENT>
where
P: Default,
{
pub fn with_capacity(capacity: usize) -> Self {
Self::new(P::default(), capacity)
}
}

pub struct Serializer<
P: BufferRetentionPolicy,
P: BufferRetentionPolicy = RetainAllRetentionPolicy,
const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT,
A = Global,
> where
Expand Down

0 comments on commit 6f0a917

Please sign in to comment.