pub struct Head<S, L>{ /* private fields */ }
Expand description
A VectorDiff
stream adapter that presents a limited view of the
underlying ObservableVector
’s items. The view starts from index 0.
This is the opposite of Tail
, which starts from the
end.
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
to a certain size. Then this Head
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().head(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 first 3 values!
assert_next_eq!(sub, VectorDiff::Append { values: vector!['a', 'b', 'c'] });
// Let's recap what we have. `ob` is our `ObservableVector`,
// `sub` is the “limited view” of `ob`:
// | `ob` | a b c d |
// | `sub` | a b c |
// Front push a value.
ob.push_front('e');
// We get three `VectorDiff`s!
assert_next_eq!(sub, VectorDiff::PopBack);
assert_next_eq!(sub, VectorDiff::PushFront { value: 'e' });
// Let's recap what we have:
// | `ob` | e a b c d |
// | `sub` | e a b |
// ^ ^
// | |
// | removed with `VectorDiff::PopBack`
// added with `VectorDiff::PushFront`
assert_pending!(sub);
drop(ob);
assert_closed!(sub);
Implementations§
Source§impl<S> Head<S, EmptyLimitStream>
impl<S> Head<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 Head
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> Head<S, L>
impl<S, L> Head<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 Head
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 Head
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 Head
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.