Skip to main content

ProtoString

Trait ProtoString 

Source
pub trait ProtoString:
    Clone
    + PartialEq
    + Default
    + Debug
    + Send
    + Sync
    + AsRef<str>
    + From<String>
    + for<'a> From<&'a str> { }
Expand description

The bound generated code places on the Rust type used for a proto string field. You neither implement nor name this trait by hand — a blanket impl covers every conforming type, and a forthcoming string_type knob in buffa_build selects the concrete type at code-generation time.

buffa generates String by default. The knob will be able to substitute a small-string-optimized type — such as smol_str::SmolStr, ecow::EcoString, or compact_str::CompactString (each behind the matching buffa feature) — for read-mostly schemas where String’s growable buffer is unnecessary.

The bounds are exactly what generated code requires of a string field:

  • Clone + PartialEq + Default + Debug — for the #[derive(...)] and the hand-written Debug impl on message structs, and for clear() (which resets the field to Default rather than relying on a String-specific clear, since the small-string types may be immutable).
  • Send + Sync — so a message owning such a field stays Send + Sync; without this bound an exotic string type could silently make every containing message thread-unsafe.
  • AsRef<str> — the encoder borrows the field as &str; encode_string and string_encoded_len take &str.
  • From<String> and From<&str> — used by the decode path (decode_string_to) and (once the knob lands) the view→owned conversion to construct the field from freshly decoded text.

For the default String representation every conversion is the identity, so the generic path costs nothing relative to the specialized one.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T> ProtoString for T
where T: Clone + PartialEq + Default + Debug + Send + Sync + AsRef<str> + From<String> + for<'a> From<&'a str>,