DZone
Security Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Security Zone > 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 · Security Zone · Tutorial
Like (1)
Save
Tweet
5.17K 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

  • MuleSoft Logs Integration With Datadog
  • Mobile App Development Trends
  • Open API and Omnichannel with Apache Kafka in Healthcare
  • SRE From Theory to Practice: What's Difficult About On-Call?

Comments

Security Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo