GithubHelp home page GithubHelp logo

Comments (4)

stearnsc avatar stearnsc commented on July 17, 2024

I'm totally fine adding ?Sized to the signature as a fix to get it working now (that will also fix the str impl). Adding an impl for Vec<u8> also doesn't bother me, although I was thinking the slice impl would cover that use case.

Long term, I don't feel like I've fully thought through the implications of the Serialize/DeserializeMessage traits as the basis for handling this problem. Given the way the proto works, we need to have owned data in the payload, but most instance of serialization (thinking serde here) will involve copying data at some point, so for those, taking by ref seems to make the most sense. However, that makes it impossible to just pass in an owned Vec without falling back to send_raw.

Alternatively, we could change SerializeMessage::serialize_message to take Self by value, change the signature of send and send_all to take T: SerializeMessage by value, and then impl SerializeMessage for borrowed types in cases where it makes sense (&Foo instead of Foo). My immediate instinct is that that approach feels like an improvement, but I don't feel like I've thought it through enough.

from pulsar-rs.

leshow avatar leshow commented on July 17, 2024

I fiddled around with it earlier today when I originally came across this problem and made Self by value. It works fine, the only problem there is that you may only have a reference to your data, in which case you'll have to clone it at the call site.

I think there could be a third solution, maybe Message holds onto a reference and has a lifetime, like:

struct Message<'a> {
   payload: &'a [u8],
...
}

then,

impl<'a> SerializeMessage for &'a [u8] {
   fn serialize_message(payload: Self) -> Message {
      Message { payload, ..Default::default()
   }
}

I haven't actually tried this though, but something to think about. It could even be something like:

struct Message<'a, B> {  // I believe B: 'a is elided now since 2018
   payload: &'a B,
...
}

Then bound <B: AsRef<[u8]>> in it's impl's.

from pulsar-rs.

stearnsc avatar stearnsc commented on July 17, 2024

Unfortunately Message has to be 'static because it's immediately sent by channel to the async runtime to be handled.

the only problem there is that you may only have a reference to your data, in which case you'll have to clone it at the call site.

This works if you impl SerializeMessage for a borrow of your struct, something like

#[derive(Debug, Clone, Serialize)]
pub struct Foo {
    pub a: String,
    pub b: u64,
}

impl SerializeMessage for &Foo {
    fn serialize_message(input: &Foo) -> Result<producer::Message, Error> {
        let payload = serde_json::to_vec(input).map_err(..)?;
        Ok(producer::Message { payload, ..producer::Message::default() })
    }
}

then you should be able to still call producer.send(..) with your &Foo

from pulsar-rs.

stearnsc avatar stearnsc commented on July 17, 2024

Fixed in #61 (at least until we make SerializeMessage work by value instead of by ref)

from pulsar-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.