Struct tonic::metadata::MetadataValue[][src]

#[repr(transparent)]
pub struct MetadataValue<VE: ValueEncoding> { /* fields omitted */ }
Expand description

Represents a custom metadata field value.

MetadataValue is used as the MetadataMap value.

Implementations

Convert a static string to a MetadataValue.

This function will not perform any copying, however the string is checked to ensure that no invalid characters are present.

For Ascii values, only visible ASCII characters (32-127) are permitted. For Binary values, the string must be valid base64.

Panics

This function panics if the argument contains invalid metadata value characters.

Examples
let val = AsciiMetadataValue::from_static("hello");
assert_eq!(val, "hello");
let val = BinaryMetadataValue::from_static("SGVsbG8hIQ==");
assert_eq!(val, "Hello!!");

Attempt to convert a byte slice to a MetadataValue.

For Ascii metadata values, If the argument contains invalid metadata value bytes, an error is returned. Only byte values between 32 and 255 (inclusive) are permitted, excluding byte 127 (DEL).

For Binary metadata values this method cannot fail. See also the Binary only version of this method from_bytes.

This function is intended to be replaced in the future by a TryFrom implementation once the trait is stabilized in std.

Examples
let val = AsciiMetadataValue::try_from_bytes(b"hello\xfa").unwrap();
assert_eq!(val, &b"hello\xfa"[..]);

An invalid value

let val = AsciiMetadataValue::try_from_bytes(b"\n");
assert!(val.is_err());

Attempt to convert a Bytes buffer to a MetadataValue.

For MetadataValue<Ascii>, if the argument contains invalid metadata value bytes, an error is returned. Only byte values between 32 and 255 (inclusive) are permitted, excluding byte 127 (DEL).

For MetadataValue<Binary>, if the argument is not valid base64, an error is returned. In use cases where the input is not base64 encoded, use from_bytes; if the value has to be encoded it’s not possible to share the memory anyways.

This function is intended to be replaced in the future by a TryFrom implementation once the trait is stabilized in std.

Convert a Bytes directly into a MetadataValue without validating. For MetadataValue the provided parameter must be base64 encoded without padding bytes at the end.

Safety

This function does NOT validate that illegal bytes are not contained within the buffer.

Returns true if the MetadataValue has a length of zero bytes.

Examples
let val = AsciiMetadataValue::from_static("");
assert!(val.is_empty());

let val = AsciiMetadataValue::from_static("hello");
assert!(!val.is_empty());

Converts a MetadataValue to a Bytes buffer. This method cannot fail for Ascii values. For Ascii values, as_bytes is more convenient to use.

Examples
let val = AsciiMetadataValue::from_static("hello");
assert_eq!(val.to_bytes().unwrap().as_ref(), b"hello");
let val = BinaryMetadataValue::from_bytes(b"hello");
assert_eq!(val.to_bytes().unwrap().as_ref(), b"hello");

Mark that the metadata value represents sensitive information.

Examples
let mut val = AsciiMetadataValue::from_static("my secret");

val.set_sensitive(true);
assert!(val.is_sensitive());

val.set_sensitive(false);
assert!(!val.is_sensitive());

Returns true if the value represents sensitive data.

Sensitive data could represent passwords or other data that should not be stored on disk or in memory. This setting can be used by components like caches to avoid storing the value. HPACK encoders must set the metadata field to never index when is_sensitive returns true.

Note that sensitivity is not factored into equality or ordering.

Examples
let mut val = AsciiMetadataValue::from_static("my secret");

val.set_sensitive(true);
assert!(val.is_sensitive());

val.set_sensitive(false);
assert!(!val.is_sensitive());

Converts a MetadataValue to a byte slice. For Binary values, the return value is base64 encoded.

Examples
let val = AsciiMetadataValue::from_static("hello");
assert_eq!(val.as_encoded_bytes(), b"hello");
let val = BinaryMetadataValue::from_bytes(b"Hello!");
assert_eq!(val.as_encoded_bytes(), b"SGVsbG8h");

Attempt to convert a string to a MetadataValue<Ascii>.

If the argument contains invalid metadata value characters, an error is returned. Only visible ASCII characters (32-127) are permitted. Use from_bytes to create a MetadataValue that includes opaque octets (128-255).

This function is intended to be replaced in the future by a TryFrom implementation once the trait is stabilized in std.

Examples
let val = AsciiMetadataValue::from_str("hello").unwrap();
assert_eq!(val, "hello");

An invalid value

let val = AsciiMetadataValue::from_str("\n");
assert!(val.is_err());

Converts a MetadataKey into a MetadataValue.

Since every valid MetadataKey is a valid MetadataValue this is done infallibly.

Examples
let val = AsciiMetadataValue::from_key::<Ascii>("accept".parse().unwrap());
assert_eq!(val, AsciiMetadataValue::try_from_bytes(b"accept").unwrap());

Returns the length of self, in bytes.

This method is not available for MetadataValue because that cannot be implemented in constant time, which most people would probably expect. To get the length of MetadataValue, convert it to a Bytes value and measure its length.

Examples
let val = AsciiMetadataValue::from_static("hello");
assert_eq!(val.len(), 5);

Yields a &str slice if the MetadataValue only contains visible ASCII chars.

This function will perform a scan of the metadata value, checking all the characters.

Examples
let val = AsciiMetadataValue::from_static("hello");
assert_eq!(val.to_str().unwrap(), "hello");

Converts a MetadataValue to a byte slice. For Binary values, use to_bytes.

Examples
let val = AsciiMetadataValue::from_static("hello");
assert_eq!(val.as_bytes(), b"hello");

Convert a byte slice to a MetadataValue<Binary>.

Examples
let val = BinaryMetadataValue::from_bytes(b"hello\xfa");
assert_eq!(val, &b"hello\xfa"[..]);

Trait Implementations

Performs the conversion.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Performs the conversion.

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

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

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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