diff --git a/Cargo.lock b/Cargo.lock index 695d4e9b4f..a24b7b81ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2172,7 +2172,6 @@ dependencies = [ "png 0.16.8", "postage", "rand 0.8.3", - "replace_with", "resvg", "seahash", "serde 1.0.125", @@ -3927,12 +3926,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "replace_with" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3a8614ee435691de62bcffcf4a66d91b3594bf1428a5722e79103249a095690" - [[package]] name = "resvg" version = "0.14.0" diff --git a/gpui/Cargo.toml b/gpui/Cargo.toml index d6e8640327..535fe16155 100644 --- a/gpui/Cargo.toml +++ b/gpui/Cargo.toml @@ -19,7 +19,6 @@ pathfinder_color = "0.5" pathfinder_geometry = "0.5" postage = { version = "0.4.1", features = ["futures-traits"] } rand = "0.8.3" -replace_with = "0.1.7" resvg = "0.14" seahash = "4.1" serde = { version = "1.0.125", features = ["derive"] } diff --git a/gpui/src/elements.rs b/gpui/src/elements.rs index 869a26b542..3d03574094 100644 --- a/gpui/src/elements.rs +++ b/gpui/src/elements.rs @@ -34,8 +34,7 @@ use crate::{ }; use core::panic; use json::ToJson; -use replace_with::replace_with_or_abort; -use std::{any::Any, borrow::Cow}; +use std::{any::Any, borrow::Cow, mem}; trait AnyElement { fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F; @@ -115,6 +114,7 @@ pub trait Element { } pub enum Lifecycle { + Empty, Init { element: T, }, @@ -139,8 +139,9 @@ pub struct ElementBox { impl AnyElement for Lifecycle { fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F { - let mut result = None; - replace_with_or_abort(self, |me| match me { + let result; + *self = match mem::take(self) { + Lifecycle::Empty => unreachable!(), Lifecycle::Init { mut element } | Lifecycle::PostLayout { mut element, .. } | Lifecycle::PostPaint { mut element, .. } => { @@ -148,7 +149,7 @@ impl AnyElement for Lifecycle { debug_assert!(size.x().is_finite()); debug_assert!(size.y().is_finite()); - result = Some(size); + result = size; Lifecycle::PostLayout { element, constraint, @@ -156,8 +157,8 @@ impl AnyElement for Lifecycle { layout, } } - }); - result.unwrap() + }; + result } fn after_layout(&mut self, cx: &mut AfterLayoutContext) { @@ -175,27 +176,25 @@ impl AnyElement for Lifecycle { } fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) { - replace_with_or_abort(self, |me| { - if let Lifecycle::PostLayout { - mut element, + *self = if let Lifecycle::PostLayout { + mut element, + constraint, + size, + mut layout, + } = mem::take(self) + { + let bounds = RectF::new(origin, size); + let paint = element.paint(bounds, &mut layout, cx); + Lifecycle::PostPaint { + element, constraint, - size, - mut layout, - } = me - { - let bounds = RectF::new(origin, size); - let paint = element.paint(bounds, &mut layout, cx); - Lifecycle::PostPaint { - element, - constraint, - bounds, - layout, - paint, - } - } else { - panic!("invalid element lifecycle state"); + bounds, + layout, + paint, } - }); + } else { + panic!("invalid element lifecycle state"); + }; } fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool { @@ -215,7 +214,7 @@ impl AnyElement for Lifecycle { fn size(&self) -> Vector2F { match self { - Lifecycle::Init { .. } => panic!("invalid element lifecycle state"), + Lifecycle::Empty | Lifecycle::Init { .. } => panic!("invalid element lifecycle state"), Lifecycle::PostLayout { size, .. } => *size, Lifecycle::PostPaint { bounds, .. } => bounds.size(), } @@ -223,6 +222,7 @@ impl AnyElement for Lifecycle { fn metadata(&self) -> Option<&dyn Any> { match self { + Lifecycle::Empty => unreachable!(), Lifecycle::Init { element } | Lifecycle::PostLayout { element, .. } | Lifecycle::PostPaint { element, .. } => element.metadata(), @@ -257,6 +257,12 @@ impl AnyElement for Lifecycle { } } +impl Default for Lifecycle { + fn default() -> Self { + Self::Empty + } +} + impl ElementBox { pub fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F { self.element.layout(constraint, cx)