Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 6be7b55

Browse files
maurerTreehugger Robot
authored andcommitted
ANDROID: rust: Use From instances for JSON encoder
This makes it easier to deal with nested objects or arrays. This is Android specific because Rust KCFI is incomplete, and we are using the binder driver as a staging ground to fix it. This patch is intended to go upstream once the compiler behaves properly. Signed-off-by: Matthew Maurer <[email protected]> Bug: 316623888 Change-Id: I1eb623bfa522fd46fe2850278dce78b7c4e2cdf1
1 parent 2b93c38 commit 6be7b55

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

scripts/generate_rust_target.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,33 +60,33 @@ impl Display for Value {
6060
}
6161
}
6262

63-
struct TargetSpec(Object);
64-
65-
impl TargetSpec {
66-
fn new() -> TargetSpec {
67-
TargetSpec(Vec::new())
63+
impl From<bool> for Value {
64+
fn from(value: bool) -> Self {
65+
Self::Boolean(value)
6866
}
6967
}
7068

71-
trait Push<T> {
72-
fn push(&mut self, key: &str, value: T);
69+
impl From<i32> for Value {
70+
fn from(value: i32) -> Self {
71+
Self::Number(value)
72+
}
7373
}
7474

75-
impl Push<bool> for TargetSpec {
76-
fn push(&mut self, key: &str, value: bool) {
77-
self.0.push((key.to_string(), Value::Boolean(value)));
75+
impl From<String> for Value {
76+
fn from(value: String) -> Self {
77+
Self::String(value)
7878
}
7979
}
8080

81-
impl Push<i32> for TargetSpec {
82-
fn push(&mut self, key: &str, value: i32) {
83-
self.0.push((key.to_string(), Value::Number(value)));
81+
impl From<&str> for Value {
82+
fn from(value: &str) -> Self {
83+
Self::String(value.to_string())
8484
}
8585
}
8686

87-
impl Push<String> for TargetSpec {
88-
fn push(&mut self, key: &str, value: String) {
89-
self.0.push((key.to_string(), Value::String(value)));
87+
impl From<Object> for Value {
88+
fn from(object: Object) -> Self {
89+
Self::Object(object)
9090
}
9191
}
9292

@@ -96,9 +96,15 @@ impl <T: Into<Value>, const N: usize> From<[T; N]> for Value {
9696
}
9797
}
9898

99-
impl Push<Object> for TargetSpec {
100-
fn push(&mut self, key: &str, value: Object) {
101-
self.0.push((key.to_string(), Value::Object(value)));
99+
struct TargetSpec(Object);
100+
101+
impl TargetSpec {
102+
fn new() -> TargetSpec {
103+
TargetSpec(Vec::new())
104+
}
105+
106+
fn push(&mut self, key: &str, value: impl Into<Value>) {
107+
self.0.push((key.to_string(), value.into()));
102108
}
103109
}
104110

0 commit comments

Comments
 (0)