BufferPool

Struct BufferPool 

Source
pub struct BufferPool<T> {
    pub buffer_size: usize,
    /* private fields */
}
Expand description

A pool of real/complex buffer objects. Buffers are dynamically created as needed and reused if previously Droped. Buffers are never freed. Instead buffers are kept in reserve and reused when a new buffer is requested.

 use pitch_detection::utils::buffer::BufferPool;

 let mut buffers = BufferPool::new(3);
 let buf_cell1 = buffers.get_real_buffer();
 {
     // This buffer won't be dropped until the end of the function
     let mut buf1 = buf_cell1.borrow_mut();
     buf1[0] = 5.5;
 }
 {
     // This buffer will be dropped when the scope ends
     let buf_cell2 = buffers.get_real_buffer();
     let mut buf2 = buf_cell2.borrow_mut();
     buf2[1] = 6.6;
 }
 {
     // This buffer will be dropped when the scope ends
     // It is the same buffer that was just used (i.e., it's a reused buffer)
     let buf_cell3 = buffers.get_real_buffer();
     let mut buf3 = buf_cell3.borrow_mut();
     buf3[2] = 7.7;
 }
 // The first buffer we asked for should not have been reused.
 assert_eq!(&buf_cell1.borrow()[..], &[5.5, 0., 0.]);
 let buf_cell2 = buffers.get_real_buffer();
 // The second buffer was reused because it was dropped and then another buffer was requested.
 assert_eq!(&buf_cell2.borrow()[..], &[0.0, 6.6, 7.7]);

Fields§

§buffer_size: usize

Implementations§

Source§

impl<T: Float> BufferPool<T>

Source

pub fn new(buffer_size: usize) -> Self

Source

pub fn get_real_buffer(&mut self) -> Rc<RefCell<Vec<T>>>

Get a reference to a buffer that can e used until it is Droped. Call .borrow_mut() to get a reference to a mutable version of the buffer.

Source

pub fn get_complex_buffer(&mut self) -> Rc<RefCell<Vec<Complex<T>>>>

Get a reference to a buffer that can e used until it is Droped. Call .borrow_mut() to get a reference to a mutable version of the buffer.

Trait Implementations§

Source§

impl<T: Debug> Debug for BufferPool<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BufferPool<T>

§

impl<T> !RefUnwindSafe for BufferPool<T>

§

impl<T> !Send for BufferPool<T>

§

impl<T> !Sync for BufferPool<T>

§

impl<T> Unpin for BufferPool<T>

§

impl<T> !UnwindSafe for BufferPool<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.