GithubHelp home page GithubHelp logo

Comments (3)

sempervictus avatar sempervictus commented on June 27, 2024

Interesting, so if i break it up as:

    loop {
        let cp = consumer.poll().unwrap();
        for ms in cp.iter() {
...

it gets rather unhappy with me, staying

error[E0597]: `cp` does not live long enough
   --> src/main.rs:176:19
    |
176 |         for ms in cp.iter() {
    |                   ^^-------
    |                   |
    |                   borrowed value does not live long enough
    |                   argument requires that `cp` is borrowed for `'static`
...
203 |     }
    |     - `cp` dropped here while still borrowed

whereas borrowing cp gives me:

error[E0277]: `&MessageSetsIter<'_>` is not an iterator
   --> src/main.rs:176:19
    |
176 |         for ms in &cp.iter() {
    |                   -^^^^^^^^^
    |                   |
    |                   `&MessageSetsIter<'_>` is not an iterator
    |                   help: consider removing the leading `&`-reference
    |
    = help: the trait `Iterator` is not implemented for `&MessageSetsIter<'_>`
    = note: `Iterator` is implemented for `&mut kafka::consumer::MessageSetsIter<'_>`, but not for `&kafka::consumer::MessageSetsIter<'_>`
    = note: required because of the requirements on the impl of `IntoIterator` for `&MessageSetsIter<'_>`
    = note: required by `into_iter`

i may have angered the Rust gods at this point, but we wouldn't have fire or skyscrapers if we let that stop us.

Next i commented out the async tokio call and sure enough, it compiled without error. However, the idea here is to kick actual data and back-end ops async to their own co-routines, so i think i need to do something with what is represented as cp in my current code (MessageSets type) to make it live long enough to make the compiler happy.
What's the tokio-proper approach to doing this?

from kafka-rust.

sempervictus avatar sempervictus commented on June 27, 2024

Victory. In case anyone runs into this down the line - turns out that a copy of the message bytes needs to be made prior to the move a la:

    loop {
        for ms in consumer.poll().unwrap().iter() {
            for m in ms.messages() {
                // Get MySQL client connection, moved into async block
                let mut mysqlconn = mysqlpool.get_conn()?;
                // Clone and own the message bytes
                let bytes = m.value[..].to_vec().to_owned();
                // Handle the message processing async
                tokio::spawn(async move {
                    // Convert copy of dequeued bytes to Message structure
                    let raw = String::from_utf8_lossy(&bytes);
                    // ...
                    let sent_sql = send_sql(&somevar, mysqlconn);
                    // ...
                });
            }
            consumer.consume_messageset(ms);
        }
...

from kafka-rust.

johnward avatar johnward commented on June 27, 2024

Great stuff! Thank you.

from kafka-rust.

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.