DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Using TLS in Rust: The Complexity of Async, Macros, and Madness

Using TLS in Rust: The Complexity of Async, Macros, and Madness

Learn more about the complexity of the async I/O implementation.

Oren Eini user avatar by
Oren Eini
·
Feb. 15, 19 · Tutorial
Like (1)
Save
Tweet
Share
5.28K Views

Join the DZone community and get the full member experience.

Join For Free

After a lot of trouble, I’m really happy that I was able to build an async I/O implementation of my protocol. However, for real code, I think that I would probably recommend using with the sync API instead since at least it's straightforward and doesn’t incur so much overhead at development time. The async stuff is still very much a “use at your own risk” kind of deal from my perspective. And I can’t imagine trying to use it in a large project and no suffering from the complexity.

As a good example, take a look at the following bit of code:

image

It doesn’t seem to be doing much, right? And it is clear what the intent of the code is.

However, if you try to compile this code, you’ll get:

image

Now, it took me a long time to figure out what is going on. The issue is that the code I’m seeing isn’t the actual code because of macro expansions.

So, let’s resolve this and see what the expanded code looks like:

fn send_results(
    receiver: Receiver<String>,
) -> impl ::futures::__rt::MyFuture<std::result::Result<(), io::Error>> + 'static {
::futures::__rt::gen(move || -> std::result::Result<(), io::Error> {
let __e: std::result::Result<(), io::Error> = {
            {
                {
let mut __stream = receiver;
loop {
let msg = {
extern crate futures_await;
let r = futures_await::Stream::poll(&mut __stream)?;
match r {
                                futures_await::Async::Ready(e) => match e {
                                    futures_await::__rt::std::option::Option::Some(e) => e,
                                    futures_await::__rt::std::option::Option::None => break,
                                },
                                futures_await::Async::NotReady => {
yield futures_await::Async::NotReady;
continue;
                                }
                            }
                        };
                        {
                            {
                                $crate::io::_print($crate::fmt::Arguments::new_v1_formatted(
&["", "\n"],
&match (&msg,) {
                                        (arg0,) => [$crate::fmt::ArgumentV1::new(
                                            arg0,
                                            $crate::fmt::Display::fmt,
                                        )],
                                    },
&[$crate::fmt::rt::v1::Argument {
                                        position: $crate::fmt::rt::v1::Position::At(0usize),
                                        format: $crate::fmt::rt::v1::FormatSpec {
                                            fill: ' ',
                                            align: $crate::fmt::rt::v1::Alignment::Unknown,
                                            flags: 0u32,
                                            precision: $crate::fmt::rt::v1::Count::Implied,
                                            width: $crate::fmt::rt::v1::Count::Implied,
                                        },
                                    }],
                                ));
                            };
                        }
                    }
                }
Ok(())
            }
        };
        #[allow(unreachable_code)]
        {
return __e;
loop {
yield ::futures::Async::NotReady
            }
        }
    })
}


This is after formatting, of course, but it certainly looks scary. Glancing at this code doesn’t tell me what the problem is, so I tried replacing the method with the expanded result, and I got the same error. But this time, I got it on a line that helped me figure it out. Here is the issue:

image

We use the ? to return early from the poll method, and the Receiver I’m using, in this case, is defined to have a Result<String, ()>, so this is the cause of the problem.

I returned my own error type as a result, giving me the ability to convert from (), but that was a really hard thing to resolve.

It might be better to have Rust also offer to show the error on the expanded code by default because it was somewhat of a chore to actually get to this.

What made this oh so confusing is that I had the exact same code, but using a Stream<String, io:Error> that worked, obviously. But it was decidedly non-obvious to see the difference between two identical pieces of code.

TLS Rust (programming language)

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Testing as a Service?
  • Why the World Is Moving Towards Serverless Computing
  • The Changing Face of ETL
  • Connecting Your Devs' Work to the Business

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: