Make tidy::version::Version a [u32; 3]

This commit is contained in:
Alexey Shmalko 2019-05-01 22:15:00 +03:00
parent d54477e974
commit 90d3fa223d
3 changed files with 69 additions and 53 deletions

View file

@ -1,4 +1,4 @@
#![stable(feature = "unix_socket_redox", since = "1.29")]
#![stable(feature = "unix_socket_redox", since = "1.29.0")]
//! Unix-specific networking functionality
@ -27,7 +27,7 @@ use crate::sys::{cvt, fd::FileDesc, syscall};
/// let addr = socket.local_addr().expect("Couldn't get local address");
/// ```
#[derive(Clone)]
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub struct SocketAddr(());
impl SocketAddr {
@ -55,7 +55,7 @@ impl SocketAddr {
/// let addr = socket.local_addr().expect("Couldn't get local address");
/// assert_eq!(addr.as_pathname(), None);
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn as_pathname(&self) -> Option<&Path> {
None
}
@ -83,12 +83,12 @@ impl SocketAddr {
/// let addr = socket.local_addr().expect("Couldn't get local address");
/// assert_eq!(addr.is_unnamed(), true);
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn is_unnamed(&self) -> bool {
false
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl fmt::Debug for SocketAddr {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "SocketAddr")
@ -109,10 +109,10 @@ impl fmt::Debug for SocketAddr {
/// stream.read_to_string(&mut response).unwrap();
/// println!("{}", response);
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub struct UnixStream(FileDesc);
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl fmt::Debug for UnixStream {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut builder = fmt.debug_struct("UnixStream");
@ -143,7 +143,7 @@ impl UnixStream {
/// }
/// };
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
if let Some(s) = path.as_ref().to_str() {
cvt(syscall::open(format!("chan:{}", s), syscall::O_CLOEXEC))
@ -174,7 +174,7 @@ impl UnixStream {
/// }
/// };
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
let server = cvt(syscall::open("chan:", syscall::O_CREAT | syscall::O_CLOEXEC))
.map(FileDesc::new)?;
@ -198,7 +198,7 @@ impl UnixStream {
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn try_clone(&self) -> io::Result<UnixStream> {
self.0.duplicate().map(UnixStream)
}
@ -213,7 +213,7 @@ impl UnixStream {
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// let addr = socket.local_addr().expect("Couldn't get local address");
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn local_addr(&self) -> io::Result<SocketAddr> {
Err(Error::new(ErrorKind::Other, "UnixStream::local_addr unimplemented on redox"))
}
@ -228,7 +228,7 @@ impl UnixStream {
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// let addr = socket.peer_addr().expect("Couldn't get peer address");
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
Err(Error::new(ErrorKind::Other, "UnixStream::peer_addr unimplemented on redox"))
}
@ -267,7 +267,7 @@ impl UnixStream {
/// let err = result.unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn set_read_timeout(&self, _timeout: Option<Duration>) -> io::Result<()> {
Err(Error::new(ErrorKind::Other, "UnixStream::set_read_timeout unimplemented on redox"))
}
@ -306,7 +306,7 @@ impl UnixStream {
/// let err = result.unwrap_err();
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn set_write_timeout(&self, _timeout: Option<Duration>) -> io::Result<()> {
Err(Error::new(ErrorKind::Other, "UnixStream::set_write_timeout unimplemented on redox"))
}
@ -323,7 +323,7 @@ impl UnixStream {
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
/// assert_eq!(socket.read_timeout().unwrap(), Some(Duration::new(1, 0)));
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
Err(Error::new(ErrorKind::Other, "UnixStream::read_timeout unimplemented on redox"))
}
@ -340,7 +340,7 @@ impl UnixStream {
/// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
/// assert_eq!(socket.write_timeout().unwrap(), Some(Duration::new(1, 0)));
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
Err(Error::new(ErrorKind::Other, "UnixStream::write_timeout unimplemented on redox"))
}
@ -355,7 +355,7 @@ impl UnixStream {
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
self.0.set_nonblocking(nonblocking)
}
@ -375,7 +375,7 @@ impl UnixStream {
///
/// # Platform specific
/// On Redox this always returns `None`.
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
Ok(None)
}
@ -397,13 +397,13 @@ impl UnixStream {
/// let socket = UnixStream::connect("/tmp/sock").unwrap();
/// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn shutdown(&self, _how: Shutdown) -> io::Result<()> {
Err(Error::new(ErrorKind::Other, "UnixStream::shutdown unimplemented on redox"))
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl io::Read for UnixStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
io::Read::read(&mut &*self, buf)
@ -415,7 +415,7 @@ impl io::Read for UnixStream {
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl<'a> io::Read for &'a UnixStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
@ -427,7 +427,7 @@ impl<'a> io::Read for &'a UnixStream {
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl io::Write for UnixStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
io::Write::write(&mut &*self, buf)
@ -438,7 +438,7 @@ impl io::Write for UnixStream {
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl<'a> io::Write for &'a UnixStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
@ -449,21 +449,21 @@ impl<'a> io::Write for &'a UnixStream {
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl AsRawFd for UnixStream {
fn as_raw_fd(&self) -> RawFd {
self.0.raw()
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl FromRawFd for UnixStream {
unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
UnixStream(FileDesc::new(fd))
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl IntoRawFd for UnixStream {
fn into_raw_fd(self) -> RawFd {
self.0.into_raw()
@ -498,10 +498,10 @@ impl IntoRawFd for UnixStream {
/// }
/// }
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub struct UnixListener(FileDesc);
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl fmt::Debug for UnixListener {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut builder = fmt.debug_struct("UnixListener");
@ -529,7 +529,7 @@ impl UnixListener {
/// }
/// };
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
if let Some(s) = path.as_ref().to_str() {
cvt(syscall::open(format!("chan:{}", s), syscall::O_CREAT | syscall::O_CLOEXEC))
@ -563,7 +563,7 @@ impl UnixListener {
/// Err(e) => println!("accept function failed: {:?}", e),
/// }
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
self.0.duplicate_path(b"listen").map(|fd| (UnixStream(fd), SocketAddr(())))
}
@ -583,7 +583,7 @@ impl UnixListener {
///
/// let listener_copy = listener.try_clone().expect("try_clone failed");
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn try_clone(&self) -> io::Result<UnixListener> {
self.0.duplicate().map(UnixListener)
}
@ -599,7 +599,7 @@ impl UnixListener {
///
/// let addr = listener.local_addr().expect("Couldn't get local address");
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn local_addr(&self) -> io::Result<SocketAddr> {
Err(Error::new(ErrorKind::Other, "UnixListener::local_addr unimplemented on redox"))
}
@ -615,7 +615,7 @@ impl UnixListener {
///
/// listener.set_nonblocking(true).expect("Couldn't set non blocking");
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
self.0.set_nonblocking(nonblocking)
}
@ -636,7 +636,7 @@ impl UnixListener {
///
/// # Platform specific
/// On Redox this always returns `None`.
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
Ok(None)
}
@ -672,34 +672,34 @@ impl UnixListener {
/// }
/// }
/// ```
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub fn incoming<'a>(&'a self) -> Incoming<'a> {
Incoming { listener: self }
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl AsRawFd for UnixListener {
fn as_raw_fd(&self) -> RawFd {
self.0.raw()
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl FromRawFd for UnixListener {
unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
UnixListener(FileDesc::new(fd))
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl IntoRawFd for UnixListener {
fn into_raw_fd(self) -> RawFd {
self.0.into_raw()
}
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl<'a> IntoIterator for &'a UnixListener {
type Item = io::Result<UnixStream>;
type IntoIter = Incoming<'a>;
@ -740,12 +740,12 @@ impl<'a> IntoIterator for &'a UnixListener {
/// }
/// ```
#[derive(Debug)]
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
pub struct Incoming<'a> {
listener: &'a UnixListener,
}
#[stable(feature = "unix_socket_redox", since = "1.29")]
#[stable(feature = "unix_socket_redox", since = "1.29.0")]
impl<'a> Iterator for Incoming<'a> {
type Item = io::Result<UnixStream>;

View file

@ -249,7 +249,7 @@ pub fn collect_lang_features(base_src_path: &Path, bad: &mut bool) -> Features {
Err(err) => {
tidy_error!(
bad,
"libsyntax/feature_gate.rs:{}: failed to parse since: {} ({})",
"libsyntax/feature_gate.rs:{}: failed to parse since: {} ({:?})",
line_number,
since_str,
err,

View file

@ -1,10 +1,11 @@
use std::str::FromStr;
use std::num::ParseIntError;
use std::fmt;
use std::convert::TryInto;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version {
parts: Vec<u32>,
parts: [u32; 3],
}
impl fmt::Display for Version {
@ -14,12 +15,27 @@ impl fmt::Display for Version {
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum ParseVersionError {
ParseIntError(ParseIntError),
// core::array::TryFromSlice is not exported from std, so we invent our own variant
WrongNumberOfParts
}
impl From<ParseIntError> for ParseVersionError {
fn from(err: ParseIntError) -> Self {
ParseVersionError::ParseIntError(err)
}
}
impl FromStr for Version {
type Err = ParseIntError;
type Err = ParseVersionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts = s.split('.').map(|part| part.parse()).collect::<Result<_, _>>()?;
Ok(Version { parts })
let parts: Vec<_> = s.split('.').map(|part| part.parse()).collect::<Result<_, _>>()?;
Ok(Self {
parts: parts.as_slice().try_into() .or(Err(ParseVersionError::WrongNumberOfParts))?,
})
}
}
@ -33,32 +49,32 @@ mod test {
assert!("hello".parse::<Version>().is_err());
assert!("1.32.hi".parse::<Version>().is_err());
assert!("1.32..1".parse::<Version>().is_err());
assert!("1.32".parse::<Version>().is_err());
assert!("1.32.0.1".parse::<Version>().is_err());
}
#[test]
fn test_try_from_single() {
assert_eq!("1.32.0".parse(), Ok(Version { parts: vec![1, 32, 0] }));
assert_eq!("1.0.0".parse(), Ok(Version { parts: vec![1, 0, 0] }));
assert_eq!("1.32.0".parse(), Ok(Version { parts: [1, 32, 0] }));
assert_eq!("1.0.0".parse(), Ok(Version { parts: [1, 0, 0] }));
}
#[test]
fn test_compare() {
let v_1_0_0 = "1.0.0".parse::<Version>().unwrap();
let v_1_32 = "1.32".parse::<Version>().unwrap();
let v_1_32_0 = "1.32.0".parse::<Version>().unwrap();
let v_1_32_1 = "1.32.1".parse::<Version>().unwrap();
assert!(v_1_0_0 < v_1_32_1);
assert!(v_1_0_0 < v_1_32);
assert!(v_1_32 < v_1_32_1);
assert!(v_1_0_0 < v_1_32_0);
assert!(v_1_32_0 < v_1_32_1);
}
#[test]
fn test_to_string() {
let v_1_0_0 = "1.0.0".parse::<Version>().unwrap();
let v_1_32 = "1.32".parse::<Version>().unwrap();
let v_1_32_1 = "1.32.1".parse::<Version>().unwrap();
assert_eq!(v_1_0_0.to_string(), "1.0.0");
assert_eq!(v_1_32.to_string(), "1.32");
assert_eq!(v_1_32_1.to_string(), "1.32.1");
assert_eq!(format!("{:<8}", v_1_32_1), "1.32.1 ");
assert_eq!(format!("{:>8}", v_1_32_1), " 1.32.1");