more cleanup
This commit is contained in:
1 parent
9f99f46cfd
commit
5cbdc571e3
10 files changed
+148
-192
No files matched your search
@@ -2,7 +2,7 @@ use crate::innerlude::*;
|
|||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
pub struct ActiveFrame {
|
pub(crate) struct ActiveFrame {
|
||||||
// We use a "generation" for users of contents in the bump frames to ensure their data isn't broken
|
// We use a "generation" for users of contents in the bump frames to ensure their data isn't broken
|
||||||
pub generation: Cell<usize>,
|
pub generation: Cell<usize>,
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ pub struct ActiveFrame {
|
|||||||
pub frames: [BumpFrame; 2],
|
pub frames: [BumpFrame; 2],
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct BumpFrame {
|
pub(crate) struct BumpFrame {
|
||||||
pub bump: Bump,
|
pub bump: Bump,
|
||||||
pub(crate) head_node: VNode<'static>,
|
pub(crate) head_node: VNode<'static>,
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use crate::innerlude::*;
|
|||||||
///
|
///
|
||||||
/// This iterator is useful when it's important to load the next real root onto the top of the stack for operations like
|
/// This iterator is useful when it's important to load the next real root onto the top of the stack for operations like
|
||||||
/// "InsertBefore".
|
/// "InsertBefore".
|
||||||
pub struct RealChildIterator<'a> {
|
pub(crate) struct RealChildIterator<'a> {
|
||||||
scopes: &'a ResourcePool,
|
scopes: &'a ResourcePool,
|
||||||
|
|
||||||
// Heuristcally we should never bleed into 4 completely nested fragments/components
|
// Heuristcally we should never bleed into 4 completely nested fragments/components
|
||||||
@@ -20,19 +20,6 @@ impl<'a> RealChildIterator<'a> {
|
|||||||
stack: smallvec::smallvec![(0, starter)],
|
stack: smallvec::smallvec![(0, starter)],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_from_slice(nodes: &'a [VNode<'a>], scopes: &'a ResourcePool) -> Self {
|
|
||||||
let mut stack = smallvec::smallvec![];
|
|
||||||
for node in nodes {
|
|
||||||
stack.push((0, node));
|
|
||||||
}
|
|
||||||
Self { scopes, stack }
|
|
||||||
}
|
|
||||||
// keep the memory around
|
|
||||||
pub fn reset_with(&mut self, node: &'a VNode<'a>) {
|
|
||||||
self.stack.clear();
|
|
||||||
self.stack.push((0, node));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Iterator for RealChildIterator<'a> {
|
impl<'a> Iterator for RealChildIterator<'a> {
|
||||||
|
|||||||
@@ -112,7 +112,6 @@ impl<'src, P> Context<'src, P> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get's this component's unique identifier.
|
/// Get's this component's unique identifier.
|
||||||
///
|
|
||||||
pub fn get_scope_id(&self) -> ScopeId {
|
pub fn get_scope_id(&self) -> ScopeId {
|
||||||
self.scope.our_arena_idx.clone()
|
self.scope.our_arena_idx.clone()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,15 @@
|
|||||||
use crate::innerlude::{ElementId, ScopeId};
|
use crate::innerlude::{ElementId, ScopeId};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct EventTrigger {
|
pub struct UiEvent {
|
||||||
/// The originator of the event trigger
|
/// The originator of the event trigger
|
||||||
pub scope: ScopeId,
|
pub scope: ScopeId,
|
||||||
|
|
||||||
/// The optional real node associated with the trigger
|
/// The optional real node associated with the trigger
|
||||||
pub mounted_dom_id: Option<ElementId>,
|
pub mounted_dom_id: Option<ElementId>,
|
||||||
|
|
||||||
|
pub listener_type: &'static str,
|
||||||
|
|
||||||
/// The type of event
|
/// The type of event
|
||||||
pub event: SyntheticEvent,
|
pub event: SyntheticEvent,
|
||||||
}
|
}
|
||||||
@@ -26,7 +28,6 @@ pub enum SyntheticEvent {
|
|||||||
FocusEvent(on::FocusEvent),
|
FocusEvent(on::FocusEvent),
|
||||||
FormEvent(on::FormEvent),
|
FormEvent(on::FormEvent),
|
||||||
SelectionEvent(on::SelectionEvent),
|
SelectionEvent(on::SelectionEvent),
|
||||||
UIEvent(on::UIEvent),
|
|
||||||
WheelEvent(on::WheelEvent),
|
WheelEvent(on::WheelEvent),
|
||||||
MediaEvent(on::MediaEvent),
|
MediaEvent(on::MediaEvent),
|
||||||
AnimationEvent(on::AnimationEvent),
|
AnimationEvent(on::AnimationEvent),
|
||||||
@@ -46,7 +47,6 @@ impl std::fmt::Debug for SyntheticEvent {
|
|||||||
SyntheticEvent::FormEvent(_) => "FormEvent",
|
SyntheticEvent::FormEvent(_) => "FormEvent",
|
||||||
SyntheticEvent::SelectionEvent(_) => "SelectionEvent",
|
SyntheticEvent::SelectionEvent(_) => "SelectionEvent",
|
||||||
SyntheticEvent::TouchEvent(_) => "TouchEvent",
|
SyntheticEvent::TouchEvent(_) => "TouchEvent",
|
||||||
SyntheticEvent::UIEvent(_) => "UIEvent",
|
|
||||||
SyntheticEvent::WheelEvent(_) => "WheelEvent",
|
SyntheticEvent::WheelEvent(_) => "WheelEvent",
|
||||||
SyntheticEvent::MediaEvent(_) => "MediaEvent",
|
SyntheticEvent::MediaEvent(_) => "MediaEvent",
|
||||||
SyntheticEvent::AnimationEvent(_) => "AnimationEvent",
|
SyntheticEvent::AnimationEvent(_) => "AnimationEvent",
|
||||||
@@ -284,6 +284,9 @@ pub mod on {
|
|||||||
/// onmouseout
|
/// onmouseout
|
||||||
onmouseout
|
onmouseout
|
||||||
|
|
||||||
|
///
|
||||||
|
onscroll
|
||||||
|
|
||||||
/// onmouseover
|
/// onmouseover
|
||||||
///
|
///
|
||||||
/// Triggered when the users's mouse hovers over an element.
|
/// Triggered when the users's mouse hovers over an element.
|
||||||
@@ -331,14 +334,9 @@ pub mod on {
|
|||||||
ontouchstart
|
ontouchstart
|
||||||
];
|
];
|
||||||
|
|
||||||
UIEventInner(UIEvent): [
|
|
||||||
///
|
|
||||||
scroll
|
|
||||||
];
|
|
||||||
|
|
||||||
WheelEventInner(WheelEvent): [
|
WheelEventInner(WheelEvent): [
|
||||||
///
|
///
|
||||||
wheel
|
onwheel
|
||||||
];
|
];
|
||||||
|
|
||||||
MediaEventInner(MediaEvent): [
|
MediaEventInner(MediaEvent): [
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ use crate::innerlude::*;
|
|||||||
use futures_util::FutureExt;
|
use futures_util::FutureExt;
|
||||||
use std::{
|
use std::{
|
||||||
any::{Any, TypeId},
|
any::{Any, TypeId},
|
||||||
cell::{Cell, RefCell},
|
cell::RefCell,
|
||||||
future::Future,
|
future::Future,
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
};
|
};
|
||||||
|
|||||||
+49
-53
@@ -1,65 +1,16 @@
|
|||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
|
|
||||||
pub use crate::innerlude::{
|
|
||||||
format_args_f, html, rsx, Context, DiffInstruction, DioxusElement, DomEdit, DomTree, ElementId,
|
|
||||||
EventPriority, EventTrigger, LazyNodes, MountType, Mutations, NodeFactory, Properties, ScopeId,
|
|
||||||
SuspendedContext, SyntheticEvent, VNode, VirtualDom, FC,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod prelude {
|
|
||||||
pub use crate::component::{fc_to_builder, Fragment, Properties};
|
|
||||||
pub use crate::context::Context;
|
|
||||||
pub use crate::hooks::*;
|
|
||||||
pub use crate::innerlude::{DioxusElement, DomTree, LazyNodes, Mutations, NodeFactory, FC};
|
|
||||||
pub use crate::nodes::VNode;
|
|
||||||
pub use crate::VirtualDom;
|
|
||||||
pub use dioxus_core_macro::{format_args_f, html, rsx, Props};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) mod innerlude {
|
|
||||||
pub use crate::bumpframe::*;
|
|
||||||
pub use crate::childiter::*;
|
|
||||||
pub use crate::component::*;
|
|
||||||
pub use crate::context::*;
|
|
||||||
pub use crate::diff::*;
|
|
||||||
pub use crate::diff_stack::*;
|
|
||||||
pub use crate::events::*;
|
|
||||||
pub use crate::heuristics::*;
|
|
||||||
pub use crate::hooklist::*;
|
|
||||||
pub use crate::hooks::*;
|
|
||||||
pub use crate::mutations::*;
|
|
||||||
pub use crate::nodes::*;
|
|
||||||
pub use crate::scheduler::*;
|
|
||||||
pub use crate::scope::*;
|
|
||||||
pub use crate::util::*;
|
|
||||||
pub use crate::virtual_dom::*;
|
|
||||||
pub use crate::yield_now::*;
|
|
||||||
|
|
||||||
pub type DomTree<'a> = Option<VNode<'a>>;
|
|
||||||
pub type FC<P> = fn(Context<P>) -> DomTree;
|
|
||||||
|
|
||||||
pub use dioxus_core_macro::{format_args_f, html, rsx};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod exports {
|
|
||||||
//! Important dependencies that are used by the rest of the library
|
|
||||||
// the foundation of this library
|
|
||||||
pub use bumpalo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Navigating this crate:
|
Navigating this crate:
|
||||||
- virtual_dom: the primary entrypoint for the crate
|
- virtual_dom: the primary entrypoint for the crate
|
||||||
- scheduler: the core interior logic called by virtual_dom
|
- scheduler: the core interior logic called by virtual_dom
|
||||||
- nodes: the definition of VNodes, listeners, etc.
|
- nodes: the definition of VNodes, listeners, etc.
|
||||||
-
|
- diff: the stackmachine-based diffing algorithm
|
||||||
|
- hooks: foundational hooks that require crate-private APIs
|
||||||
|
- mutations: DomEdits/NodeRefs and internal API to create them
|
||||||
|
|
||||||
Some utilities
|
Some utilities
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
pub mod bumpframe;
|
pub mod bumpframe;
|
||||||
pub mod childiter;
|
pub mod childiter;
|
||||||
@@ -78,4 +29,49 @@ pub mod scope;
|
|||||||
pub mod signals;
|
pub mod signals;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
pub mod virtual_dom;
|
pub mod virtual_dom;
|
||||||
pub mod yield_now;
|
|
||||||
|
pub(crate) mod innerlude {
|
||||||
|
pub(crate) use crate::bumpframe::*;
|
||||||
|
pub(crate) use crate::childiter::*;
|
||||||
|
pub use crate::component::*;
|
||||||
|
pub use crate::context::*;
|
||||||
|
pub(crate) use crate::diff::*;
|
||||||
|
pub use crate::diff_stack::*;
|
||||||
|
pub use crate::events::*;
|
||||||
|
pub use crate::heuristics::*;
|
||||||
|
pub use crate::hooklist::*;
|
||||||
|
pub use crate::hooks::*;
|
||||||
|
pub use crate::mutations::*;
|
||||||
|
pub use crate::nodes::*;
|
||||||
|
pub use crate::scheduler::*;
|
||||||
|
pub use crate::scope::*;
|
||||||
|
pub use crate::util::*;
|
||||||
|
pub use crate::virtual_dom::*;
|
||||||
|
|
||||||
|
pub type DomTree<'a> = Option<VNode<'a>>;
|
||||||
|
pub type FC<P> = fn(Context<P>) -> DomTree;
|
||||||
|
|
||||||
|
pub use dioxus_core_macro::{format_args_f, html, rsx};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use crate::innerlude::{
|
||||||
|
format_args_f, html, rsx, Context, DiffInstruction, DioxusElement, DomEdit, DomTree, ElementId,
|
||||||
|
EventPriority, LazyNodes, MountType, Mutations, NodeFactory, Properties, ScopeId,
|
||||||
|
SuspendedContext, SyntheticEvent, UiEvent, VNode, VirtualDom, FC,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub mod prelude {
|
||||||
|
pub use crate::component::{fc_to_builder, Fragment, Properties};
|
||||||
|
pub use crate::context::Context;
|
||||||
|
pub use crate::hooks::*;
|
||||||
|
pub use crate::innerlude::{DioxusElement, DomTree, LazyNodes, Mutations, NodeFactory, FC};
|
||||||
|
pub use crate::nodes::VNode;
|
||||||
|
pub use crate::VirtualDom;
|
||||||
|
pub use dioxus_core_macro::{format_args_f, html, rsx, Props};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod exports {
|
||||||
|
//! Important dependencies that are used by the rest of the library
|
||||||
|
// the foundation of this library
|
||||||
|
pub use bumpalo;
|
||||||
|
}
|
||||||
@@ -97,7 +97,7 @@ pub struct EventChannel {
|
|||||||
|
|
||||||
pub enum SchedulerMsg {
|
pub enum SchedulerMsg {
|
||||||
Immediate(ScopeId),
|
Immediate(ScopeId),
|
||||||
UiEvent(EventTrigger),
|
UiEvent(UiEvent),
|
||||||
SubmitTask(FiberTask, u64),
|
SubmitTask(FiberTask, u64),
|
||||||
ToggleTask(u64),
|
ToggleTask(u64),
|
||||||
PauseTask(u64),
|
PauseTask(u64),
|
||||||
@@ -140,11 +140,11 @@ pub(crate) struct Scheduler {
|
|||||||
// scheduler stuff
|
// scheduler stuff
|
||||||
pub current_priority: EventPriority,
|
pub current_priority: EventPriority,
|
||||||
|
|
||||||
pub ui_events: VecDeque<EventTrigger>,
|
pub ui_events: VecDeque<UiEvent>,
|
||||||
|
|
||||||
pub pending_immediates: VecDeque<ScopeId>,
|
pub pending_immediates: VecDeque<ScopeId>,
|
||||||
|
|
||||||
pub pending_tasks: VecDeque<EventTrigger>,
|
pub pending_tasks: VecDeque<UiEvent>,
|
||||||
|
|
||||||
pub garbage_scopes: HashSet<ScopeId>,
|
pub garbage_scopes: HashSet<ScopeId>,
|
||||||
|
|
||||||
@@ -248,39 +248,37 @@ impl Scheduler {
|
|||||||
|
|
||||||
// Converts UI events into dirty scopes with various priorities
|
// Converts UI events into dirty scopes with various priorities
|
||||||
pub fn consume_pending_events(&mut self) {
|
pub fn consume_pending_events(&mut self) {
|
||||||
// while let Some(trigger) = self.ui_events.pop_back() {
|
while let Some(trigger) = self.ui_events.pop_back() {
|
||||||
// match &trigger.event {
|
if let Some(scope) = self.pool.get_scope_mut(trigger.scope) {
|
||||||
// SyntheticEvent::ClipboardEvent(_)
|
if let Some(element) = trigger.mounted_dom_id {
|
||||||
// | SyntheticEvent::CompositionEvent(_)
|
let priority = match &trigger.event {
|
||||||
// | SyntheticEvent::KeyboardEvent(_)
|
SyntheticEvent::ClipboardEvent(_) => {}
|
||||||
// | SyntheticEvent::FocusEvent(_)
|
SyntheticEvent::CompositionEvent(_) => {}
|
||||||
// | SyntheticEvent::FormEvent(_)
|
SyntheticEvent::KeyboardEvent(_) => {}
|
||||||
// | SyntheticEvent::SelectionEvent(_)
|
SyntheticEvent::FocusEvent(_) => {}
|
||||||
// | SyntheticEvent::TouchEvent(_)
|
SyntheticEvent::FormEvent(_) => {}
|
||||||
// | SyntheticEvent::UIEvent(_)
|
SyntheticEvent::SelectionEvent(_) => {}
|
||||||
// | SyntheticEvent::WheelEvent(_)
|
SyntheticEvent::TouchEvent(_) => {}
|
||||||
// | SyntheticEvent::MediaEvent(_)
|
SyntheticEvent::WheelEvent(_) => {}
|
||||||
// | SyntheticEvent::AnimationEvent(_)
|
SyntheticEvent::MediaEvent(_) => {}
|
||||||
// | SyntheticEvent::TransitionEvent(_)
|
SyntheticEvent::AnimationEvent(_) => {}
|
||||||
// | SyntheticEvent::ToggleEvent(_)
|
SyntheticEvent::TransitionEvent(_) => {}
|
||||||
// | SyntheticEvent::MouseEvent(_)
|
SyntheticEvent::ToggleEvent(_) => {}
|
||||||
// | SyntheticEvent::PointerEvent(_) => {
|
SyntheticEvent::MouseEvent(_) => {}
|
||||||
// if let Some(scope) = self.get_scope_mut(trigger.scope) {
|
SyntheticEvent::PointerEvent(_) => {}
|
||||||
// if let Some(element) = trigger.mounted_dom_id {
|
};
|
||||||
// scope.call_listener(trigger.event, element)?;
|
|
||||||
|
|
||||||
// // let receiver = self.immediate_receiver.clone();
|
scope.call_listener(trigger.event, element);
|
||||||
// // let mut receiver = receiver.borrow_mut();
|
// let receiver = self.immediate_receiver.clone();
|
||||||
|
// let mut receiver = receiver.borrow_mut();
|
||||||
|
|
||||||
// // // Drain the immediates into the dirty scopes, setting the appropiate priorities
|
// // Drain the immediates into the dirty scopes, setting the appropiate priorities
|
||||||
// // while let Ok(Some(dirty_scope)) = receiver.try_next() {
|
// while let Ok(Some(dirty_scope)) = receiver.try_next() {
|
||||||
// // self.add_dirty_scope(dirty_scope, trigger.priority)
|
// self.add_dirty_scope(dirty_scope, trigger.priority)
|
||||||
// // }
|
// }
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// nothing to do, no events on channels, no work
|
// nothing to do, no events on channels, no work
|
||||||
@@ -293,26 +291,13 @@ impl Scheduler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_work(&self) -> bool {
|
pub fn has_work(&self) -> bool {
|
||||||
todo!()
|
self.lanes.iter().find(|f| f.has_work()).is_some()
|
||||||
// self.high_priorty.has_work()
|
|
||||||
// || self.medium_priority.has_work()
|
|
||||||
// || self.low_priority.has_work()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_pending_garbage(&self) -> bool {
|
pub fn has_pending_garbage(&self) -> bool {
|
||||||
!self.garbage_scopes.is_empty()
|
!self.garbage_scopes.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_fiber<'a>(&'a mut self) -> &mut DiffMachine<'a> {
|
|
||||||
todo!()
|
|
||||||
// let fib = match self.current_priority {
|
|
||||||
// EventPriority::High => &mut self.high_priorty,
|
|
||||||
// EventPriority::Medium => &mut self.medium_priority,
|
|
||||||
// EventPriority::Low => &mut self.low_priority,
|
|
||||||
// };
|
|
||||||
// unsafe { std::mem::transmute(fib) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn shift_priorities(&mut self) {
|
fn shift_priorities(&mut self) {
|
||||||
self.current_priority = match (
|
self.current_priority = match (
|
||||||
self.lanes[0].has_work(),
|
self.lanes[0].has_work(),
|
||||||
@@ -634,7 +619,7 @@ pub enum EventPriority {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ResourcePool {
|
pub(crate) struct ResourcePool {
|
||||||
/*
|
/*
|
||||||
This *has* to be an UnsafeCell.
|
This *has* to be an UnsafeCell.
|
||||||
|
|
||||||
|
|||||||
@@ -195,11 +195,7 @@ impl Scope {
|
|||||||
// A safe wrapper around calling listeners
|
// A safe wrapper around calling listeners
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
pub(crate) fn call_listener(
|
pub(crate) fn call_listener(&mut self, event: SyntheticEvent, element: ElementId) {
|
||||||
&mut self,
|
|
||||||
event: SyntheticEvent,
|
|
||||||
element: ElementId,
|
|
||||||
) -> Option<()> {
|
|
||||||
let listners = self.listeners.borrow_mut();
|
let listners = self.listeners.borrow_mut();
|
||||||
|
|
||||||
let raw_listener = listners.iter().find(|lis| {
|
let raw_listener = listners.iter().find(|lis| {
|
||||||
@@ -222,8 +218,6 @@ impl Scope {
|
|||||||
} else {
|
} else {
|
||||||
log::warn!("An event was triggered but there was no listener to handle it");
|
log::warn!("An event was triggered but there was no listener to handle it");
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn root(&self) -> &VNode {
|
pub fn root(&self) -> &VNode {
|
||||||
|
|||||||
@@ -11,3 +11,56 @@ pub fn empty_cell() -> Cell<Option<ElementId>> {
|
|||||||
pub fn type_name_of<T>(_: T) -> &'static str {
|
pub fn type_name_of<T>(_: T) -> &'static str {
|
||||||
std::any::type_name::<T>()
|
std::any::type_name::<T>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
// use crate::task::{Context, Poll};
|
||||||
|
|
||||||
|
/// Cooperatively gives up a timeslice to the task scheduler.
|
||||||
|
///
|
||||||
|
/// Calling this function will move the currently executing future to the back
|
||||||
|
/// of the execution queue, making room for other futures to execute. This is
|
||||||
|
/// especially useful after running CPU-intensive operations inside a future.
|
||||||
|
///
|
||||||
|
/// See also [`task::spawn_blocking`].
|
||||||
|
///
|
||||||
|
/// [`task::spawn_blocking`]: fn.spawn_blocking.html
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// Basic usage:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # async_std::task::block_on(async {
|
||||||
|
/// #
|
||||||
|
/// use async_std::task;
|
||||||
|
///
|
||||||
|
/// task::yield_now().await;
|
||||||
|
/// #
|
||||||
|
/// # })
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
pub async fn yield_now() {
|
||||||
|
YieldNow(false).await
|
||||||
|
}
|
||||||
|
|
||||||
|
struct YieldNow(bool);
|
||||||
|
|
||||||
|
impl Future for YieldNow {
|
||||||
|
type Output = ();
|
||||||
|
|
||||||
|
// The futures executor is implemented as a FIFO queue, so all this future
|
||||||
|
// does is re-schedule the future back to the end of the queue, giving room
|
||||||
|
// for other futures to progress.
|
||||||
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
if !self.0 {
|
||||||
|
self.0 = true;
|
||||||
|
cx.waker().wake_by_ref();
|
||||||
|
Poll::Pending
|
||||||
|
} else {
|
||||||
|
Poll::Ready(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
//! Utility Code taken from async_std to immediately yield the current task to the executor.
|
|
||||||
//!
|
|
||||||
//!
|
|
||||||
|
|
||||||
use std::future::Future;
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::task::{Context, Poll};
|
|
||||||
|
|
||||||
// use crate::task::{Context, Poll};
|
|
||||||
|
|
||||||
/// Cooperatively gives up a timeslice to the task scheduler.
|
|
||||||
///
|
|
||||||
/// Calling this function will move the currently executing future to the back
|
|
||||||
/// of the execution queue, making room for other futures to execute. This is
|
|
||||||
/// especially useful after running CPU-intensive operations inside a future.
|
|
||||||
///
|
|
||||||
/// See also [`task::spawn_blocking`].
|
|
||||||
///
|
|
||||||
/// [`task::spawn_blocking`]: fn.spawn_blocking.html
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// Basic usage:
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # async_std::task::block_on(async {
|
|
||||||
/// #
|
|
||||||
/// use async_std::task;
|
|
||||||
///
|
|
||||||
/// task::yield_now().await;
|
|
||||||
/// #
|
|
||||||
/// # })
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
|
||||||
pub async fn yield_now() {
|
|
||||||
YieldNow(false).await
|
|
||||||
}
|
|
||||||
|
|
||||||
struct YieldNow(bool);
|
|
||||||
|
|
||||||
impl Future for YieldNow {
|
|
||||||
type Output = ();
|
|
||||||
|
|
||||||
// The futures executor is implemented as a FIFO queue, so all this future
|
|
||||||
// does is re-schedule the future back to the end of the queue, giving room
|
|
||||||
// for other futures to progress.
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
if !self.0 {
|
|
||||||
self.0 = true;
|
|
||||||
cx.waker().wake_by_ref();
|
|
||||||
Poll::Pending
|
|
||||||
} else {
|
|
||||||
Poll::Ready(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user