pub struct Tail<S, L>{ /* private fields */ }
Expand description
A VectorDiff
stream adapter that presents a reversed limited view
of the underlying ObservableVector
s items. The view starts from the
last index of the ObservableVector
, i.e. it starts from the end. This
is the opposite of Head
, which starts from 0.
For example, let S
be a Stream<Item = VectorDiff>
. The Vector
represented by S
can have any length, but one may want to virtually
limit this Vector
from the end to a certain size. Then this Tail
adapter is appropriate.
An internal buffered vector is kept so that the adapter knows which
values can be added when the limit is increased, or when values are
removed and new values must be inserted. This fact is important if the
items of the Vector
have a non-negligible size.
It’s okay to have a limit larger than the length of the observed
Vector
.
§Examples
use eyeball_im::{ObservableVector, VectorDiff};
use eyeball_im_util::vector::VectorObserverExt;
use imbl::vector;
use stream_assert::{assert_closed, assert_next_eq, assert_pending};
// Our vector.
let mut ob = ObservableVector::<char>::new();
let (values, mut sub) = ob.subscribe().tail(3);
assert!(values.is_empty());
assert_pending!(sub);
// Append multiple values.
ob.append(vector!['a', 'b', 'c', 'd']);
// We get a `VectorDiff::Append` with the latest 3 values!
assert_next_eq!(sub, VectorDiff::Append { values: vector!['b', 'c', 'd'] });
// Let's recap what we have. `ob` is our `ObservableVector`,
// `sub` is the “limited view” of `ob`:
// | `ob` | a b c d |
// | `sub` | b c d |
// Append multiple other values.
ob.append(vector!['e', 'f']);
// We get three `VectorDiff`s!
assert_next_eq!(sub, VectorDiff::PopFront);
assert_next_eq!(sub, VectorDiff::PopFront);
assert_next_eq!(sub, VectorDiff::Append { values: vector!['e', 'f'] });
// Let's recap what we have:
// | `ob` | a b c d e f |
// | `sub` | d e f |
// ^ ^ ^^^
// | | |
// | | added with `VectorDiff::Append { .. }`
// | removed with `VectorDiff::PopFront`
// removed with `VectorDiff::PopFront`
assert_pending!(sub);
drop(ob);
assert_closed!(sub);
Implementations§
Source§impl<S> Tail<S, EmptyLimitStream>
impl<S> Tail<S, EmptyLimitStream>
Sourcepub fn new(
initial_values: Vector<VectorDiffContainerStreamElement<S>>,
inner_stream: S,
limit: usize,
) -> (Vector<VectorDiffContainerStreamElement<S>>, Self)
pub fn new( initial_values: Vector<VectorDiffContainerStreamElement<S>>, inner_stream: S, limit: usize, ) -> (Vector<VectorDiffContainerStreamElement<S>>, Self)
Create a new Tail
with the given (unlimited) initial values,
stream of VectorDiff
updates for those values, and a fixed limit.
Returns the truncated initial values as well as a stream of updates that ensure that the resulting vector never exceeds the given limit.
Source§impl<S, L> Tail<S, L>
impl<S, L> Tail<S, L>
Sourcepub fn dynamic(
initial_values: Vector<VectorDiffContainerStreamElement<S>>,
inner_stream: S,
limit_stream: L,
) -> Self
pub fn dynamic( initial_values: Vector<VectorDiffContainerStreamElement<S>>, inner_stream: S, limit_stream: L, ) -> Self
Create a new Tail
with the given (unlimited) initial values,
stream of VectorDiff
updates for those values, and a stream of
limits.
This is equivalent to dynamic_with_initial_limit
where the
initial_limit
is 0, except that it doesn’t return the limited
vector as it would be empty anyways.
Note that the returned Tail
won’t produce anything until the first
limit is produced by the limit stream.
Sourcepub fn dynamic_with_initial_limit(
initial_values: Vector<VectorDiffContainerStreamElement<S>>,
inner_stream: S,
initial_limit: usize,
limit_stream: L,
) -> (Vector<VectorDiffContainerStreamElement<S>>, Self)
pub fn dynamic_with_initial_limit( initial_values: Vector<VectorDiffContainerStreamElement<S>>, inner_stream: S, initial_limit: usize, limit_stream: L, ) -> (Vector<VectorDiffContainerStreamElement<S>>, Self)
Create a new Tail
with the given (unlimited) initial values,
stream of VectorDiff
updates for those values, and an initial
limit as well as a stream of new limits.