Skip to content

Commit

Permalink
Default to std::sync::mpsc, introduce crossbeam feature (#775)
Browse files Browse the repository at this point in the history
* create/update-signal fn use std channel, introduce `crossbeam feature`

* fmt

* apply requested changes

* simplify feature by rename, add changelog

* another rename

---------

Co-authored-by: Karol <[email protected]>
  • Loading branch information
charlescgs and Karol authored Feb 21, 2025
1 parent d065981 commit 7e5e78a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

## Unreleased

- Use by default `std::sync::mpsc::channel` and place crossbeam behind the `crossbeam` feature [#775](https://github.com/lapce/floem/pull/775)

## [0.2.0] - 2024-11-13

Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ tokio = { version = "1", features = ["sync", "rt"], optional = true }
raw-window-handle = { workspace = true }
unicode-segmentation = "1.10.0"
peniko = { workspace = true }
crossbeam-channel = "0.5.6"
im-rc = "15.1.0"
serde = { workspace = true, optional = true }
lapce-xi-rope = { workspace = true, optional = true }
Expand All @@ -81,7 +80,7 @@ image = { workspace = true }
im = { workspace = true }
winit = { workspace = true }
futures = { version = "0.3.30", optional = true }
crossbeam = "0.8"
crossbeam = { version = "0.8", optional = true }

[target.'cfg(any(target_os = "windows", target_os = "macos"))'.dependencies]
muda = { workspace = true }
Expand Down Expand Up @@ -147,3 +146,5 @@ tokio = ["dep:tokio"]
rfd-async-std = ["dep:rfd", "rfd/async-std"]
rfd-tokio = ["dep:rfd", "rfd/tokio"]
futures = ["dep:futures"]

crossbeam = [ "dep:crossbeam", "floem_renderer/crossbeam" ]
5 changes: 4 additions & 1 deletion renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ cosmic-text = { version = "0.12.1", features = ["shape-run-cache"] }

winit = { workspace = true }
wgpu = { workspace = true }
crossbeam = { version = "0.8" }
crossbeam = { version = "0.8", optional = true }
futures = "0.3.26"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = { version = "0.4" }

[features]
crossbeam = [ "dep:crossbeam" ]
8 changes: 6 additions & 2 deletions renderer/src/gpu_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
use std::{future::Future, sync::Arc};

use crossbeam::channel::{self, Receiver};
#[cfg(feature = "crossbeam")]
use crossbeam::channel::{bounded as sync_channel, Receiver};
#[cfg(not(feature = "crossbeam"))]
use std::sync::mpsc::{sync_channel, Receiver};
use wgpu::Backends;

use winit::window::{Window, WindowId};
Expand Down Expand Up @@ -53,7 +56,8 @@ impl GpuResources {
});
// Channel passing to do async out-of-band within the winit event_loop since wasm can't
// execute futures with a return value
let (tx, rx) = channel::bounded(1);
let (tx, rx) = sync_channel(1);

spawn({
async move {
let surface = match instance.create_surface(Arc::clone(&window)) {
Expand Down
9 changes: 7 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{cell::RefCell, rc::Rc};

use crossbeam_channel::{Receiver, Sender};
#[cfg(feature = "crossbeam")]
use crossbeam::channel::{unbounded as channel, Receiver, Sender};
#[cfg(not(feature = "crossbeam"))]
use std::sync::mpsc::{channel, Receiver, Sender};

use floem_reactive::WriteSignal;
use parking_lot::Mutex;
use raw_window_handle::HasDisplayHandle;
Expand Down Expand Up @@ -160,7 +164,8 @@ impl Application {
crate::app_delegate::set_app_delegate();

let event_loop_proxy = event_loop.create_proxy();
let (sender, receiver) = crossbeam_channel::unbounded();
let (sender, receiver) = channel();

*EVENT_LOOP_PROXY.lock() = Some((event_loop_proxy.clone(), sender));
unsafe {
Clipboard::init(event_loop.display_handle().unwrap().as_raw());
Expand Down
9 changes: 7 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ use std::time::{Duration, Instant};
#[cfg(target_arch = "wasm32")]
use web_time::{Duration, Instant};

#[cfg(feature = "crossbeam")]
use crossbeam::channel::Receiver;
#[cfg(not(feature = "crossbeam"))]
use std::sync::mpsc::Receiver;

use taffy::prelude::NodeId;

use crate::animate::{AnimStateKind, RepeatMode};
Expand Down Expand Up @@ -1210,7 +1215,7 @@ pub enum PaintState {
/// The renderer is not yet initialized. This state is used to wait for the GPU resources to be acquired.
PendingGpuResources {
window: Arc<dyn Window>,
rx: crossbeam::channel::Receiver<Result<GpuResources, GpuResourceError>>,
rx: Receiver<Result<GpuResources, GpuResourceError>>,
font_embolden: f32,
/// This field holds an instance of `Renderer::Uninitialized` until the GPU resources are acquired,
/// which will be returned in `PaintState::renderer` and `PaintState::renderer_mut`.
Expand All @@ -1227,7 +1232,7 @@ pub enum PaintState {
impl PaintState {
pub fn new(
window: Arc<dyn Window>,
rx: crossbeam::channel::Receiver<Result<GpuResources, GpuResourceError>>,
rx: Receiver<Result<GpuResources, GpuResourceError>>,
scale: f64,
size: Size,
font_embolden: f32,
Expand Down
13 changes: 8 additions & 5 deletions src/ext_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ use crate::{
Application,
};

#[derive(Debug)]
#[cfg(feature = "crossbeam")]
use crossbeam::channel::Receiver;
#[cfg(not(feature = "crossbeam"))]
use std::sync::mpsc::Receiver;

/// # SAFETY
///
/// **DO NOT USE THIS** trigger except for when using with `create_ext_action` or when you guarantee that
/// the signal is never used from a different thread than it was created on.
#[derive(Debug)]
pub struct ExtSendTrigger {
signal: RwSignal<()>,
}
Expand Down Expand Up @@ -123,7 +128,7 @@ pub fn create_ext_action<T: Send + 'static>(

pub fn update_signal_from_channel<T: Send + 'static>(
writer: WriteSignal<Option<T>>,
rx: crossbeam_channel::Receiver<T>,
rx: Receiver<T>,
) {
let cx = Scope::new();
let trigger = with_scope(cx, ExtSendTrigger::new);
Expand Down Expand Up @@ -158,9 +163,7 @@ pub fn update_signal_from_channel<T: Send + 'static>(
});
}

pub fn create_signal_from_channel<T: Send + 'static>(
rx: crossbeam_channel::Receiver<T>,
) -> ReadSignal<Option<T>> {
pub fn create_signal_from_channel<T: Send + 'static>(rx: Receiver<T>) -> ReadSignal<Option<T>> {
let cx = Scope::new();
let trigger = with_scope(cx, ExtSendTrigger::new);

Expand Down

0 comments on commit 7e5e78a

Please sign in to comment.