Skip to content

Commit 6675fd1

Browse files
committed
[generalize] impl <T: AsRef<str>> SetterInput<text::Owned> for T
1 parent 2d79832 commit 6675fd1

File tree

3 files changed

+10
-29
lines changed

3 files changed

+10
-29
lines changed

capnp-rpc/examples/hello-world/server.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ impl hello_world::Server for HelloWorldImpl {
3838
let request = pry!(pry!(params.get()).get_request());
3939
let name = pry!(pry!(request.get_name()).to_str());
4040
let message = format!("Hello, {name}!");
41-
42-
results.get().init_reply().set_message(&message[..]);
41+
results.get().init_reply().set_message(message);
4342

4443
Promise::ok(())
4544
}

capnp-rpc/examples/pubsub/server.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
166166
if subscriber.requests_in_flight < 5 {
167167
subscriber.requests_in_flight += 1;
168168
let mut request = subscriber.client.push_message_request();
169-
request.get().set_message(
170-
&format!("system time is: {:?}", ::std::time::SystemTime::now())[..],
171-
)?;
169+
request.get().set_message(format!(
170+
"system time is: {:?}",
171+
::std::time::SystemTime::now()
172+
))?;
172173
let subscribers2 = subscribers1.clone();
173174
tokio::task::spawn_local(request.send().promise.map(
174175
move |r| match r {

capnp/src/text.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -318,35 +318,16 @@ impl<'a> crate::traits::SetterInput<Owned> for Reader<'a> {
318318
}
319319
}
320320

321-
// Text field setters are generated with a signature like
322-
// ```
323-
// set_foo(&mut self, value: impl SetterInput<text::Owned>)
324-
// ```
325-
// Combined with the below impls of `SetterInput`, this
326-
// allows text fields to be conveniently set from values
327-
// of type `&str`.
328-
329-
impl<'a> crate::traits::SetterInput<Owned> for &'a str {
321+
// Allow text fields to be set with &str or String or anything
322+
// else that implements `AsRef<str>`.
323+
impl<T: AsRef<str>> crate::traits::SetterInput<Owned> for T {
330324
#[inline]
331325
fn set_pointer_builder<'b>(
332326
mut pointer: crate::private::layout::PointerBuilder<'b>,
333-
value: &'a str,
327+
value: T,
334328
_canonicalize: bool,
335329
) -> Result<()> {
336-
pointer.set_text(value.into());
337-
Ok(())
338-
}
339-
}
340-
341-
#[cfg(feature = "alloc")]
342-
impl<'a> crate::traits::SetterInput<Owned> for &'a alloc::string::String {
343-
#[inline]
344-
fn set_pointer_builder<'b>(
345-
mut pointer: crate::private::layout::PointerBuilder<'b>,
346-
value: &'a alloc::string::String,
347-
_canonicalize: bool,
348-
) -> Result<()> {
349-
pointer.set_text(value.as_str().into());
330+
pointer.set_text(value.as_ref().into());
350331
Ok(())
351332
}
352333
}

0 commit comments

Comments
 (0)