Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change parameter type of Any::try_pack. #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> Result<(), AnyError> {
};

let mut request: Request = Request::default();
let any = Any::try_pack(foo_msg)?;
let any = Any::try_pack(&foo_msg)?;
request.request_id = "test1".to_string();
request.payload = Some(any);

Expand Down
8 changes: 4 additions & 4 deletions wkt-types/src/pbany.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ impl Any {
/// Packs a message into an `Any` containing a `type_url` which will take the format
/// of `type.googleapis.com/package_name.struct_name`, and a value containing the
/// encoded message.
pub fn try_pack<T>(message: T) -> Result<Self, AnyError>
pub fn try_pack<T>(message: &T) -> Result<Self, AnyError>
where
T: Message + MessageSerde + Default,
{
let type_url = MessageSerde::type_url(&message).to_string();
let type_url = MessageSerde::type_url(message).to_string();
// Serialize the message into a value
let mut buf = Vec::new();
buf.reserve(message.encoded_len());
Expand Down Expand Up @@ -207,7 +207,7 @@ mod tests {
let msg = Foo {
string: "Hello World!".to_string(),
};
let any = Any::try_pack(msg.clone()).unwrap();
let any = Any::try_pack(&msg).unwrap();
println!("{any:?}");
let unpacked = any.unpack_as(Foo::default()).unwrap();
println!("{unpacked:?}");
Expand All @@ -219,7 +219,7 @@ mod tests {
let msg = Foo {
string: "Hello World!".to_string(),
};
let any = Any::try_pack(msg.clone()).unwrap();
let any = Any::try_pack(&msg).unwrap();
println!("{any:?}");
let unpacked: &dyn MessageSerde = &any.unpack_as(Foo::default()).unwrap();
let downcast = unpacked.downcast_ref::<Foo>().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions wkt-types/tests/pbany_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn test_any_serialization() {
boolean: true,
value_data: Some(prost_wkt_types::Value::from("world".to_string())),
list: vec!["one".to_string(), "two".to_string()],
payload: prost_wkt_types::Any::try_pack(inner).ok(),
payload: prost_wkt_types::Any::try_pack(&inner).ok(),
};
println!(
"Serialized to string: {}",
Expand Down Expand Up @@ -150,7 +150,7 @@ fn test_any_serialize_deserialize() {
boolean: true,
value_data: Some(prost_wkt_types::Value::from("world".to_string())),
list: vec!["one".to_string(), "two".to_string()],
payload: prost_wkt_types::Any::try_pack(inner).ok(),
payload: prost_wkt_types::Any::try_pack(&inner).ok(),
};

let json = serde_json::to_string(&original).unwrap();
Expand All @@ -171,7 +171,7 @@ fn test_any_unpack() {
list: vec!["een".to_string(), "twee".to_string()],
payload: None,
};
let any = prost_wkt_types::Any::try_pack(payload).unwrap();
let any = prost_wkt_types::Any::try_pack(&payload).unwrap();
let unpacked = any.try_unpack().unwrap();
let foo = unpacked.downcast_ref::<Foo>().unwrap();
println!("Unpacked: {foo:?}");
Expand Down
Loading