Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Reduce amount of wasm cfgs in gpui (#50278)
Lukas Wirth7f09bff76a51
diff --git a/Cargo.lock b/Cargo.lockindex 85cf10d661d..2d8b9858deb 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -14242,13 +14242,13 @@ dependencies = ["bytes 1.11.1","futures 0.3.31","gpui",+ "gpui_util","http_client","http_client_tls","log","regex","serde","tokio",- "util","zed-reqwest",]diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.tomlindex fbc8a10571d..4bd9510eac1 100644--- a/crates/gpui/Cargo.toml+++ b/crates/gpui/Cargo.toml@@ -55,6 +55,7 @@ etagere = "0.2"futures.workspace = truefutures-concurrency.workspace = truegpui_macros.workspace = true+http_client.workspace = trueimage.workspace = trueinventory.workspace = trueitertools.workspace = true@@ -103,8 +104,6 @@ web-time.workspace = truegetrandom = { version = "0.3.4", features = ["wasm_js"] }uuid = { workspace = true, features = ["js"] }-[target.'cfg(not(target_family = "wasm"))'.dependencies]-http_client.workspace = true[target.'cfg(target_os = "macos")'.dependencies]block = "0.1"diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rsindex dd81d9166d4..1bd5cd6b3c6 100644--- a/crates/gpui/src/app.rs+++ b/crates/gpui/src/app.rs@@ -27,7 +27,6 @@ use collections::{FxHashMap, FxHashSet, HashMap, VecDeque};pub use context::*;pub use entity_map::*;use gpui_util::{ResultExt, debug_panic};-#[cfg(not(target_family = "wasm"))]use http_client::{HttpClient, Url};use smallvec::SmallVec;#[cfg(any(test, feature = "test-support"))]@@ -139,7 +138,6 @@ impl Application {Self(App::new_app(platform,Arc::new(()),- #[cfg(not(target_family = "wasm"))]Arc::new(NullHttpClient),))}@@ -155,7 +153,6 @@ impl Application {}/// Sets the HTTP client for the application.- #[cfg(not(target_family = "wasm"))]pub fn with_http_client(self, http_client: Arc<dyn HttpClient>) -> Self {let mut context_lock = self.0.borrow_mut();context_lock.http_client = http_client;@@ -585,7 +582,6 @@ pub struct App {pub(crate) loading_assets: FxHashMap<(TypeId, u64), Box<dyn Any>>,asset_source: Arc<dyn AssetSource>,pub(crate) svg_renderer: SvgRenderer,- #[cfg(not(target_family = "wasm"))]http_client: Arc<dyn HttpClient>,pub(crate) globals_by_type: FxHashMap<TypeId, Box<dyn Any>>,pub(crate) entities: EntityMap,@@ -642,7 +638,7 @@ impl App {pub(crate) fn new_app(platform: Rc<dyn Platform>,asset_source: Arc<dyn AssetSource>,- #[cfg(not(target_family = "wasm"))] http_client: Arc<dyn HttpClient>,+ http_client: Arc<dyn HttpClient>,) -> Rc<AppCell> {let background_executor = platform.background_executor();let foreground_executor = platform.foreground_executor();@@ -672,7 +668,6 @@ impl App {svg_renderer: SvgRenderer::new(asset_source.clone()),loading_assets: Default::default(),asset_source,- #[cfg(not(target_family = "wasm"))]http_client,globals_by_type: FxHashMap::default(),entities,@@ -1281,13 +1276,11 @@ impl App {}/// Returns the HTTP client for the application.- #[cfg(not(target_family = "wasm"))]pub fn http_client(&self) -> Arc<dyn HttpClient> {self.http_client.clone()}/// Sets the HTTP client for the application.- #[cfg(not(target_family = "wasm"))]pub fn set_http_client(&mut self, new_client: Arc<dyn HttpClient>) {self.http_client = new_client;}@@ -2512,10 +2505,8 @@ pub struct KeystrokeEvent {pub context_stack: Vec<KeyContext>,}-#[cfg(not(target_family = "wasm"))]struct NullHttpClient;-#[cfg(not(target_family = "wasm"))]impl HttpClient for NullHttpClient {fn send(&self,diff --git a/crates/gpui/src/app/test_context.rs b/crates/gpui/src/app/test_context.rsindex 7ce7f22e3c3..dd4f37ed2a5 100644--- a/crates/gpui/src/app/test_context.rs+++ b/crates/gpui/src/app/test_context.rs@@ -120,16 +120,10 @@ impl TestAppContext {let foreground_executor = ForegroundExecutor::new(arc_dispatcher);let platform = TestPlatform::new(background_executor.clone(), foreground_executor.clone());let asset_source = Arc::new(());- #[cfg(not(target_family = "wasm"))]let http_client = http_client::FakeHttpClient::with_404_response();let text_system = Arc::new(TextSystem::new(platform.text_system()));- let app = App::new_app(- platform.clone(),- asset_source,- #[cfg(not(target_family = "wasm"))]- http_client,- );+ let app = App::new_app(platform.clone(), asset_source, http_client);app.borrow_mut().mode = GpuiMode::test();Self {diff --git a/crates/gpui/src/elements/img.rs b/crates/gpui/src/elements/img.rsindex 59dd9de5fdf..875f9e6dc1c 100644--- a/crates/gpui/src/elements/img.rs+++ b/crates/gpui/src/elements/img.rs@@ -594,7 +594,6 @@ impl Asset for ImageAssetLoader {source: Self::Source,cx: &mut App,) -> impl Future<Output = Self::Output> + Send + 'static {- #[cfg(not(target_family = "wasm"))]let client = cx.http_client();// TODO: Can we make SVGs always rescale?// let scale_factor = cx.scale_factor();@@ -603,7 +602,6 @@ impl Asset for ImageAssetLoader {async move {let bytes = match source.clone() {Resource::Path(uri) => fs::read(uri.as_ref())?,- #[cfg(not(target_family = "wasm"))]Resource::Uri(uri) => {use anyhow::Context as _;use futures::AsyncReadExt as _;@@ -626,12 +624,6 @@ impl Asset for ImageAssetLoader {}body}- #[cfg(target_family = "wasm")]- Resource::Uri(_) => {- return Err(ImageCacheError::Other(Arc::new(anyhow::anyhow!(- "Uri resources are not supported on wasm"- ))));- }Resource::Embedded(path) => {let data = asset_source.load(&path).ok().flatten();if let Some(data) = data {@@ -722,7 +714,6 @@ pub enum ImageCacheError {#[error("IO error: {0}")]Io(Arc<std::io::Error>),/// An error that occurred while processing an image.- #[cfg(not(target_family = "wasm"))]#[error("unexpected http status for {uri}: {status}, body: {body}")]BadStatus {/// The URI of the image.diff --git a/crates/gpui/src/executor.rs b/crates/gpui/src/executor.rsindex d48be9dc308..31c1ed80b92 100644--- a/crates/gpui/src/executor.rs+++ b/crates/gpui/src/executor.rs@@ -564,15 +564,9 @@ mod test {let platform = TestPlatform::new(background_executor.clone(), foreground_executor);let asset_source = Arc::new(());- #[cfg(not(target_family = "wasm"))]let http_client = http_client::FakeHttpClient::with_404_response();- let app = App::new_app(- platform,- asset_source,- #[cfg(not(target_family = "wasm"))]- http_client,- );+ let app = App::new_app(platform, asset_source, http_client);(dispatcher, background_executor, app)}diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rsindex 6e592655162..af3fb51ce51 100644--- a/crates/gpui/src/gpui.rs+++ b/crates/gpui/src/gpui.rs@@ -88,7 +88,6 @@ pub use geometry::*;pub use global::*;pub use gpui_macros::{AppContext, IntoElement, Render, VisualContext, register_action, test};pub use gpui_util::arc_cow::ArcCow;-#[cfg(not(target_family = "wasm"))]pub use http_client;pub use input::*;pub use inspector::*;diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rsindex 885e031cba3..a6714ff250f 100644--- a/crates/gpui/src/platform.rs+++ b/crates/gpui/src/platform.rs@@ -229,7 +229,7 @@ pub trait Platform: 'static {}/// A handle to a platform's display, e.g. a monitor or laptop screen.-pub trait PlatformDisplay: Send + Sync + Debug {+pub trait PlatformDisplay: Debug {/// Get the ID for this displayfn id(&self) -> DisplayId;diff --git a/crates/http_client/Cargo.toml b/crates/http_client/Cargo.tomlindex 177f8639ca1..6273d773d8c 100644--- a/crates/http_client/Cargo.toml+++ b/crates/http_client/Cargo.toml@@ -19,8 +19,6 @@ doctest = true[dependencies]anyhow.workspace = trueasync-compression.workspace = true-async-fs.workspace = true-async-tar.workspace = truebytes.workspace = truederive_more.workspace = truefutures.workspace = true@@ -31,7 +29,11 @@ parking_lot.workspace = trueserde.workspace = trueserde_json.workspace = trueserde_urlencoded.workspace = true-sha2.workspace = true-tempfile.workspace = trueurl.workspace = true++[target.'cfg(not(target_family = "wasm"))'.dependencies]util.workspace = true+async-fs.workspace = true+async-tar.workspace = true+sha2.workspace = true+tempfile.workspace = truediff --git a/crates/http_client/src/http_client.rs b/crates/http_client/src/http_client.rsindex 1182ef74ca3..5cf25a82778 100644--- a/crates/http_client/src/http_client.rs+++ b/crates/http_client/src/http_client.rs@@ -1,5 +1,7 @@mod async_body;+#[cfg(not(target_family = "wasm"))]pub mod github;+#[cfg(not(target_family = "wasm"))]pub mod github_download;pub use anyhow::{Result, anyhow};diff --git a/crates/reqwest_client/Cargo.toml b/crates/reqwest_client/Cargo.tomlindex 2f23ed3072f..41fcd1f5d2f 100644--- a/crates/reqwest_client/Cargo.toml+++ b/crates/reqwest_client/Cargo.toml@@ -20,13 +20,15 @@ anyhow.workspace = truebytes.workspace = truefutures.workspace = truehttp_client.workspace = true-http_client_tls.workspace = trueserde.workspace = truelog.workspace = truetokio = { workspace = true, features = ["rt", "rt-multi-thread"] }regex.workspace = truereqwest.workspace = true-util.workspace = true+gpui_util.workspace = true++[target.'cfg(not(target_family = "wasm"))'.dependencies]+http_client_tls.workspace = true[dev-dependencies]gpui.workspace = truediff --git a/crates/reqwest_client/src/reqwest_client.rs b/crates/reqwest_client/src/reqwest_client.rsindex 7c8ab84bd40..3239a48904a 100644--- a/crates/reqwest_client/src/reqwest_client.rs+++ b/crates/reqwest_client/src/reqwest_client.rs@@ -2,7 +2,7 @@ use std::error::Error;use std::sync::{LazyLock, OnceLock};use std::{borrow::Cow, mem, pin::Pin, task::Poll, time::Duration};-use util::defer;+use gpui_util::defer;use anyhow::anyhow;use bytes::{BufMut, Bytes, BytesMut};