about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-03-22 21:00:07 +0000
committerbors <bors@rust-lang.org>2019-03-22 21:00:07 +0000
commit9f91bee03f3eea93285330354dda54706028671c (patch)
tree0b8cb0de7ea1416081cfa16e36ea400a9d966498 /src/libstd
parentcb2f34dc6d7e83d8bcfef79e0388d49f0c24aca7 (diff)
parentcf8c73936d48560b50d98a299c98fd35e5b4581e (diff)
downloadrust-9f91bee03f3eea93285330354dda54706028671c.tar.gz
rust-9f91bee03f3eea93285330354dda54706028671c.zip
Auto merge of #59370 - Centril:rollup, r=Centril
Rollup of 18 pull requests

Successful merges:

 - #59106 (Add peer_addr function to UdpSocket)
 - #59170 (Add const generics to rustdoc)
 - #59172 (Update and clean up several parts of CONTRIBUTING.md)
 - #59190 (consistent naming for Rhs type parameter in libcore/ops)
 - #59236 (Rename miri component to miri-preview)
 - #59266 (Do not complain about non-existing fields after parse recovery)
 - #59273 (some small HIR doc improvements)
 - #59291 (Make Option<ThreadId> no larger than ThreadId, with NonZeroU64)
 - #59297 (convert field/method confusion help to suggestions)
 - #59304 (Move some bench tests back from libtest)
 - #59309 (Add messages for different verbosity levels. Output copy actions.)
 - #59321 (Unify E0109, E0110 and E0111)
 - #59322 (Tweak incorrect escaped char diagnostic)
 - #59323 (use suggestions for "enum instead of variant" error)
 - #59327 (Add NAN test to docs)
 - #59329 (cleanup: Remove compile-fail-fulldeps directory again)
 - #59347 (Move one test from run-make-fulldeps to ui)
 - #59360 (Add tracking issue number for `seek_convenience`)

Failed merges:

r? @ghost
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/f32.rs19
-rw-r--r--src/libstd/f64.rs19
-rw-r--r--src/libstd/io/mod.rs4
-rw-r--r--src/libstd/net/udp.rs43
-rw-r--r--src/libstd/sys/cloudabi/shims/net.rs4
-rw-r--r--src/libstd/sys/redox/net/udp.rs5
-rw-r--r--src/libstd/sys/sgx/net.rs4
-rw-r--r--src/libstd/sys/unix/l4re.rs5
-rw-r--r--src/libstd/sys/wasm/net.rs4
-rw-r--r--src/libstd/sys_common/net.rs6
-rw-r--r--src/libstd/thread/mod.rs15
11 files changed, 120 insertions, 8 deletions
diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs
index 2fabe30fa93..688d9c1aabb 100644
--- a/src/libstd/f32.rs
+++ b/src/libstd/f32.rs
@@ -970,6 +970,7 @@ impl f32 {
     /// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32);
     /// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32);
     /// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32);
+    /// assert!((std::f32::NAN).clamp(-2.0f32, 1.0f32).is_nan());
     /// ```
     #[unstable(feature = "clamp", issue = "44095")]
     #[inline]
@@ -1581,4 +1582,22 @@ mod tests {
         assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
         assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
     }
+
+    #[test]
+    #[should_panic]
+    fn test_clamp_min_greater_than_max() {
+        1.0f32.clamp(3.0, 1.0);
+    }
+
+    #[test]
+    #[should_panic]
+    fn test_clamp_min_is_nan() {
+        1.0f32.clamp(NAN, 1.0);
+    }
+
+    #[test]
+    #[should_panic]
+    fn test_clamp_max_is_nan() {
+        1.0f32.clamp(3.0, NAN);
+    }
 }
diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs
index a471117f6d6..b171e1c7ac9 100644
--- a/src/libstd/f64.rs
+++ b/src/libstd/f64.rs
@@ -892,6 +892,7 @@ impl f64 {
     /// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64);
     /// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64);
     /// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64);
+    /// assert!((std::f64::NAN).clamp(-2.0f64, 1.0f64).is_nan());
     /// ```
     #[unstable(feature = "clamp", issue = "44095")]
     #[inline]
@@ -1522,4 +1523,22 @@ mod tests {
         assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
         assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
     }
+
+    #[test]
+    #[should_panic]
+    fn test_clamp_min_greater_than_max() {
+        1.0f64.clamp(3.0, 1.0);
+    }
+
+    #[test]
+    #[should_panic]
+    fn test_clamp_min_is_nan() {
+        1.0f64.clamp(NAN, 1.0);
+    }
+
+    #[test]
+    #[should_panic]
+    fn test_clamp_max_is_nan() {
+        1.0f64.clamp(3.0, NAN);
+    }
 }
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs
index 4c88fc889f3..7147b641e47 100644
--- a/src/libstd/io/mod.rs
+++ b/src/libstd/io/mod.rs
@@ -1381,7 +1381,7 @@ pub trait Seek {
     ///     Ok(())
     /// }
     /// ```
-    #[unstable(feature = "seek_convenience", issue = "0")]
+    #[unstable(feature = "seek_convenience", issue = "59359")]
     fn stream_len(&mut self) -> Result<u64> {
         let old_pos = self.stream_position()?;
         let len = self.seek(SeekFrom::End(0))?;
@@ -1420,7 +1420,7 @@ pub trait Seek {
     ///     Ok(())
     /// }
     /// ```
-    #[unstable(feature = "seek_convenience", issue = "0")]
+    #[unstable(feature = "seek_convenience", issue = "59359")]
     fn stream_position(&mut self) -> Result<u64> {
         self.seek(SeekFrom::Current(0))
     }
diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs
index edc9d665444..b42a8123042 100644
--- a/src/libstd/net/udp.rs
+++ b/src/libstd/net/udp.rs
@@ -180,6 +180,37 @@ impl UdpSocket {
         }
     }
 
+    /// Returns the socket address of the remote peer this socket was connected to.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(udp_peer_addr)]
+    /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.connect("192.168.0.1:41203").expect("couldn't connect to address");
+    /// assert_eq!(socket.peer_addr().unwrap(),
+    ///            SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 0, 1), 41203)));
+    /// ```
+    ///
+    /// If the socket isn't connected, it will return a [`NotConnected`] error.
+    ///
+    /// [`NotConnected`]: ../../std/io/enum.ErrorKind.html#variant.NotConnected
+    ///
+    /// ```no_run
+    /// #![feature(udp_peer_addr)]
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// assert_eq!(socket.peer_addr().unwrap_err().kind(),
+    ///            ::std::io::ErrorKind::NotConnected);
+    /// ```
+    #[unstable(feature = "udp_peer_addr", issue = "59127")]
+    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
+        self.0.peer_addr()
+    }
+
     /// Returns the socket address that this socket was created from.
     ///
     /// # Examples
@@ -865,7 +896,7 @@ mod tests {
     }
 
     #[test]
-    fn socket_name_ip4() {
+    fn socket_name() {
         each_ip(&mut |addr, _| {
             let server = t!(UdpSocket::bind(&addr));
             assert_eq!(addr, t!(server.local_addr()));
@@ -873,6 +904,16 @@ mod tests {
     }
 
     #[test]
+    fn socket_peer() {
+        each_ip(&mut |addr1, addr2| {
+            let server = t!(UdpSocket::bind(&addr1));
+            assert_eq!(server.peer_addr().unwrap_err().kind(), ErrorKind::NotConnected);
+            t!(server.connect(&addr2));
+            assert_eq!(addr2, t!(server.peer_addr()));
+        })
+    }
+
+    #[test]
     fn udp_clone_smoke() {
         each_ip(&mut |addr1, addr2| {
             let sock1 = t!(UdpSocket::bind(&addr1));
diff --git a/src/libstd/sys/cloudabi/shims/net.rs b/src/libstd/sys/cloudabi/shims/net.rs
index 6d2a4962ab4..4364a136544 100644
--- a/src/libstd/sys/cloudabi/shims/net.rs
+++ b/src/libstd/sys/cloudabi/shims/net.rs
@@ -159,6 +159,10 @@ impl UdpSocket {
         unsupported()
     }
 
+    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
+        match self.0 {}
+    }
+
     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
         match self.0 {}
     }
diff --git a/src/libstd/sys/redox/net/udp.rs b/src/libstd/sys/redox/net/udp.rs
index b1a60b14570..274123dce4b 100644
--- a/src/libstd/sys/redox/net/udp.rs
+++ b/src/libstd/sys/redox/net/udp.rs
@@ -72,6 +72,11 @@ impl UdpSocket {
         Ok(None)
     }
 
+    pub fn peer_addr(&self) -> Result<SocketAddr> {
+        let path = self.0.path()?;
+        Ok(path_to_peer_addr(path.to_str().unwrap_or("")))
+    }
+
     pub fn socket_addr(&self) -> Result<SocketAddr> {
         let path = self.0.path()?;
         Ok(path_to_local_addr(path.to_str().unwrap_or("")))
diff --git a/src/libstd/sys/sgx/net.rs b/src/libstd/sys/sgx/net.rs
index e167e917957..81f33c16294 100644
--- a/src/libstd/sys/sgx/net.rs
+++ b/src/libstd/sys/sgx/net.rs
@@ -249,6 +249,10 @@ impl UdpSocket {
         unsupported()
     }
 
+    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
+        match self.0 {}
+    }
+
     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
         match self.0 {}
     }
diff --git a/src/libstd/sys/unix/l4re.rs b/src/libstd/sys/unix/l4re.rs
index b9e725371a3..b3dd1cf6aaa 100644
--- a/src/libstd/sys/unix/l4re.rs
+++ b/src/libstd/sys/unix/l4re.rs
@@ -292,6 +292,10 @@ pub mod net {
 
         pub fn into_socket(self) -> Socket { self.inner }
 
+        pub fn peer_addr(&self) -> io::Result<SocketAddr> {
+            unimpl!();
+        }
+
         pub fn socket_addr(&self) -> io::Result<SocketAddr> {
             unimpl!();
         }
@@ -463,4 +467,3 @@ pub mod net {
         }
     }
 }
-
diff --git a/src/libstd/sys/wasm/net.rs b/src/libstd/sys/wasm/net.rs
index a2ea2dfbbc0..c85dd000afe 100644
--- a/src/libstd/sys/wasm/net.rs
+++ b/src/libstd/sys/wasm/net.rs
@@ -156,6 +156,10 @@ impl UdpSocket {
         unsupported()
     }
 
+    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
+        match self.0 {}
+    }
+
     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
         match self.0 {}
     }
diff --git a/src/libstd/sys_common/net.rs b/src/libstd/sys_common/net.rs
index b9505aaa69b..b77bcee4b9d 100644
--- a/src/libstd/sys_common/net.rs
+++ b/src/libstd/sys_common/net.rs
@@ -472,6 +472,12 @@ impl UdpSocket {
 
     pub fn into_socket(self) -> Socket { self.inner }
 
+    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
+        sockname(|buf, len| unsafe {
+            c::getpeername(*self.inner.as_inner(), buf, len)
+        })
+    }
+
     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
         sockname(|buf, len| unsafe {
             c::getsockname(*self.inner.as_inner(), buf, len)
diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs
index 08f0aa2f0d2..d856f9b465e 100644
--- a/src/libstd/thread/mod.rs
+++ b/src/libstd/thread/mod.rs
@@ -163,6 +163,7 @@ use crate::ffi::{CStr, CString};
 use crate::fmt;
 use crate::io;
 use crate::mem;
+use crate::num::NonZeroU64;
 use crate::panic;
 use crate::panicking;
 use crate::str;
@@ -1036,7 +1037,7 @@ pub fn park_timeout(dur: Duration) {
 /// [`Thread`]: ../../std/thread/struct.Thread.html
 #[stable(feature = "thread_id", since = "1.19.0")]
 #[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)]
-pub struct ThreadId(u64);
+pub struct ThreadId(NonZeroU64);
 
 impl ThreadId {
     // Generate a new unique thread ID.
@@ -1044,7 +1045,7 @@ impl ThreadId {
         // We never call `GUARD.init()`, so it is UB to attempt to
         // acquire this mutex reentrantly!
         static GUARD: mutex::Mutex = mutex::Mutex::new();
-        static mut COUNTER: u64 = 0;
+        static mut COUNTER: u64 = 1;
 
         unsafe {
             let _guard = GUARD.lock();
@@ -1058,7 +1059,7 @@ impl ThreadId {
             let id = COUNTER;
             COUNTER += 1;
 
-            ThreadId(id)
+            ThreadId(NonZeroU64::new(id).unwrap())
         }
     }
 }
@@ -1484,9 +1485,10 @@ fn _assert_sync_and_send() {
 mod tests {
     use super::Builder;
     use crate::any::Any;
+    use crate::mem;
     use crate::sync::mpsc::{channel, Sender};
     use crate::result;
-    use crate::thread;
+    use crate::thread::{self, ThreadId};
     use crate::time::Duration;
     use crate::u32;
 
@@ -1717,6 +1719,11 @@ mod tests {
     }
 
     #[test]
+    fn test_size_of_option_thread_id() {
+        assert_eq!(mem::size_of::<Option<ThreadId>>(), mem::size_of::<ThreadId>());
+    }
+
+    #[test]
     fn test_thread_id_equal() {
         assert!(thread::current().id() == thread::current().id());
     }