Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions fiber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ description = "A library for implementing distributed systems with commmunicatin
corosensei = "0.1"
fibril_core = { path = "../core", version = "0" }

[dependencies.tracing]
optional = true
version = "0.1"

[features]
serde = ["fibril_core/serde"]
45 changes: 36 additions & 9 deletions fiber/src/fiber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,49 @@ use {
fibril_core::{Command, Event, Step},
};

#[cfg(feature = "tracing")]
use tracing::{event, span, Level};

impl<'a, M> Fiber<'a, M> {
pub fn new(behavior: impl FnOnce(Sdk<'_, M>) + 'a) -> Self {
Self(ScopedCoroutine::new(move |yielder, spawn_ok| {
let id = match spawn_ok {
Event::SpawnOk(id) => id,
_ => unreachable!(),
};
behavior(Sdk(yielder, id))
}))
#[cfg(feature = "tracing")]
let span = span!(Level::TRACE, "fiber");

Self(
ScopedCoroutine::new(move |yielder, spawn_ok| {
let id = match spawn_ok {
Event::SpawnOk(id) => id,
_ => unreachable!(),
};

#[cfg(feature = "tracing")]
event!(Level::TRACE, "fiber id: {}", id);

behavior(Sdk(yielder, id))
}),
#[cfg(feature = "tracing")]
span,
)
}
}
impl<M> Step<M> for Fiber<'_, M> {
fn step(&mut self, event: Event<M>) -> Command<M> {
#[cfg(feature = "tracing")]
let span = span!(Level::TRACE, "step");
#[cfg(feature = "tracing")]
let _enter = span.enter();

match self.0.resume(event) {
CoroutineResult::Yield(command) => command,
CoroutineResult::Return(()) => Command::Exit,
CoroutineResult::Yield(command) => {
#[cfg(feature = "tracing")]
event!(Level::TRACE, "yield");
command
}
CoroutineResult::Return(()) => {
#[cfg(feature = "tracing")]
event!(Level::TRACE, "return");
Command::Exit
}
}
}
}
6 changes: 6 additions & 0 deletions fiber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ use {
mod fiber;
mod sdk;

#[cfg(not(feature = "tracing"))]
pub struct Fiber<'a, M>(ScopedCoroutine<'a, Event<M>, Command<M>, (), DefaultStack>);
#[cfg(feature = "tracing")]
pub struct Fiber<'a, M>(
ScopedCoroutine<'a, Event<M>, Command<M>, (), DefaultStack>,
tracing::Span,
);

pub use fibril_core::Id;

Expand Down