Skip to main content

serialize_option_via_str

Function serialize_option_via_str 

Source
pub fn serialize_option_via_str<S, T, R>(
    v: &Option<T>,
    s: S,
    render: R,
) -> Result<S::Ok, S::Error>
where S: Serializer, T: Copy, R: FnOnce(T) -> String,
Expand description

Serialize Option<T> as an optional canonical string via render — the substrate-side single-owner primitive for the forward arm of the typed-magnitude codec family (:limits :memory u64 / :limits :wall-clock std::time::Duration / :limits :cpu u32 in crate::limits, :supervisor :restart-window / :politicas :timeout / :politicas :circuit-breaker :window std::time::Duration in crate::supervisor::duration_codec, :politicas :rate-limit crate::aplicacao::RateLimit in crate::aplicacao’s rate_limit_codec).

Every typed-magnitude codec’s serialize_with entry-point used to re-inline the identical block

match v {
    Some(x) => s.serialize_str(&render(*x)),
    None => s.serialize_none(),
}

around a one-token render dispatch — five verbatim copies across limits::ser_byte_size / limits::ser_duration / limits::ser_millicores / supervisor::duration_codec::serialize / aplicacao::rate_limit_codec::serialize. Folding onto this helper closes the forward-arm shape at one substrate-side dispatch: a future change (typed-error wrapping, sentinel-string for the None arm, tagged-serialization for a versioned typed-magnitude shape, alternate string-representation for a versioned YAML flavour) lands at one place rather than through a coordinated per-site rewrite.

Peer of deserialize_option_via_str on the reverse-arm codec axis — the two helpers together partition the substrate’s Option<T> <-> canonical-string codec surface at the primitive layer. Structural peer of find_ascii_whitespace_byte / find_non_ascii_whitespace_char on the codec’s parser-side canonical-form-drift-rejection axis.

The T: Copy bound matches every routed typed-magnitude shape (u32, u64, std::time::Duration, crate::aplicacao::RateLimit); dropping the bound would force every call site to spell an adapter closure (|n| render_byte_size(*n)) around a bare function reference (render_byte_size), re-inserting the syntactic overhead this lift removes.