Skip to content
Open
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
28 changes: 24 additions & 4 deletions src/spanner/src/to_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,24 @@ impl ToValue for ProtoValue {

impl ToValue for String {
fn to_value(&self) -> Value {
Value(ProtoValue {
kind: Some(prost_types::value::Kind::StringValue(self.clone())),
})
<str as ToValue>::to_value(self)
}
}

impl ToValue for &str {
impl ToValue for str {
fn to_value(&self) -> Value {
Value(ProtoValue {
kind: Some(prost_types::value::Kind::StringValue(self.to_string())),
})
}
}

impl ToValue for &str {
fn to_value(&self) -> Value {
<str as ToValue>::to_value(*self)
}
}

impl ToValue for i64 {
fn to_value(&self) -> Value {
Value(ProtoValue {
Expand Down Expand Up @@ -211,6 +215,22 @@ mod tests {
assert_eq!(v.as_string(), "world");
}

#[test]
fn test_to_value_str_trait_bound() {
fn bind_param<T: ToValue + ?Sized>(val: &T) -> Value {
val.to_value()
}

let v = bind_param("hello");
assert_eq!(v.kind(), Kind::String);
assert_eq!(v.as_string(), "hello");

let owned: String = "hello".to_string();
assert_eq!(bind_param(&owned), v);
let borrowed: &str = &owned;
assert_eq!(bind_param(&borrowed), v);
}

#[test]
fn test_to_value_int() {
let v = 42i64.to_value();
Expand Down
Loading