Skip to content

Stop circumventing the std definition of FromUtf8Error #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,11 +750,10 @@ impl Socket {

/// Receive a `String` from the socket.
///
/// If the received message is not valid UTF-8, it is returned as the original
/// Vec in the `Err` part of the inner result.
pub fn recv_string(&self, flags: i32) -> Result<result::Result<String, Vec<u8>>> {
self.recv_bytes(flags)
.map(|bytes| String::from_utf8(bytes).map_err(FromUtf8Error::into_bytes))
/// If the received message is not valid UTF-8, a [`FromUtf8Error`] is returned and the original
/// bytes can be retrieved via [`FromUtf8Error::into_bytes()`].
pub fn recv_string(&self, flags: i32) -> Result<result::Result<String, FromUtf8Error>> {
self.recv_bytes(flags).map(String::from_utf8)
}

/// Receive a multipart message from the socket.
Expand Down Expand Up @@ -885,7 +884,7 @@ impl Socket {
sockopt::get_bytes(self.sock, zmq_sys::ZMQ_ROUTING_ID as c_int, 255)
}

pub fn get_socks_proxy(&self) -> Result<result::Result<String, Vec<u8>>> {
pub fn get_socks_proxy(&self) -> Result<result::Result<String, FromUtf8Error>> {
// 255 = longest allowable domain name is 253 so this should
// be a reasonable size.
sockopt::get_string(self.sock, zmq_sys::ZMQ_SOCKS_PROXY as c_int, 255, true)
Expand All @@ -901,17 +900,17 @@ impl Socket {
})
}

pub fn get_plain_username(&self) -> Result<result::Result<String, Vec<u8>>> {
pub fn get_plain_username(&self) -> Result<result::Result<String, FromUtf8Error>> {
// 255 = arbitrary size
sockopt::get_string(self.sock, zmq_sys::ZMQ_PLAIN_USERNAME as c_int, 255, true)
}

pub fn get_plain_password(&self) -> Result<result::Result<String, Vec<u8>>> {
pub fn get_plain_password(&self) -> Result<result::Result<String, FromUtf8Error>> {
// 256 = arbitrary size based on std crypto key size
sockopt::get_string(self.sock, zmq_sys::ZMQ_PLAIN_PASSWORD as c_int, 256, true)
}

pub fn get_zap_domain(&self) -> Result<result::Result<String, Vec<u8>>> {
pub fn get_zap_domain(&self) -> Result<result::Result<String, FromUtf8Error>> {
// 255 = arbitrary size
sockopt::get_string(self.sock, zmq_sys::ZMQ_ZAP_DOMAIN as c_int, 255, true)
}
Expand All @@ -924,7 +923,7 @@ impl Socket {
/// used with the wildcard address (`"*"`), in the address
/// returned, the wildcard will be expanded into the any address
/// (i.e. `0.0.0.0` with IPv4).
pub fn get_last_endpoint(&self) -> Result<result::Result<String, Vec<u8>>> {
pub fn get_last_endpoint(&self) -> Result<result::Result<String, FromUtf8Error>> {
// 256 + 9 + 1 = maximum inproc name size (= 256) + "inproc://".len() (= 9), plus null byte
sockopt::get_string(
self.sock,
Expand Down Expand Up @@ -962,12 +961,12 @@ impl Socket {
sockopt::get_bytes(self.sock, zmq_sys::ZMQ_CURVE_SERVERKEY as c_int, 32)
}

pub fn get_gssapi_principal(&self) -> Result<result::Result<String, Vec<u8>>> {
pub fn get_gssapi_principal(&self) -> Result<result::Result<String, FromUtf8Error>> {
// 260 = best guess of max length based on docs.
sockopt::get_string(self.sock, zmq_sys::ZMQ_GSSAPI_PRINCIPAL as c_int, 260, true)
}

pub fn get_gssapi_service_principal(&self) -> Result<result::Result<String, Vec<u8>>> {
pub fn get_gssapi_service_principal(&self) -> Result<result::Result<String, FromUtf8Error>> {
// 260 = best guess of max length based on docs.
sockopt::get_string(
self.sock,
Expand Down
4 changes: 2 additions & 2 deletions src/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ pub fn get_string(
opt: c_int,
size: size_t,
remove_nulbyte: bool,
) -> Result<result::Result<String, Vec<u8>>> {
) -> Result<result::Result<String, FromUtf8Error>> {
let mut value = get_bytes(sock, opt, size)?;

if remove_nulbyte {
value.pop();
}
Ok(String::from_utf8(value).map_err(FromUtf8Error::into_bytes))
Ok(String::from_utf8(value))
}

macro_rules! setsockopt_num(
Expand Down
2 changes: 1 addition & 1 deletion tests/compile-fail/socket-thread-unsafe.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ error[E0277]: `*mut c_void` cannot be shared between threads safely
15 | | });
| |_____^ `*mut c_void` cannot be shared between threads safely
|
= help: within `Socket`, the trait `Sync` is not implemented for `*mut c_void`, which is required by `{closure@$DIR/tests/compile-fail/socket-thread-unsafe.rs:13:27: 13:34}: Send`
= help: within `Socket`, the trait `Sync` is not implemented for `*mut c_void`
note: required because it appears within the type `Socket`
--> src/lib.rs
|
Expand Down
6 changes: 5 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod common;

use std::io;
use std::net::TcpStream;
use std::string::FromUtf8Error;
use zmq::*;

fn version_ge_4_2() -> bool {
Expand Down Expand Up @@ -64,7 +65,10 @@ test!(test_exchanging_strings, {
// non-UTF8 strings -> get an Err with bytes when receiving
receiver.send(b"\xff\xb7".as_ref(), 0).unwrap();
let result = sender.recv_string(0).unwrap();
assert_eq!(result, Err(vec![0xff, 0xb7]));
assert_eq!(
result.map_err(FromUtf8Error::into_bytes),
Err(vec![0xff, 0xb7])
);
});

test!(test_exchanging_multipart, {
Expand Down