Improve the ergonomics of creating local buffers (#10347)
This PR renames `language::Buffer::new` to `language::Buffer::local` and simplifies its interface. Instead of taking a replica id (which should always be 0 for the local case) and a `BufferId`, which was awkward and verbose to construct, it simply takes text and a `cx`. It uses the `cx` to derive a `BufferId` from the `EntityId` associated with the `cx`, which should always be positive based on the following analysis... We convert the entity id to a u64 using this method on `EntityId`, which is defined by macros in the `slotmap` crate: ```rust pub fn as_ffi(self) -> u64 { (u64::from(self.version.get()) << 32) | u64::from(self.idx) } ``` If you look at the type of `version` in `KeyData`, it is non-zero: ```rust #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct KeyData { idx: u32, version: NonZeroU32, } ``` This commit also adds `Context::reserve_model` and `Context::insert_model` to determine a model's entity ID before it is created, which we need in order to assign a `BufferId` in the background when loading a buffer asynchronously. Release Notes: - N/A --------- Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
This commit is contained in:
parent
664efef76b
commit
7abb63cfda
31 changed files with 376 additions and 519 deletions
|
@ -1,6 +1,7 @@
|
|||
use crate::{
|
||||
AnyView, AnyWindowHandle, AppContext, AsyncAppContext, Context, Effect, Entity, EntityId,
|
||||
EventEmitter, Model, Subscription, Task, View, WeakModel, WindowContext, WindowHandle,
|
||||
EventEmitter, Model, Reservation, Subscription, Task, View, WeakModel, WindowContext,
|
||||
WindowHandle,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use derive_more::{Deref, DerefMut};
|
||||
|
@ -229,6 +230,18 @@ impl<'a, T> Context for ModelContext<'a, T> {
|
|||
self.app.new_model(build_model)
|
||||
}
|
||||
|
||||
fn reserve_model<U: 'static>(&mut self) -> Reservation<U> {
|
||||
self.app.reserve_model()
|
||||
}
|
||||
|
||||
fn insert_model<U: 'static>(
|
||||
&mut self,
|
||||
reservation: Reservation<U>,
|
||||
build_model: impl FnOnce(&mut ModelContext<'_, U>) -> U,
|
||||
) -> Self::Result<Model<U>> {
|
||||
self.app.insert_model(reservation, build_model)
|
||||
}
|
||||
|
||||
fn update_model<U: 'static, R>(
|
||||
&mut self,
|
||||
handle: &Model<U>,
|
||||
|
@ -237,13 +250,6 @@ impl<'a, T> Context for ModelContext<'a, T> {
|
|||
self.app.update_model(handle, update)
|
||||
}
|
||||
|
||||
fn update_window<R, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<R>
|
||||
where
|
||||
F: FnOnce(AnyView, &mut WindowContext<'_>) -> R,
|
||||
{
|
||||
self.app.update_window(window, update)
|
||||
}
|
||||
|
||||
fn read_model<U, R>(
|
||||
&self,
|
||||
handle: &Model<U>,
|
||||
|
@ -255,6 +261,13 @@ impl<'a, T> Context for ModelContext<'a, T> {
|
|||
self.app.read_model(handle, read)
|
||||
}
|
||||
|
||||
fn update_window<R, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<R>
|
||||
where
|
||||
F: FnOnce(AnyView, &mut WindowContext<'_>) -> R,
|
||||
{
|
||||
self.app.update_window(window, update)
|
||||
}
|
||||
|
||||
fn read_window<U, R>(
|
||||
&self,
|
||||
window: &WindowHandle<U>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue