diff --git a/src/lib.rs b/src/lib.rs index dbb4c64a..2db9a7d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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>> { - 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> { + self.recv_bytes(flags).map(String::from_utf8) } /// Receive a multipart message from the socket. @@ -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>> { + pub fn get_socks_proxy(&self) -> Result> { // 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) @@ -901,17 +900,17 @@ impl Socket { }) } - pub fn get_plain_username(&self) -> Result>> { + pub fn get_plain_username(&self) -> Result> { // 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>> { + pub fn get_plain_password(&self) -> Result> { // 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>> { + pub fn get_zap_domain(&self) -> Result> { // 255 = arbitrary size sockopt::get_string(self.sock, zmq_sys::ZMQ_ZAP_DOMAIN as c_int, 255, true) } @@ -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>> { + pub fn get_last_endpoint(&self) -> Result> { // 256 + 9 + 1 = maximum inproc name size (= 256) + "inproc://".len() (= 9), plus null byte sockopt::get_string( self.sock, @@ -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>> { + pub fn get_gssapi_principal(&self) -> Result> { // 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>> { + pub fn get_gssapi_service_principal(&self) -> Result> { // 260 = best guess of max length based on docs. sockopt::get_string( self.sock, diff --git a/src/sockopt.rs b/src/sockopt.rs index c0907ddd..75b7a437 100644 --- a/src/sockopt.rs +++ b/src/sockopt.rs @@ -63,13 +63,13 @@ pub fn get_string( opt: c_int, size: size_t, remove_nulbyte: bool, -) -> Result>> { +) -> Result> { 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( diff --git a/tests/compile-fail/socket-thread-unsafe.stderr b/tests/compile-fail/socket-thread-unsafe.stderr index 7fb2ab6a..a50ea1b6 100644 --- a/tests/compile-fail/socket-thread-unsafe.stderr +++ b/tests/compile-fail/socket-thread-unsafe.stderr @@ -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 | diff --git a/tests/test.rs b/tests/test.rs index bb1aa0d8..c05360bf 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -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 { @@ -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, {