Skip to content

Commit

Permalink
[generalize] impl <T: AsRef<str>> SetterInput<text::Owned> for T
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrensha committed Jan 15, 2024
1 parent 2d79832 commit 6675fd1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
3 changes: 1 addition & 2 deletions capnp-rpc/examples/hello-world/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ impl hello_world::Server for HelloWorldImpl {
let request = pry!(pry!(params.get()).get_request());
let name = pry!(pry!(request.get_name()).to_str());
let message = format!("Hello, {name}!");

results.get().init_reply().set_message(&message[..]);
results.get().init_reply().set_message(message);

Promise::ok(())
}
Expand Down
7 changes: 4 additions & 3 deletions capnp-rpc/examples/pubsub/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
if subscriber.requests_in_flight < 5 {
subscriber.requests_in_flight += 1;
let mut request = subscriber.client.push_message_request();
request.get().set_message(
&format!("system time is: {:?}", ::std::time::SystemTime::now())[..],
)?;
request.get().set_message(format!(
"system time is: {:?}",
::std::time::SystemTime::now()
))?;
let subscribers2 = subscribers1.clone();
tokio::task::spawn_local(request.send().promise.map(
move |r| match r {
Expand Down
29 changes: 5 additions & 24 deletions capnp/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,35 +318,16 @@ impl<'a> crate::traits::SetterInput<Owned> for Reader<'a> {
}
}

// Text field setters are generated with a signature like
// ```
// set_foo(&mut self, value: impl SetterInput<text::Owned>)
// ```
// Combined with the below impls of `SetterInput`, this
// allows text fields to be conveniently set from values
// of type `&str`.

impl<'a> crate::traits::SetterInput<Owned> for &'a str {
// Allow text fields to be set with &str or String or anything
// else that implements `AsRef<str>`.
impl<T: AsRef<str>> crate::traits::SetterInput<Owned> for T {
#[inline]
fn set_pointer_builder<'b>(

Check warning on line 325 in capnp/src/text.rs

View workflow job for this annotation

GitHub Actions / lint

the following explicit lifetimes could be elided: 'b

warning: the following explicit lifetimes could be elided: 'b --> capnp/src/text.rs:325:28 | 325 | fn set_pointer_builder<'b>( | ^^ 326 | mut pointer: crate::private::layout::PointerBuilder<'b>, | ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes = note: `#[warn(clippy::needless_lifetimes)]` on by default help: elide the lifetimes | 325 ~ fn set_pointer_builder( 326 ~ mut pointer: crate::private::layout::PointerBuilder<'_>, |
mut pointer: crate::private::layout::PointerBuilder<'b>,
value: &'a str,
value: T,
_canonicalize: bool,
) -> Result<()> {
pointer.set_text(value.into());
Ok(())
}
}

#[cfg(feature = "alloc")]
impl<'a> crate::traits::SetterInput<Owned> for &'a alloc::string::String {
#[inline]
fn set_pointer_builder<'b>(
mut pointer: crate::private::layout::PointerBuilder<'b>,
value: &'a alloc::string::String,
_canonicalize: bool,
) -> Result<()> {
pointer.set_text(value.as_str().into());
pointer.set_text(value.as_ref().into());
Ok(())
}
}
Expand Down

0 comments on commit 6675fd1

Please sign in to comment.