1//! One job: open, active, closed.2//!3//! Protocol.md section 7 as a state machine, plus the credit of section 8 and4//! the cancel flag of section 9. There is no I/O here, so every rule is a test.5//!6//! A job is open when the caller may still be refused for free. It is active7//! when the server has started to spend. That boundary is the only place where8//! the two terminal frames differ, and it is why `reject` after `open` is a9//! protocol error and not a slower `done`.1011use std::fmt;1213usecrate::credit::{Credit, CreditError};1415/// How far one job has got.16 #[derive(Clone, Copy, PartialEq, Eq, Debug)]17pubenumJobState {18Open,19Active,20Closed,21 }2223/// The answer to one attempt to send an event.24 #[derive(Clone, Copy, PartialEq, Eq, Debug)]25pubenumEmit {26/// The bytes are on the wire. The credit was counted.27Sent,28/// The peer has not granted that many bytes. Wait for a `window`.29Blocked,30/// The caller cancelled. Send the terminal frame and stop.31Cancelled,32 }3334/// What a cancel obliges the server to send.35 #[derive(Clone, Copy, PartialEq, Eq, Debug)]36pubenumAnswer {37/// The job was open: nothing was spent, so refuse.38Reject,39/// The job was active: work was spent, so end it.40Done,41/// The job was already closed: send nothing.42Nothing,43 }4445/// A state change that the job does not allow.46 #[derive(Clone, Copy, PartialEq, Eq, Debug)]47pubenumJobError {48AcceptWhile(JobState),49DoneWhile(JobState),50Credit(CreditError),51 }5253impl fmt::DisplayforJobError {54fnfmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {55matchself {56JobError::AcceptWhile(s) => write!(f, "cannot accept while {s:?}"),57JobError::DoneWhile(s) => write!(f, "cannot finish while {s:?}"),58JobError::Credit(e) => write!(f, "{e}"),59 }60 }61 }6263implFrom<CreditError> forJobError {64fnfrom(e: CreditError) -> JobError {65JobError::Credit(e)66 }67 }6869/// One stream of work.70 #[derive(Clone, Copy, Debug)]71pubstructJob {72 state: JobState,73 credit: Credit,74 cancelled: bool,75 }7677implJob {78/// A job that the caller opened with `credit` bytes of initial window.79pubfnnew(credit: u64) -> Job {80Job {81 state: JobState::Open,82 credit: Credit::new(credit),83 cancelled: false,84 }85 }8687pubfnstate(self) -> JobState {88self.state89 }9091pubfncredit(self) -> Credit {92self.credit93 }9495/// True once the caller has cancelled. The worker checks this at the top of96/// its own loop, not only inside `emit`.97pubfnis_cancelled(self) -> bool {98self.cancelled99 }100101/// The server has started to spend. Only `done` can follow.102pubfnaccept(&mutself) -> Result<(), JobError> {103matchself.state {104JobState::Open => {105self.state = JobState::Active;106Ok(())107 }108 other => Err(JobError::AcceptWhile(other)),109 }110 }111112/// A terminal frame has been sent. Both terminal frames land here.113///114/// The outcome travels in the body, not in the state. A closed job has no115/// further transitions, which is the fact this machine needs.116pubfnclose(&mutself) {117self.state = JobState::Closed;118 }119120/// The caller sent `cancel`.121///122/// A cancel is not credit and it is not monotonic. It is recorded once and123/// the first answer stands, because a second `cancel` on a closed job must124/// not produce a second terminal frame.125pubfncancel(&mutself) -> Answer {126matchself.state {127JobState::Open => {128self.state = JobState::Closed;129Answer::Reject130 }131JobState::Active => {132self.cancelled = true;133Answer::Done134 }135JobState::Closed => Answer::Nothing,136 }137 }138139/// Applies a `window`. A watermark below what was sent is an error.140pubfnwindow(&mutself, watermark: u64) -> Result<(), JobError> {141self.credit.grant(watermark)?;142Ok(())143 }144145/// Offers one event body of `len` bytes to the wire.146///147/// `Emit::Blocked` is not an error and not a wait: the SDK waits, then148/// calls again. That is the one place where a worker blocks.149pubfnemit(&mutself, len: u64) -> Result<Emit, JobError> {150ifself.cancelled || self.state == JobState::Closed {151returnOk(Emit::Cancelled);152 }153ifself.credit.may_send() < len {154returnOk(Emit::Blocked);155 }156self.credit.take(len)?;157Ok(Emit::Sent)158 }159 }