Checkpoint: Cast through std::mem::transmute

This commit is contained in:
Marshall Bowers 2023-09-29 19:41:31 -04:00
parent 103183f494
commit 963f179d7f
6 changed files with 59 additions and 72 deletions

View file

@ -112,6 +112,23 @@ impl DerefMut for AppContext<MainThread> {
}
impl<Thread> AppContext<Thread> {
// TODO: Better names for these?
#[inline]
pub fn downcast(&self) -> &AppContext<()> {
// Any `Thread` can become `()`.
//
// Can't do this in a blanket `Deref` impl, as it infinitely recurses.
unsafe { std::mem::transmute::<&AppContext<Thread>, &AppContext<()>>(self) }
}
#[inline]
pub fn downcast_mut(&mut self) -> &mut AppContext<()> {
// Any `Thread` can become `()`.
//
// Can't do this in a blanket `DerefMut` impl, as it infinitely recurses.
unsafe { std::mem::transmute::<&mut AppContext<Thread>, &mut AppContext<()>>(self) }
}
pub fn text_system(&self) -> &Arc<TextSystem> {
&self.text_system
}
@ -206,7 +223,7 @@ impl<Thread> AppContext<Thread> {
pub(crate) fn update_window<R>(
&mut self,
id: WindowId,
update: impl FnOnce(&mut WindowContext<Thread>) -> R,
update: impl FnOnce(&mut WindowContext) -> R,
) -> Result<R> {
self.update(|cx| {
let mut window = cx
@ -216,7 +233,7 @@ impl<Thread> AppContext<Thread> {
.take()
.unwrap();
let result = update(&mut WindowContext::mutable(cx, &mut window));
let result = update(&mut WindowContext::mutable(cx.downcast_mut(), &mut window));
window.dirty = true;
cx.windows
@ -276,8 +293,8 @@ impl<Thread> AppContext<Thread> {
}
}
impl<Thread: 'static> Context for AppContext<Thread> {
type EntityContext<'a, 'w, T: Send + Sync + 'static> = ModelContext<'a, T, Thread>;
impl Context for AppContext {
type EntityContext<'a, 'w, T: Send + Sync + 'static> = ModelContext<'a, T>;
type Result<T> = T;
fn entity<T: Send + Sync + 'static>(

View file

@ -7,8 +7,8 @@ pub struct ModelContext<'a, T, Thread = ()> {
entity_id: EntityId,
}
impl<'a, T: Send + Sync + 'static, Thread> ModelContext<'a, T, Thread> {
pub(crate) fn mutable(app: &'a mut AppContext<Thread>, entity_id: EntityId) -> Self {
impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> {
pub(crate) fn mutable(app: &'a mut AppContext, entity_id: EntityId) -> Self {
Self {
app: Reference::Mutable(app),
entity_type: PhantomData,
@ -41,7 +41,7 @@ impl<'a, T: Send + Sync + 'static, Thread> ModelContext<'a, T, Thread> {
pub fn observe<E: Send + Sync + 'static>(
&mut self,
handle: &Handle<E>,
on_notify: impl Fn(&mut T, Handle<E>, &mut ModelContext<'_, T, Thread>) + Send + Sync + 'static,
on_notify: impl Fn(&mut T, Handle<E>, &mut ModelContext<'_, T>) + Send + Sync + 'static,
) {
let this = self.handle();
let handle = handle.downgrade();

View file

@ -5,18 +5,18 @@ pub trait Element: 'static {
type State;
type FrameState;
fn layout<Thread>(
fn layout(
&mut self,
state: &mut Self::State,
cx: &mut ViewContext<Self::State, Thread>,
cx: &mut ViewContext<Self::State>,
) -> Result<(LayoutId, Self::FrameState)>;
fn paint<Thread>(
fn paint(
&mut self,
layout: Layout,
state: &mut Self::State,
frame_state: &mut Self::FrameState,
cx: &mut ViewContext<Self::State, Thread>,
cx: &mut ViewContext<Self::State>,
) -> Result<()>;
}
@ -42,16 +42,12 @@ pub trait ParentElement<S> {
}
trait ElementObject<S> {
fn layout<Thread>(
&mut self,
state: &mut S,
cx: &mut ViewContext<S, Thread>,
) -> Result<LayoutId>;
fn paint<Thread>(
fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> Result<LayoutId>;
fn paint(
&mut self,
state: &mut S,
offset: Option<Point<Pixels>>,
cx: &mut ViewContext<S, Thread>,
cx: &mut ViewContext<S>,
) -> Result<()>;
}
@ -139,19 +135,15 @@ impl<E: Element> ElementObject<E::State> for RenderedElement<E> {
pub struct AnyElement<S>(Box<dyn ElementObject<S>>);
impl<S> AnyElement<S> {
pub fn layout<Thread>(
&mut self,
state: &mut S,
cx: &mut ViewContext<S, Thread>,
) -> Result<LayoutId> {
pub fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> Result<LayoutId> {
self.0.layout(state, cx)
}
pub fn paint<Thread>(
pub fn paint(
&mut self,
state: &mut S,
offset: Option<Point<Pixels>>,
cx: &mut ViewContext<S, Thread>,
cx: &mut ViewContext<S>,
) -> Result<()> {
self.0.paint(state, offset, cx)
}

View file

@ -27,10 +27,10 @@ impl<S: 'static + Send + Sync> Element for Div<S> {
type State = S;
type FrameState = Vec<LayoutId>;
fn layout<Thread>(
fn layout(
&mut self,
view: &mut S,
cx: &mut ViewContext<S, Thread>,
cx: &mut ViewContext<S>,
) -> Result<(LayoutId, Self::FrameState)> {
let style = self.computed_style();
let child_layout_ids = if let Some(text_style) = style.text_style(cx) {
@ -45,12 +45,12 @@ impl<S: 'static + Send + Sync> Element for Div<S> {
))
}
fn paint<Thread>(
fn paint(
&mut self,
layout: Layout,
state: &mut S,
child_layouts: &mut Self::FrameState,
cx: &mut ViewContext<S, Thread>,
cx: &mut ViewContext<S>,
) -> Result<()> {
let Layout { order, bounds } = layout;
@ -130,22 +130,18 @@ impl<S: 'static> Div<S> {
offset
}
fn layout_children<Thread>(
&mut self,
view: &mut S,
cx: &mut ViewContext<S, Thread>,
) -> Result<Vec<LayoutId>> {
fn layout_children(&mut self, view: &mut S, cx: &mut ViewContext<S>) -> Result<Vec<LayoutId>> {
self.children
.iter_mut()
.map(|child| child.layout(view, cx))
.collect::<Result<Vec<LayoutId>>>()
}
fn paint_children<Thread>(
fn paint_children(
&mut self,
overflow: &Point<Overflow>,
state: &mut S,
cx: &mut ViewContext<S, Thread>,
cx: &mut ViewContext<S>,
) -> Result<()> {
let scroll_offset = self.scroll_offset(overflow);
for child in &mut self.children {
@ -154,13 +150,13 @@ impl<S: 'static> Div<S> {
Ok(())
}
fn handle_scroll<Thread>(
fn handle_scroll(
&mut self,
_order: u32,
bounds: Bounds<Pixels>,
overflow: Point<Overflow>,
child_layout_ids: &[LayoutId],
cx: &mut ViewContext<S, Thread>,
cx: &mut ViewContext<S>,
) {
if overflow.y == Overflow::Scroll || overflow.x == Overflow::Scroll {
let mut scroll_max = Point::default();

View file

@ -171,7 +171,7 @@ pub struct HighlightStyle {
impl Eq for HighlightStyle {}
impl Style {
pub fn text_style<Thread>(&self, _cx: &WindowContext<Thread>) -> Option<&TextStyleRefinement> {
pub fn text_style(&self, _cx: &WindowContext) -> Option<&TextStyleRefinement> {
if self.text.is_some() {
Some(&self.text)
} else {
@ -180,11 +180,7 @@ impl Style {
}
/// Paints the background of an element styled with this style.
pub fn paint_background<V: 'static, Thread>(
&self,
_bounds: Bounds<Pixels>,
cx: &mut ViewContext<V, Thread>,
) {
pub fn paint_background<V: 'static>(&self, _bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) {
let _rem_size = cx.rem_size();
if let Some(_color) = self.fill.as_ref().and_then(Fill::color) {
todo!();
@ -192,11 +188,7 @@ impl Style {
}
/// Paints the foreground of an element styled with this style.
pub fn paint_foreground<V: 'static, Thread>(
&self,
_bounds: Bounds<Pixels>,
cx: &mut ViewContext<V, Thread>,
) {
pub fn paint_foreground<V: 'static>(&self, _bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) {
let rem_size = cx.rem_size();
if let Some(_color) = self.border_color {

View file

@ -71,8 +71,8 @@ pub struct WindowContext<'a, 'w, Thread = ()> {
window: Reference<'w, Window>,
}
impl<'a, 'w, Thread> WindowContext<'a, 'w, Thread> {
pub(crate) fn mutable(app: &'a mut AppContext<Thread>, window: &'w mut Window) -> Self {
impl<'a, 'w> WindowContext<'a, 'w> {
pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
Self {
thread: PhantomData,
app: Reference::Mutable(app),
@ -158,8 +158,8 @@ impl WindowContext<'_, '_, MainThread> {
}
}
impl<Thread: 'static> Context for WindowContext<'_, '_, Thread> {
type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T, Thread>;
impl Context for WindowContext<'_, '_> {
type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T>;
type Result<T> = T;
fn entity<T: Send + Sync + 'static>(
@ -167,7 +167,7 @@ impl<Thread: 'static> Context for WindowContext<'_, '_, Thread> {
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Handle<T> {
let slot = self.entities.reserve();
let entity = build_entity(&mut ViewContext::<'_, '_, T, Thread>::mutable(
let entity = build_entity(&mut ViewContext::mutable(
&mut *self.app,
&mut self.window,
slot.id,
@ -190,8 +190,8 @@ impl<Thread: 'static> Context for WindowContext<'_, '_, Thread> {
}
}
impl<S, Thread> StackContext for ViewContext<'_, '_, S, Thread> {
fn app(&mut self) -> &mut AppContext<Thread> {
impl<S> StackContext for ViewContext<'_, '_, S> {
fn app(&mut self) -> &mut AppContext {
&mut *self.app
}
@ -226,12 +226,8 @@ pub struct ViewContext<'a, 'w, S, Thread = ()> {
thread: PhantomData<Thread>,
}
impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {
fn mutable(
app: &'a mut AppContext<Thread>,
window: &'w mut Window,
entity_id: EntityId,
) -> Self {
impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
fn mutable(app: &'a mut AppContext, window: &'w mut Window, entity_id: EntityId) -> Self {
Self {
window_cx: WindowContext::mutable(app, window),
entity_id,
@ -247,10 +243,7 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {
pub fn observe<E: Send + Sync + 'static>(
&mut self,
handle: &Handle<E>,
on_notify: impl Fn(&mut S, Handle<E>, &mut ViewContext<'_, '_, S, Thread>)
+ Send
+ Sync
+ 'static,
on_notify: impl Fn(&mut S, Handle<E>, &mut ViewContext<'_, '_, S>) + Send + Sync + 'static,
) {
let this = self.handle();
let handle = handle.downgrade();
@ -280,10 +273,7 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {
self.window.dirty = true;
}
pub(crate) fn erase_state<R>(
&mut self,
f: impl FnOnce(&mut ViewContext<(), Thread>) -> R,
) -> R {
pub(crate) fn erase_state<R>(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R {
let entity_id = self.unit_entity.id;
let mut cx = ViewContext::mutable(
&mut *self.window_cx.app,
@ -294,8 +284,8 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> {
}
}
impl<'a, 'w, S: 'static, Thread: 'static> Context for ViewContext<'a, 'w, S, Thread> {
type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U, Thread>;
impl<'a, 'w, S: 'static> Context for ViewContext<'a, 'w, S> {
type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U>;
type Result<U> = U;
fn entity<T2: Send + Sync + 'static>(