[go: up one dir, main page]

Struct Any

Source
#[non_exhaustive]
pub struct Any(/* private fields */);
Expand description

Any contains an arbitrary serialized protocol buffer message along with a URL that describes the type of the serialized message.

§Example

let duration = Duration::clamp(123, 456);
let any = Any::from_msg(&duration)?;
let extracted = any.to_msg::<Duration>()?;
assert_eq!(extracted, duration);
let fail = any.to_msg::<Timestamp>();
assert!(matches!(fail, Err(AnyError::TypeMismatch{..})));

Protobuf library provides support to pack/unpack Any values in the form of utility functions or additional generated methods of the Any type.

§JSON

The JSON representation of an Any value uses the regular representation of the deserialized, embedded message, with an additional field @type which contains the type URL. Example:

    package google.profile;
    message Person {
      string first_name = 1;
      string last_name = 2;
    }

    {
      "@type": "type.googleapis.com/google.profile.Person",
      "firstName": <string>,
      "lastName": <string>
    }

If the embedded message type is well-known and has a custom JSON representation, that representation will be embedded adding a field value which holds the custom JSON in addition to the @type field. Example (for message [google.protobuf.Duration][]):

    {
      "@type": "type.googleapis.com/google.protobuf.Duration",
      "value": "1.212s"
    }

Implementations§

Source§

impl Any

Source

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

Returns the name of the contained type.

§Example
use google_cloud_wkt::message::Message;
let any = Any::from_msg(&Duration::clamp(123, 456))?;
assert_eq!(any.type_url(), Some(Duration::typename()));

An Any may contain any message type. The name of the message is a URL, usually with the https:// scheme elided. All types in Google Cloud APIs are of the form type.googleapis.com/${fully-qualified-name}.

Note that this is not an available URL where you can download data (such as the message schema) from.

Source

pub fn from_msg<T>(message: &T) -> Result<Self, AnyError>
where T: Message,

Creates a new Any from any Message that also supports serialization to JSON.

§Example
let any = Any::from_msg(&Duration::clamp(123, 456))?;
Source

pub fn to_msg<T>(&self) -> Result<T, AnyError>
where T: Message,

Extracts (if possible) a T value from the Any.

§Example
let any = Any::from_msg(&Duration::clamp(123, 456))?;
let duration = any.to_msg::<Duration>()?;
assert_eq!(duration, Duration::clamp(123, 456));

Trait Implementations§

Source§

impl Clone for Any

Source§

fn clone(&self) -> Any

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Any

Source§

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

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

impl Default for Any

Source§

fn default() -> Any

Returns the “default value” for a type. Read more
Source§

impl Message for Any

Source§

fn typename() -> &'static str

The typename of this message.
Source§

impl PartialEq for Any

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Any

Auto Trait Implementations§

§

impl Freeze for Any

§

impl RefUnwindSafe for Any

§

impl Send for Any

§

impl Sync for Any

§

impl Unpin for Any

§

impl UnwindSafe for Any

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<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, 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,