pub struct PerBuffer<T: 'static>(/* private fields */);Expand description
A struct to associate one T to each Buffer
This is very useful to implement the “parser pattern”, where you
have one parser per Buffer, acting on changes that take place on
each Buffer.
Implementations§
Source§impl<T: 'static> PerBuffer<T>
impl<T: 'static> PerBuffer<T>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Returns a new mapping of Buffers to a type T
Since this function is const, you can conveniently place it
in a static variable, in order to record one T for every
Buffer that you want to associate the struct to.
This is very useful in order to create the “parser pattern” on
a number of different Buffers, letting you store things and
retrieve them as needed.
§Note
This function will not automatically add new Buffers to
the list. To do that, you should add a hook on Buffer,
that calls PerBuffer::register.
Additionally, you will probably also want to setup a hook
that calls PerBuffer::unregister on BufferClosed
Sourcepub fn register<'p>(
&'p self,
pa: &'p mut Pass,
handle: &'p Handle,
new_value: T,
) -> (&'p mut T, &'p mut Buffer)
pub fn register<'p>( &'p self, pa: &'p mut Pass, handle: &'p Handle, new_value: T, ) -> (&'p mut T, &'p mut Buffer)
Register a Buffer with an initial value of T
If there was a previous version of T assoiated with the
Buffer, then the new T will replace that old version.
You should most likely call this function on the
WidgetOpened<Buffer> hook, often aliased to just
Buffer.
Sourcepub fn unregister(&self, pa: &mut Pass, handle: &Handle) -> Option<T>
pub fn unregister(&self, pa: &mut Pass, handle: &Handle) -> Option<T>
Unregisters a Buffer
This will remove the Buffer from the list, making future
calls to Self::write return None. You should consider
doing this on the BufferClosed hook.
Returns None if the Buffer wasn’t already registered.
Sourcepub fn get<'b>(&'b self, buffer_pass: &'b impl BufferPass) -> Option<&'b T>
pub fn get<'b>(&'b self, buffer_pass: &'b impl BufferPass) -> Option<&'b T>
Gets a reference to the T associated with a Buffer
This function lets you bipass the normal requirement of a
Pass in order to acquire a T associated with any given
Buffer.
For now, the two types that can be used as [BufferPass]es
are the Buffer itself and a Cursor<Buffer>. These
types are allowed to do this because they are impossible
to acquire without first borrowing from an
RwData<Buffer>, either directly or through a Handle
Will return None if the Buffer in question wasn’t
registered or was unregistered.
§Note: Why is this safe?
From the rest of the operations on this struct, you may glean
that the PerBuffer struct is backed by a RwData, and
the only way to safely access the data in those is through a
Pass.
So why can you suddenly do this without a Pass. Basically,
since you can’t construct a Buffer, the only way to actually
get one is by borrowing from a RwData<Buffer> or
Handle.
Given that, the Pass will already be borrowed, and the
Buffer will act as an “extensionto the Pass’s borrow”,
and will become invalid at the same time.
It is important to note that this is only safe because
Buffers can’t be acquired without a Pass.
Sourcepub fn get_mut<'b>(
&'b self,
buffer: &'b mut impl BufferPass,
) -> Option<&'b mut T>
pub fn get_mut<'b>( &'b self, buffer: &'b mut impl BufferPass, ) -> Option<&'b mut T>
Gets a mutable reference to the T associated with a
Buffer
This function lets you bipass the normal requirement of a
Pass in order to acquire a T associated with any given
Buffer.
For now, the two types that can be used as [BufferPass]es
are the Buffer itself and a Cursor<Buffer>. These
types are allowed to do this because they are impossible
to acquire without first borrowing from an RwData<Buffer>,
either directly or through a Handle
Will return None if the Buffer in question wasn’t
registered or was unregistered.
§Note: Why is this safe?
For the same reason that PerBuffer::get is safe. However,
in order to prevent multiple borrowings from happening at the
same time, this will take a mutable borrow of the Buffer,
acting much like a &mut Pass in that regard.
Sourcepub fn write<'p>(
&'p self,
pa: &'p mut Pass,
handle: &'p Handle,
) -> Option<(&'p mut T, &'p mut Buffer)>
pub fn write<'p>( &'p self, pa: &'p mut Pass, handle: &'p Handle, ) -> Option<(&'p mut T, &'p mut Buffer)>
Writes to the Buffer and the T at the same time
Will return None if the Buffer in question wasn’t
registered or was unregistered.
Sourcepub fn write_with<'p, Tup: WriteableTuple<'p, impl Any>>(
&'p self,
pa: &'p mut Pass,
handle: &'p Handle,
tup: Tup,
) -> Option<(&'p mut T, &'p mut Buffer, Tup::Return)>
pub fn write_with<'p, Tup: WriteableTuple<'p, impl Any>>( &'p self, pa: &'p mut Pass, handle: &'p Handle, tup: Tup, ) -> Option<(&'p mut T, &'p mut Buffer, Tup::Return)>
Writes to the Buffer and a tuple of writeable types
This is an extension to the Pass::write_many method,
allowing you to write to many RwData-like structs at once.
Returns None if any two structs, either in the Handle,
tuple, or PerBuffer point to the same thing, or if the
Buffer wasn’t registered or was unregistered.
Sourcepub fn write_many<'p, const N: usize>(
&'p self,
pa: &'p mut Pass,
handles: [&'p Handle; N],
) -> Option<[(&'p mut T, &'p mut Buffer); N]>
pub fn write_many<'p, const N: usize>( &'p self, pa: &'p mut Pass, handles: [&'p Handle; N], ) -> Option<[(&'p mut T, &'p mut Buffer); N]>
Tries to write to a bunch of Buffers and their respective
Ts
Returns None if any two Handles point to the same
Buffer, or if any of the Buffers weren’t registered or
were unregistered.
Sourcepub fn write_many_with<'p, const N: usize, Tup: WriteableTuple<'p, impl Any>>(
&'p self,
pa: &'p mut Pass,
handles: [&'p Handle; N],
tup: Tup,
) -> Option<([(&'p mut T, &'p mut Buffer); N], Tup::Return)>
pub fn write_many_with<'p, const N: usize, Tup: WriteableTuple<'p, impl Any>>( &'p self, pa: &'p mut Pass, handles: [&'p Handle; N], tup: Tup, ) -> Option<([(&'p mut T, &'p mut Buffer); N], Tup::Return)>
Fusion of write_many and write_with
Returns None if any two structs point to the same
RwData-like struct, or if any of the Buffers weren’t
registered or were unregistered.
Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for PerBuffer<T>
impl<T> !RefUnwindSafe for PerBuffer<T>
impl<T> Send for PerBuffer<T>
impl<T> Sync for PerBuffer<T>
impl<T> Unpin for PerBuffer<T>
impl<T> !UnwindSafe for PerBuffer<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.