mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-25 01:02:20 -04:00
chore(sdk): Rename Updates to ObservableUpdates.
This commit is contained in:
@@ -29,8 +29,8 @@ use super::{
|
||||
type ChunkLength = usize;
|
||||
|
||||
/// A type that transforms a `Vec<Update<Item, Gap>>` (given by
|
||||
/// [`Updates::take`](super::Updates::take)) into a `Vec<VectorDiff<Item>>`
|
||||
/// (this type). Basically, it helps to consume a
|
||||
/// [`ObservableUpdates::take`](super::ObservableUpdates::take)) into a
|
||||
/// `Vec<VectorDiff<Item>>` (this type). Basically, it helps to consume a
|
||||
/// [`LinkedChunk<CAP, Item, Gap>`](super::LinkedChunk) as if it was an
|
||||
/// [`eyeball_im::ObservableVector<Item>`].
|
||||
pub struct AsVector<Item, Gap> {
|
||||
@@ -47,7 +47,8 @@ pub struct AsVector<Item, Gap> {
|
||||
impl<Item, Gap> AsVector<Item, Gap> {
|
||||
/// Create a new [`AsVector`].
|
||||
///
|
||||
/// `updates` is the inner value of [`Updates`][super::updates::Updates].
|
||||
/// `updates` is the inner value of
|
||||
/// [`ObservableUpdates`][super::updates::ObservableUpdates].
|
||||
/// It's required to read the new [`Update`]s. `token` is the
|
||||
/// [`ReaderToken`] necessary for this type to read the [`Update`]s.
|
||||
/// `chunk_iterator` is the iterator of all [`Chunk`](super::Chunk)s, used
|
||||
@@ -277,11 +278,11 @@ impl UpdateToVectorDiff {
|
||||
.chunks
|
||||
.iter()
|
||||
.position(|(chunk_identifier, _)| chunk_identifier == next)
|
||||
// SAFETY: Assuming `LinkedChunk` and `Updates` are not buggy, and
|
||||
// assuming `Self::chunks` is correctly initialized, it is not
|
||||
// possible to insert a chunk between two chunks where one does not
|
||||
// exist. If this predicate fails, it means `LinkedChunk` or
|
||||
// `Updates` contain a bug.
|
||||
// SAFETY: Assuming `LinkedChunk` and `ObservableUpdates` are not
|
||||
// buggy, and assuming `Self::chunks` is correctly initialized, it
|
||||
// is not possible to insert a chunk between two chunks where one
|
||||
// does not exist. If this predicate fails, it means `LinkedChunk`
|
||||
// or `ObservableUpdates` contain a bug.
|
||||
.expect("Inserting new chunk: The chunk is not found");
|
||||
|
||||
debug_assert!(
|
||||
@@ -308,10 +309,10 @@ impl UpdateToVectorDiff {
|
||||
.position(|(chunk_identifier, _)| {
|
||||
chunk_identifier == expected_chunk_identifier
|
||||
})
|
||||
// SAFETY: Assuming `LinkedChunk` and `Updates` are not buggy, and assuming
|
||||
// `Self::chunks` is correctly initialized, it is not possible to remove a
|
||||
// chunk that does not exist. If this predicate fails, it means
|
||||
// `LinkedChunk` or `Updates` contain a bug.
|
||||
// SAFETY: Assuming `LinkedChunk` and `ObservableUpdates` are not buggy, and
|
||||
// assuming `Self::chunks` is correctly initialized, it is not possible to
|
||||
// remove a chunk that does not exist. If this predicate fails, it means
|
||||
// `LinkedChunk` or `ObservableUpdates` contain a bug.
|
||||
.expect("Removing a chunk: The chunk is not found");
|
||||
|
||||
// It's OK to ignore the result. The `chunk_index` exists because it's been
|
||||
@@ -341,11 +342,11 @@ impl UpdateToVectorDiff {
|
||||
|
||||
// Chunk has not been found.
|
||||
ControlFlow::Continue(..) => {
|
||||
// SAFETY: Assuming `LinkedChunk` and `Updates` are not buggy, and
|
||||
// assuming `Self::chunks` is correctly initialized, it is not
|
||||
// possible to push items on a chunk that does not exist. If this
|
||||
// predicate fails, it means `LinkedChunk` or `Updates` contain a
|
||||
// bug.
|
||||
// SAFETY: Assuming `LinkedChunk` and `ObservableUpdates` are not
|
||||
// buggy, and assuming `Self::chunks` is correctly initialized, it
|
||||
// is not possible to push items on a chunk that does not exist. If
|
||||
// this predicate fails, it means `LinkedChunk` or
|
||||
// `ObservableUpdates` contain a bug.
|
||||
panic!("Pushing items: The chunk is not found");
|
||||
}
|
||||
}
|
||||
@@ -380,10 +381,10 @@ impl UpdateToVectorDiff {
|
||||
.find_map(|(chunk_identifier, length)| {
|
||||
(*chunk_identifier == expected_chunk_identifier).then_some(length)
|
||||
})
|
||||
// SAFETY: Assuming `LinkedChunk` and `Updates` are not buggy, and assuming
|
||||
// `Self::chunks` is correctly initialized, it is not possible to detach
|
||||
// items from a chunk that does not exist. If this predicate fails, it means
|
||||
// `LinkedChunk` or `Updates` contain a bug.
|
||||
// SAFETY: Assuming `LinkedChunk` and `ObservableUpdates` are not buggy, and
|
||||
// assuming `Self::chunks` is correctly initialized, it is not possible to
|
||||
// detach items from a chunk that does not exist. If this predicate fails,
|
||||
// it means `LinkedChunk` or `ObservableUpdates` contain a bug.
|
||||
.expect("Detach last items: The chunk is not found");
|
||||
|
||||
*length = new_length;
|
||||
|
||||
@@ -210,7 +210,7 @@ pub struct LinkedChunk<const CHUNK_CAPACITY: usize, Item, Gap> {
|
||||
/// All updates that have been made on this `LinkedChunk`. If this field is
|
||||
/// `Some(…)`, update history is enabled, otherwise, if it's `None`, update
|
||||
/// history is disabled.
|
||||
updates: Option<Updates<Item, Gap>>,
|
||||
updates: Option<ObservableUpdates<Item, Gap>>,
|
||||
|
||||
/// Marker.
|
||||
marker: PhantomData<Box<Chunk<CHUNK_CAPACITY, Item, Gap>>>,
|
||||
@@ -234,8 +234,9 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> {
|
||||
|
||||
/// Create a new [`Self`] with a history of updates.
|
||||
///
|
||||
/// When [`Self`] is built with update history, the [`Updates::take`] method
|
||||
/// must be called to consume and clean the updates. See [`Self::updates`].
|
||||
/// When [`Self`] is built with update history, the
|
||||
/// [`ObservableUpdates::take`] method must be called to consume and
|
||||
/// clean the updates. See [`Self::updates`].
|
||||
pub fn new_with_update_history() -> Self {
|
||||
Self {
|
||||
links: Ends {
|
||||
@@ -245,7 +246,7 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> {
|
||||
},
|
||||
length: 0,
|
||||
chunk_identifier_generator: ChunkIdentifierGenerator::new_from_scratch(),
|
||||
updates: Some(Updates::new()),
|
||||
updates: Some(ObservableUpdates::new()),
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -721,16 +722,18 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> {
|
||||
.skip(position.index()))
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the `LinkedChunk` updates, aka [`Updates`].
|
||||
/// Get a mutable reference to the `LinkedChunk` updates, aka
|
||||
/// [`ObservableUpdates`].
|
||||
///
|
||||
/// If the `Option` becomes `None`, it will disable update history. Thus, be
|
||||
/// careful when you want to empty the update history: do not use
|
||||
/// `Option::take()` directly but rather [`Updates::take`] for example.
|
||||
/// `Option::take()` directly but rather [`ObservableUpdates::take`] for
|
||||
/// example.
|
||||
///
|
||||
/// It returns `None` if updates are disabled, i.e. if this linked chunk has
|
||||
/// been constructed with [`Self::new`], otherwise, if it's been constructed
|
||||
/// with [`Self::new_with_update_history`], it returns `Some(…)`.
|
||||
pub fn updates(&mut self) -> Option<&mut Updates<Item, Gap>> {
|
||||
pub fn updates(&mut self) -> Option<&mut ObservableUpdates<Item, Gap>> {
|
||||
self.updates.as_mut()
|
||||
}
|
||||
|
||||
@@ -1046,7 +1049,7 @@ impl<const CAPACITY: usize, Item, Gap> Chunk<CAPACITY, Item, Gap> {
|
||||
&mut self,
|
||||
mut new_items: I,
|
||||
chunk_identifier_generator: &ChunkIdentifierGenerator,
|
||||
updates: &mut Option<Updates<Item, Gap>>,
|
||||
updates: &mut Option<ObservableUpdates<Item, Gap>>,
|
||||
) -> &mut Self
|
||||
where
|
||||
I: Iterator<Item = Item> + ExactSizeIterator,
|
||||
@@ -1128,7 +1131,7 @@ impl<const CAPACITY: usize, Item, Gap> Chunk<CAPACITY, Item, Gap> {
|
||||
fn insert_next(
|
||||
&mut self,
|
||||
mut new_chunk_ptr: NonNull<Self>,
|
||||
updates: &mut Option<Updates<Item, Gap>>,
|
||||
updates: &mut Option<ObservableUpdates<Item, Gap>>,
|
||||
) -> &mut Self
|
||||
where
|
||||
Gap: Clone,
|
||||
@@ -1172,7 +1175,7 @@ impl<const CAPACITY: usize, Item, Gap> Chunk<CAPACITY, Item, Gap> {
|
||||
///
|
||||
/// Be careful: `self` won't belong to `LinkedChunk` anymore, and should be
|
||||
/// dropped appropriately.
|
||||
fn unlink(&mut self, updates: &mut Option<Updates<Item, Gap>>) {
|
||||
fn unlink(&mut self, updates: &mut Option<ObservableUpdates<Item, Gap>>) {
|
||||
let previous_ptr = self.previous;
|
||||
let next_ptr = self.next;
|
||||
|
||||
|
||||
@@ -92,15 +92,15 @@ pub enum Update<Item, Gap> {
|
||||
EndReattachItems,
|
||||
}
|
||||
|
||||
/// A collection of [`Update`].
|
||||
/// A collection of [`Update`]s that can be observed.
|
||||
///
|
||||
/// Get a value for this type with [`LinkedChunk::updates`].
|
||||
pub struct Updates<Item, Gap> {
|
||||
pub struct ObservableUpdates<Item, Gap> {
|
||||
pub(super) inner: Arc<RwLock<UpdatesInner<Item, Gap>>>,
|
||||
}
|
||||
|
||||
impl<Item, Gap> Updates<Item, Gap> {
|
||||
/// Create a new [`Self`].
|
||||
impl<Item, Gap> ObservableUpdates<Item, Gap> {
|
||||
/// Create a new [`ObservableUpdates`].
|
||||
pub(super) fn new() -> Self {
|
||||
Self { inner: Arc::new(RwLock::new(UpdatesInner::new())) }
|
||||
}
|
||||
@@ -148,20 +148,20 @@ impl<Item, Gap> Updates<Item, Gap> {
|
||||
/// [`UpdatesInner`].
|
||||
pub(super) type ReaderToken = usize;
|
||||
|
||||
/// Inner type for [`Updates`].
|
||||
/// Inner type for [`ObservableUpdates`].
|
||||
///
|
||||
/// The particularity of this type is that multiple readers can read the
|
||||
/// updates. A reader has a [`ReaderToken`]. The public API (i.e.
|
||||
/// [`Updates`]) is considered to be the _main reader_ (it has the token
|
||||
/// [`Self::MAIN_READER_TOKEN`]).
|
||||
/// [`ObservableUpdates`]) is considered to be the _main reader_ (it has the
|
||||
/// token [`Self::MAIN_READER_TOKEN`]).
|
||||
///
|
||||
/// An update that have been read by all readers are garbage collected to be
|
||||
/// removed from the memory. An update will never be read twice by the same
|
||||
/// reader.
|
||||
///
|
||||
/// Why do we need multiple readers? The public API reads the updates with
|
||||
/// [`Updates::take`], but the private API must also read the updates for
|
||||
/// example with [`UpdatesSubscriber`]. Of course, they can be multiple
|
||||
/// [`ObservableUpdates::take`], but the private API must also read the updates
|
||||
/// for example with [`UpdatesSubscriber`]. Of course, they can be multiple
|
||||
/// `UpdatesSubscriber`s at the same time. Hence the need of supporting multiple
|
||||
/// readers.
|
||||
pub(super) struct UpdatesInner<Item, Gap> {
|
||||
@@ -270,12 +270,12 @@ impl<Item, Gap> UpdatesInner<Item, Gap> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A subscriber to [`Updates`]. It is helpful to receive updates via a
|
||||
/// [`Stream`].
|
||||
/// A subscriber to [`ObservableUpdates`]. It is helpful to receive updates via
|
||||
/// a [`Stream`].
|
||||
pub(super) struct UpdatesSubscriber<Item, Gap> {
|
||||
/// Weak reference to [`UpdatesInner`].
|
||||
///
|
||||
/// Using a weak reference allows [`Updates`] to be dropped
|
||||
/// Using a weak reference allows [`ObservableUpdates`] to be dropped
|
||||
/// freely even if a subscriber exists.
|
||||
updates: Weak<RwLock<UpdatesInner<Item, Gap>>>,
|
||||
|
||||
@@ -299,7 +299,7 @@ where
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let Some(updates) = self.updates.upgrade() else {
|
||||
// The `Updates` has been dropped. It's time to close this stream.
|
||||
// The `ObservableUpdates` has been dropped. It's time to close this stream.
|
||||
return Poll::Ready(None);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user