Skip to main content

Task

Struct Task 

Source
pub struct Task { /* private fields */ }
Expand description

A v1 task envelope: the flat map of string fields a producer adds to a task stream.

Every task carries task_id, model_type, model_id, task_name, model_name, source and v, and reply_to when the producer names its reply stream. Every other field, the payload and the optional settings alike, is the caller’s: this crate keeps them in order and never reads them. A task parsed from a stream and written back gives the same fields in the same order.

Debug shows the identity fields and the names of the others, never their values: a payload can be large, or a credential such as a presigned URL.

use legit_streams::Task;

let task = Task::builder("app", "post", "42")
    .task_id("app_01J0000000000000000000000A")
    .field("text_data", "Hello")
    .build()?;

assert_eq!(task.get("text_data"), Some("Hello"));
assert_eq!(task.task_name(), "default");
assert_eq!(Task::from_fields(task.fields().to_vec())?, task);

Implementations§

Source§

impl Task

Source

pub fn builder( source: impl Into<String>, model_type: impl Into<String>, model_id: impl Into<String>, ) -> TaskBuilder

Start a task for the application source, about the caller’s record model_id of type model_type. The worker echoes both back untouched.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").build()?;
assert!(task.task_id().starts_with("app_"));
Source

pub fn from_entry(entry: &Entry) -> Result<Self>

Read a task from a stream entry, within the default Limits.

§Errors

The errors of Task::from_raw.

use legit_streams::{Entry, EntryId, Task};

let entry = Entry::new(EntryId::ZERO, vec![(b"v".to_vec(), b"2".to_vec())]);
assert!(Task::from_entry(&entry).is_err());
Source

pub fn from_raw(raw: &[(Vec<u8>, Vec<u8>)], limits: &Limits) -> Result<Self>

Read a task from raw stream fields.

§Errors

Error::TooLarge for an entry over limits, Error::InvalidUtf8 for a name or value that isn’t UTF-8, Error::DuplicateField for a name given twice, Error::UnsupportedVersion when v is present and isn’t 1 (a task without v, or with an empty one, is version 1), Error::MissingField when another of the envelope’s own fields is absent, Error::EmptyField when one of them but model_name is empty, and Error::InvalidValue when task_id fails is_valid_task_id. Any non-empty source is taken: only a producer keeps to is_valid_app, and only a fast reply key’s {app} is checked against it. A model_type, model_id or task_name longer than IDENTITY_MAX_LEN is taken too; a worker’s result carries UNKNOWN in its place. Fields the envelope doesn’t name, reserved names among them, are kept as they are, and so is a missing or empty v: a Producer or FastCaller that sends the task on writes v = 1.

use legit_streams::{Error, Limits, Task};

let raw = vec![(b"task_id".to_vec(), vec![0xff])];
assert!(matches!(Task::from_raw(&raw, &Limits::default()), Err(Error::InvalidUtf8 { .. })));
Source

pub fn from_fields(fields: Vec<(String, String)>) -> Result<Self>

Read a task from decoded fields, within the default Limits.

§Errors

The errors of Task::from_raw that apply to strings.

use legit_streams::{Error, Task};

let fields = vec![("v".to_owned(), "1".to_owned())];
assert_eq!(Task::from_fields(fields), Err(Error::MissingField("task_id")));
Source

pub fn task_id(&self) -> &str

The task id: {app}_{ULID} when this crate’s builder generated it.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").task_id("app_1").build()?;
assert_eq!(task.task_id(), "app_1");
Source

pub fn model_type(&self) -> &str

The type of the caller’s record, echoed in the result.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.model_type(), "post");
Source

pub fn model_id(&self) -> &str

The id of the caller’s record, echoed in the result.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.model_id(), "42");
Source

pub fn task_name(&self) -> &str

A free-form label for logs; default unless the producer set one.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.task_name(), "default");
Source

pub fn model_name(&self) -> &str

Which model the task asks for; empty means the worker’s default.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.model_name(), "");
Source

pub fn source(&self) -> &str

The producing application.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.source(), "app");
Source

pub fn reply_to(&self) -> Option<&str>

Where the producer wants the result, when it named a stream. An empty reply_to is an absent one.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").reply_to("results:app:hash").build()?;
assert_eq!(task.reply_to(), Some("results:app:hash"));

let mut fields = Task::builder("app", "post", "42").build()?.into_fields();
fields.push(("reply_to".to_owned(), String::new()));
assert_eq!(Task::from_fields(fields)?.reply_to(), None);
Source

pub fn get(&self, name: &str) -> Option<&str>

The value of a field, the envelope’s own or the caller’s.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").field("prefix", "").build()?;
assert_eq!(task.get("prefix"), Some(""));
assert_eq!(task.get("embed_role"), None);
assert_eq!(task.get("v"), Some("1"));
Source

pub fn fields(&self) -> &[(String, String)]

Every field, in order.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.fields()[6], ("v".to_owned(), "1".to_owned()));
Source

pub fn extra(&self) -> impl Iterator<Item = (&str, &str)>

The fields that aren’t the envelope’s own: the payload and the optional settings, in order.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").reply_to("results:app:hash").field("text_data", "Hi").build()?;
let extra: Vec<_> = task.extra().collect();
assert_eq!(extra, [("text_data", "Hi")]);
Source

pub fn into_fields(self) -> Vec<(String, String)>

The fields, in order, consuming the task.

use legit_streams::Task;

let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.into_fields().len(), 7);

Trait Implementations§

Source§

impl Clone for Task

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Task

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Task

Source§

impl PartialEq for Task

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Task

Auto Trait Implementations§

§

impl Forget for Task

§

impl Freeze for Task

§

impl Move for Task

§

impl RefUnwindSafe for Task

§

impl Send for Task
where RawVecInner: Send,

§

impl Sync for Task
where RawVecInner: Sync,

§

impl Unpin for Task
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for Task

§

impl UnwindSafe for Task
where RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more