#[derive(Protocol)]
{
// Attributes available to this derive:
#[async_proto]
}
Expand description
Implements the Protocol
trait for this type.
By default, the network representation is very simple:
- Attempting to read an
enum
with no variants errors immediately, without waiting for data to appear on the stream. - For non-empty
enum
s, the representation starts with the discriminant (a number representing the variant), starting with0
for the first variant declared and so on. - Then follow the
Protocol
representations of any fields of thestruct
or variant, in the order declared.
This representation can waste bandwidth for some types, e.g. struct
s with multiple bool
fields. For those, you may want to implement Protocol
manually.
§Attributes
This macro’s behavior can be modified using attributes. Multiple attributes can be specified as #[async_proto(attr1, attr2, ...)]
or #[async_proto(attr1)] #[async_proto(attr2)] ...
. The following attributes are available:
#[async_proto(as_string)]
: ImplementsProtocol
for this type by converting from and to a string using theFromStr
andToString
traits. TheFromStr
error type must implementInto<ReadErrorKind>
.#[async_proto(map_err = ...)]
: Removes the requirement for theFromStr
error type to implementInto<ReadErrorKind>
and instead uses the given expression (which should be anFnOnce(<T as FromStr>::Err) -> ReadErrorKind
) to convert the error.
#[async_proto(attr(...))]
: Adds the given attribute(s) to theProtocol
implementation. For example, the implementation can be documented using#[async_proto(attr(doc = "..."))]
. May be specified multiple times.#[async_proto(via = Proxy)]
: ImplementsProtocol
for this type (let’s call itT
) in terms of another type (Proxy
in this case) instead of using the variant- and field-based representation described above.&'a T
must implementTryInto<Proxy>
for all'a
, with anError
type that implementsInto<WriteErrorKind>
, andProxy
must implementProtocol
andTryInto<T>
, with anError
type that implementsInto<ReadErrorKind>
.#[async_proto(clone)]
: Replaces the requirement for&'a T
to implementTryInto<Proxy>
with requirements forT
to implementClone
andTryInto<Proxy>
.#[async_proto(map_err = ...)]
: Removes the requirement for<Proxy as TryInto<T>>::Error
to implementInto<ReadErrorKind>
and instead uses the given expression (which should be anFnOnce(<Proxy as TryInto<T>>::Error) -> ReadErrorKind
) to convert the error.
#[async_proto(where(...))]
: Overrides the bounds for the generatedProtocol
implementation. The default is to requireProtocol + Send + Sync + 'static
for each type parameter of this type.
§Field attributes
Additionally, the following attributes can be set on struct or enum fields, rather than the entire type for which Protocol
is being derived:
#[async_proto(max_len = ...)]
: Can be used on a field implementing theLengthPrefixed
trait to limit the allowable length. Note that this alters the network representation of the length prefix (with amax_len
of up to 255, the length is represented as au8
; with amax_len
of 256 to 65535, as au16
; and so on), so adding/removing/changing this attribute may break protocol compatibility.
§Compile errors
- This macro can’t be used with
union
s.