boring
boring copied to clipboard
tokio-boring: Add additional accessors to `HandshakeError`
Currently, the HandshakeError type in tokio-boring provides a
std::error::Error implementation and an as_io_error method that
returns an Option<&std::io::Error>, as the only ways to access the
potentially underlying error. There are a couple of cases where this is
somewhat insufficient:
-
If a user has an owned
HandshakeError, and needs an ownedio::Error, they have to create a new one, using theHandshakeError's cause and message, like this:let err: HandshakeError<_> = // ...; if let Some(io_err) = err.as_io_error() { return Err(io::Error::new(io_err.cause(), io_err.to_string())); } // ...This seems like kind of a shame, since it allocates two additional times (for the
Stringand then again for theio::Erroritself), and deallocates the originalio::Errorat the end of the scope. None of this should be necessary, since theHandshakeErrorin this case is owned and the originalio::Errorcould be returned. -
HandshakeErroronly implementsfmt::Debugandfmt::Displaywhen the wrapped I/O stream it's generic over implementsDebug. This means that bounding theStype withDebugis necessary forHandshakeErrorto implement theErrortrait. In some cases, introducing aDebugbound on a generic I/O type is kind of a heavyweight requirement. Therefore, it would be nice to have a way to extract a type that always implementsErrorfrom theHandshakeError, in cases where the returned I/O stream is just going to be discarded if the handshake failed.
This branch introduces new functions on HandshakeError:
-
HandshakeError::as_ssl_error, which is analogous toHandshakeError::as_io_errorbut returning anOption<&boring::error::ErrorStack>instead of an I/O error. -
HandshakeError::into_io_errorandHandshakeError::into_ssl_error, which consume the error and return anOption<io::Error>or anOption<ErrorStack>, respectively.I noticed that some of the
into_$TYPEmethods on errors in theboringcrate returnResults rather thanOptions so that the original error can be reused if it couldn't be converted into the requested type. However, this can't easily be done inHandshakeError'sinto_io_errorandinto_ssl_error, because these methods callMidHandshakeSslStream::into_error, and (sinceMidHandshakeSslStreamcan only be constructed by theboringcrate, not intokio-boring), we can't ever get theMidHandshakeSslStreamback...so we can't return the originalSelftype in this case.
Hopefully this should make it much easier to convert HandshakeErrors
into other error types as required in user code!
Signed-off-by: Eliza Weisman [email protected]
Do be aware though that we're considering larger changes to the error types soon: #20
Hmm, if the intent is to make larger changes in the near future, is it worth moving forwards with the changes in this branch, or should I just try to make the changes proposed in #20?
should I just try to make the changes proposed in #20?
That would be really helpful! I'd love to hear if that new design fixes your issue.
Btw #142 completely removes HandshakeError.