aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src
diff options
context:
space:
mode:
authorRonald Kinard <[email protected]>2017-01-28 23:12:35 -0600
committerGitHub <[email protected]>2017-01-28 23:12:35 -0600
commit20a7da8cfae69cbd51b26203bca036a700ba79de (patch)
tree9677e765df77c7a9d11bcaf21c40cb33a4c6bd08 /ctr-std/src
parentUpdate AUTHORS.md (diff)
parentctru-rs: impl Seek for File (diff)
downloadctru-rs-20a7da8cfae69cbd51b26203bca036a700ba79de.tar.xz
ctru-rs-20a7da8cfae69cbd51b26203bca036a700ba79de.zip
Merge pull request #15 from FenrirWolf/std
Standard library support, for reals this time.
Diffstat (limited to 'ctr-std/src')
-rw-r--r--ctr-std/src/ascii.rs573
-rw-r--r--ctr-std/src/error.rs497
-rw-r--r--ctr-std/src/f32.rs1871
-rw-r--r--ctr-std/src/f64.rs1756
-rw-r--r--ctr-std/src/ffi/c_str.rs849
-rw-r--r--ctr-std/src/ffi/mod.rs24
-rw-r--r--ctr-std/src/ffi/os_str.rs629
-rw-r--r--ctr-std/src/io/buffered.rs1153
-rw-r--r--ctr-std/src/io/cursor.rs600
-rw-r--r--ctr-std/src/io/error.rs594
-rw-r--r--ctr-std/src/io/impls.rs296
-rw-r--r--ctr-std/src/io/mod.rs1990
-rw-r--r--ctr-std/src/io/prelude.rs24
-rw-r--r--ctr-std/src/io/print.rs33
-rw-r--r--ctr-std/src/io/util.rs215
-rw-r--r--ctr-std/src/lib.rs174
-rw-r--r--ctr-std/src/macros.rs481
-rw-r--r--ctr-std/src/memchr.rs143
-rw-r--r--ctr-std/src/num.rs293
-rw-r--r--ctr-std/src/os/mod.rs17
-rw-r--r--ctr-std/src/panicking.rs59
-rw-r--r--ctr-std/src/path.rs3561
-rw-r--r--ctr-std/src/prelude/mod.rs146
-rw-r--r--ctr-std/src/prelude/v1.rs53
-rw-r--r--ctr-std/src/rt.rs36
-rw-r--r--ctr-std/src/sync/mod.rs29
-rw-r--r--ctr-std/src/sys/mod.rs37
-rw-r--r--ctr-std/src/sys/unix/ext/ffi.rs61
-rw-r--r--ctr-std/src/sys/unix/ext/mod.rs41
-rw-r--r--ctr-std/src/sys/unix/memchr.rs51
-rw-r--r--ctr-std/src/sys/unix/mod.rs50
-rw-r--r--ctr-std/src/sys/unix/os.rs123
-rw-r--r--ctr-std/src/sys/unix/os_str.rs119
-rw-r--r--ctr-std/src/sys/unix/path.rs29
-rw-r--r--ctr-std/src/sys_common/io.rs179
-rw-r--r--ctr-std/src/sys_common/mod.rs76
36 files changed, 16862 insertions, 0 deletions
diff --git a/ctr-std/src/ascii.rs b/ctr-std/src/ascii.rs
new file mode 100644
index 0000000..a063b85
--- /dev/null
+++ b/ctr-std/src/ascii.rs
@@ -0,0 +1,573 @@
+// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Operations on ASCII strings and characters.
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+use mem;
+use ops::Range;
+use iter::FusedIterator;
+
+/// Extension methods for ASCII-subset only operations on string slices.
+///
+/// Be aware that operations on seemingly non-ASCII characters can sometimes
+/// have unexpected results. Consider this example:
+///
+/// ```
+/// use std::ascii::AsciiExt;
+///
+/// assert_eq!("café".to_ascii_uppercase(), "CAFÉ");
+/// assert_eq!("café".to_ascii_uppercase(), "CAFé");
+/// ```
+///
+/// In the first example, the lowercased string is represented `"cafe\u{301}"`
+/// (the last character is an acute accent [combining character]). Unlike the
+/// other characters in the string, the combining character will not get mapped
+/// to an uppercase variant, resulting in `"CAFE\u{301}"`. In the second
+/// example, the lowercased string is represented `"caf\u{e9}"` (the last
+/// character is a single Unicode character representing an 'e' with an acute
+/// accent). Since the last character is defined outside the scope of ASCII,
+/// it will not get mapped to an uppercase variant, resulting in `"CAF\u{e9}"`.
+///
+/// [combining character]: https://en.wikipedia.org/wiki/Combining_character
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait AsciiExt {
+ /// Container type for copied ASCII characters.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ type Owned;
+
+ /// Checks if the value is within the ASCII range.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ascii::AsciiExt;
+ ///
+ /// let ascii = 'a';
+ /// let utf8 = '❤';
+ ///
+ /// assert!(ascii.is_ascii());
+ /// assert!(!utf8.is_ascii());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn is_ascii(&self) -> bool;
+
+ /// Makes a copy of the string in ASCII upper case.
+ ///
+ /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
+ /// but non-ASCII letters are unchanged.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ascii::AsciiExt;
+ ///
+ /// let ascii = 'a';
+ /// let utf8 = '❤';
+ ///
+ /// assert_eq!('A', ascii.to_ascii_uppercase());
+ /// assert_eq!('❤', utf8.to_ascii_uppercase());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn to_ascii_uppercase(&self) -> Self::Owned;
+
+ /// Makes a copy of the string in ASCII lower case.
+ ///
+ /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
+ /// but non-ASCII letters are unchanged.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ascii::AsciiExt;
+ ///
+ /// let ascii = 'A';
+ /// let utf8 = '❤';
+ ///
+ /// assert_eq!('a', ascii.to_ascii_lowercase());
+ /// assert_eq!('❤', utf8.to_ascii_lowercase());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn to_ascii_lowercase(&self) -> Self::Owned;
+
+ /// Checks that two strings are an ASCII case-insensitive match.
+ ///
+ /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
+ /// but without allocating and copying temporary strings.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ascii::AsciiExt;
+ ///
+ /// let ascii1 = 'A';
+ /// let ascii2 = 'a';
+ /// let ascii3 = 'A';
+ /// let ascii4 = 'z';
+ ///
+ /// assert!(ascii1.eq_ignore_ascii_case(&ascii2));
+ /// assert!(ascii1.eq_ignore_ascii_case(&ascii3));
+ /// assert!(!ascii1.eq_ignore_ascii_case(&ascii4));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn eq_ignore_ascii_case(&self, other: &Self) -> bool;
+
+ /// Converts this type to its ASCII upper case equivalent in-place.
+ ///
+ /// See `to_ascii_uppercase` for more information.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ascii::AsciiExt;
+ ///
+ /// let mut ascii = 'a';
+ ///
+ /// ascii.make_ascii_uppercase();
+ ///
+ /// assert_eq!('A', ascii);
+ /// ```
+ #[stable(feature = "ascii", since = "1.9.0")]
+ fn make_ascii_uppercase(&mut self);
+
+ /// Converts this type to its ASCII lower case equivalent in-place.
+ ///
+ /// See `to_ascii_lowercase` for more information.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ascii::AsciiExt;
+ ///
+ /// let mut ascii = 'A';
+ ///
+ /// ascii.make_ascii_lowercase();
+ ///
+ /// assert_eq!('a', ascii);
+ /// ```
+ #[stable(feature = "ascii", since = "1.9.0")]
+ fn make_ascii_lowercase(&mut self);
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsciiExt for str {
+ type Owned = String;
+
+ #[inline]
+ fn is_ascii(&self) -> bool {
+ self.bytes().all(|b| b.is_ascii())
+ }
+
+ #[inline]
+ fn to_ascii_uppercase(&self) -> String {
+ let mut bytes = self.as_bytes().to_vec();
+ bytes.make_ascii_uppercase();
+ // make_ascii_uppercase() preserves the UTF-8 invariant.
+ unsafe { String::from_utf8_unchecked(bytes) }
+ }
+
+ #[inline]
+ fn to_ascii_lowercase(&self) -> String {
+ let mut bytes = self.as_bytes().to_vec();
+ bytes.make_ascii_lowercase();
+ // make_ascii_uppercase() preserves the UTF-8 invariant.
+ unsafe { String::from_utf8_unchecked(bytes) }
+ }
+
+ #[inline]
+ fn eq_ignore_ascii_case(&self, other: &str) -> bool {
+ self.as_bytes().eq_ignore_ascii_case(other.as_bytes())
+ }
+
+ fn make_ascii_uppercase(&mut self) {
+ let me: &mut [u8] = unsafe { mem::transmute(self) };
+ me.make_ascii_uppercase()
+ }
+
+ fn make_ascii_lowercase(&mut self) {
+ let me: &mut [u8] = unsafe { mem::transmute(self) };
+ me.make_ascii_lowercase()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsciiExt for [u8] {
+ type Owned = Vec<u8>;
+ #[inline]
+ fn is_ascii(&self) -> bool {
+ self.iter().all(|b| b.is_ascii())
+ }
+
+ #[inline]
+ fn to_ascii_uppercase(&self) -> Vec<u8> {
+ let mut me = self.to_vec();
+ me.make_ascii_uppercase();
+ return me
+ }
+
+ #[inline]
+ fn to_ascii_lowercase(&self) -> Vec<u8> {
+ let mut me = self.to_vec();
+ me.make_ascii_lowercase();
+ return me
+ }
+
+ #[inline]
+ fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
+ self.len() == other.len() &&
+ self.iter().zip(other).all(|(a, b)| {
+ a.eq_ignore_ascii_case(b)
+ })
+ }
+
+ fn make_ascii_uppercase(&mut self) {
+ for byte in self {
+ byte.make_ascii_uppercase();
+ }
+ }
+
+ fn make_ascii_lowercase(&mut self) {
+ for byte in self {
+ byte.make_ascii_lowercase();
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsciiExt for u8 {
+ type Owned = u8;
+ #[inline]
+ fn is_ascii(&self) -> bool { *self & 128 == 0 }
+ #[inline]
+ fn to_ascii_uppercase(&self) -> u8 { ASCII_UPPERCASE_MAP[*self as usize] }
+ #[inline]
+ fn to_ascii_lowercase(&self) -> u8 { ASCII_LOWERCASE_MAP[*self as usize] }
+ #[inline]
+ fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
+ self.to_ascii_lowercase() == other.to_ascii_lowercase()
+ }
+ #[inline]
+ fn make_ascii_uppercase(&mut self) { *self = self.to_ascii_uppercase(); }
+ #[inline]
+ fn make_ascii_lowercase(&mut self) { *self = self.to_ascii_lowercase(); }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsciiExt for char {
+ type Owned = char;
+ #[inline]
+ fn is_ascii(&self) -> bool {
+ *self as u32 <= 0x7F
+ }
+
+ #[inline]
+ fn to_ascii_uppercase(&self) -> char {
+ if self.is_ascii() {
+ (*self as u8).to_ascii_uppercase() as char
+ } else {
+ *self
+ }
+ }
+
+ #[inline]
+ fn to_ascii_lowercase(&self) -> char {
+ if self.is_ascii() {
+ (*self as u8).to_ascii_lowercase() as char
+ } else {
+ *self
+ }
+ }
+
+ #[inline]
+ fn eq_ignore_ascii_case(&self, other: &char) -> bool {
+ self.to_ascii_lowercase() == other.to_ascii_lowercase()
+ }
+
+ #[inline]
+ fn make_ascii_uppercase(&mut self) { *self = self.to_ascii_uppercase(); }
+ #[inline]
+ fn make_ascii_lowercase(&mut self) { *self = self.to_ascii_lowercase(); }
+}
+
+/// An iterator over the escaped version of a byte, constructed via
+/// `std::ascii::escape_default`.
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct EscapeDefault {
+ range: Range<usize>,
+ data: [u8; 4],
+}
+
+/// Returns an iterator that produces an escaped version of a `u8`.
+///
+/// The default is chosen with a bias toward producing literals that are
+/// legal in a variety of languages, including C++11 and similar C-family
+/// languages. The exact rules are:
+///
+/// - Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
+/// - Single-quote, double-quote and backslash chars are backslash-escaped.
+/// - Any other chars in the range [0x20,0x7e] are not escaped.
+/// - Any other chars are given hex escapes of the form '\xNN'.
+/// - Unicode escapes are never generated by this function.
+///
+/// # Examples
+///
+/// ```
+/// use std::ascii;
+///
+/// let escaped = ascii::escape_default(b'0').next().unwrap();
+/// assert_eq!(b'0', escaped);
+///
+/// let mut escaped = ascii::escape_default(b'\t');
+///
+/// assert_eq!(b'\\', escaped.next().unwrap());
+/// assert_eq!(b't', escaped.next().unwrap());
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn escape_default(c: u8) -> EscapeDefault {
+ let (data, len) = match c {
+ b'\t' => ([b'\\', b't', 0, 0], 2),
+ b'\r' => ([b'\\', b'r', 0, 0], 2),
+ b'\n' => ([b'\\', b'n', 0, 0], 2),
+ b'\\' => ([b'\\', b'\\', 0, 0], 2),
+ b'\'' => ([b'\\', b'\'', 0, 0], 2),
+ b'"' => ([b'\\', b'"', 0, 0], 2),
+ b'\x20' ... b'\x7e' => ([c, 0, 0, 0], 1),
+ _ => ([b'\\', b'x', hexify(c >> 4), hexify(c & 0xf)], 4),
+ };
+
+ return EscapeDefault { range: (0.. len), data: data };
+
+ fn hexify(b: u8) -> u8 {
+ match b {
+ 0 ... 9 => b'0' + b,
+ _ => b'a' + b - 10,
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Iterator for EscapeDefault {
+ type Item = u8;
+ fn next(&mut self) -> Option<u8> { self.range.next().map(|i| self.data[i]) }
+ fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl DoubleEndedIterator for EscapeDefault {
+ fn next_back(&mut self) -> Option<u8> {
+ self.range.next_back().map(|i| self.data[i])
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl ExactSizeIterator for EscapeDefault {}
+#[unstable(feature = "fused", issue = "35602")]
+impl FusedIterator for EscapeDefault {}
+
+
+static ASCII_LOWERCASE_MAP: [u8; 256] = [
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ b' ', b'!', b'"', b'#', b'$', b'%', b'&', b'\'',
+ b'(', b')', b'*', b'+', b',', b'-', b'.', b'/',
+ b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7',
+ b'8', b'9', b':', b';', b'<', b'=', b'>', b'?',
+ b'@',
+
+ b'a', b'b', b'c', b'd', b'e', b'f', b'g',
+ b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o',
+ b'p', b'q', b'r', b's', b't', b'u', b'v', b'w',
+ b'x', b'y', b'z',
+
+ b'[', b'\\', b']', b'^', b'_',
+ b'`', b'a', b'b', b'c', b'd', b'e', b'f', b'g',
+ b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o',
+ b'p', b'q', b'r', b's', b't', b'u', b'v', b'w',
+ b'x', b'y', b'z', b'{', b'|', b'}', b'~', 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
+ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
+ 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
+ 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
+ 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
+ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
+ 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
+ 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
+ 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
+];
+
+static ASCII_UPPERCASE_MAP: [u8; 256] = [
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ b' ', b'!', b'"', b'#', b'$', b'%', b'&', b'\'',
+ b'(', b')', b'*', b'+', b',', b'-', b'.', b'/',
+ b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7',
+ b'8', b'9', b':', b';', b'<', b'=', b'>', b'?',
+ b'@', b'A', b'B', b'C', b'D', b'E', b'F', b'G',
+ b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O',
+ b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W',
+ b'X', b'Y', b'Z', b'[', b'\\', b']', b'^', b'_',
+ b'`',
+
+ b'A', b'B', b'C', b'D', b'E', b'F', b'G',
+ b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O',
+ b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W',
+ b'X', b'Y', b'Z',
+
+ b'{', b'|', b'}', b'~', 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
+ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
+ 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
+ 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
+ 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
+ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
+ 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
+ 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
+ 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
+];
+
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use char::from_u32;
+
+ #[test]
+ fn test_is_ascii() {
+ assert!(b"".is_ascii());
+ assert!(b"banana\0\x7F".is_ascii());
+ assert!(b"banana\0\x7F".iter().all(|b| b.is_ascii()));
+ assert!(!b"Vi\xe1\xbb\x87t Nam".is_ascii());
+ assert!(!b"Vi\xe1\xbb\x87t Nam".iter().all(|b| b.is_ascii()));
+ assert!(!b"\xe1\xbb\x87".iter().any(|b| b.is_ascii()));
+
+ assert!("".is_ascii());
+ assert!("banana\0\u{7F}".is_ascii());
+ assert!("banana\0\u{7F}".chars().all(|c| c.is_ascii()));
+ assert!(!"ประเทศไทย中华Việt Nam".chars().all(|c| c.is_ascii()));
+ assert!(!"ประเทศไทย中华ệ ".chars().any(|c| c.is_ascii()));
+ }
+
+ #[test]
+ fn test_to_ascii_uppercase() {
+ assert_eq!("url()URL()uRl()ürl".to_ascii_uppercase(), "URL()URL()URL()üRL");
+ assert_eq!("hıKß".to_ascii_uppercase(), "HıKß");
+
+ for i in 0..501 {
+ let upper = if 'a' as u32 <= i && i <= 'z' as u32 { i + 'A' as u32 - 'a' as u32 }
+ else { i };
+ assert_eq!((from_u32(i).unwrap()).to_string().to_ascii_uppercase(),
+ (from_u32(upper).unwrap()).to_string());
+ }
+ }
+
+ #[test]
+ fn test_to_ascii_lowercase() {
+ assert_eq!("url()URL()uRl()Ürl".to_ascii_lowercase(), "url()url()url()Ürl");
+ // Dotted capital I, Kelvin sign, Sharp S.
+ assert_eq!("HİKß".to_ascii_lowercase(), "hİKß");
+
+ for i in 0..501 {
+ let lower = if 'A' as u32 <= i && i <= 'Z' as u32 { i + 'a' as u32 - 'A' as u32 }
+ else { i };
+ assert_eq!((from_u32(i).unwrap()).to_string().to_ascii_lowercase(),
+ (from_u32(lower).unwrap()).to_string());
+ }
+ }
+
+ #[test]
+ fn test_make_ascii_lower_case() {
+ macro_rules! test {
+ ($from: expr, $to: expr) => {
+ {
+ let mut x = $from;
+ x.make_ascii_lowercase();
+ assert_eq!(x, $to);
+ }
+ }
+ }
+ test!(b'A', b'a');
+ test!(b'a', b'a');
+ test!(b'!', b'!');
+ test!('A', 'a');
+ test!('À', 'À');
+ test!('a', 'a');
+ test!('!', '!');
+ test!(b"H\xc3\x89".to_vec(), b"h\xc3\x89");
+ test!("HİKß".to_string(), "hİKß");
+ }
+
+
+ #[test]
+ fn test_make_ascii_upper_case() {
+ macro_rules! test {
+ ($from: expr, $to: expr) => {
+ {
+ let mut x = $from;
+ x.make_ascii_uppercase();
+ assert_eq!(x, $to);
+ }
+ }
+ }
+ test!(b'a', b'A');
+ test!(b'A', b'A');
+ test!(b'!', b'!');
+ test!('a', 'A');
+ test!('à', 'à');
+ test!('A', 'A');
+ test!('!', '!');
+ test!(b"h\xc3\xa9".to_vec(), b"H\xc3\xa9");
+ test!("hıKß".to_string(), "HıKß");
+
+ let mut x = "Hello".to_string();
+ x[..3].make_ascii_uppercase(); // Test IndexMut on String.
+ assert_eq!(x, "HELlo")
+ }
+
+ #[test]
+ fn test_eq_ignore_ascii_case() {
+ assert!("url()URL()uRl()Ürl".eq_ignore_ascii_case("url()url()url()Ürl"));
+ assert!(!"Ürl".eq_ignore_ascii_case("ürl"));
+ // Dotted capital I, Kelvin sign, Sharp S.
+ assert!("HİKß".eq_ignore_ascii_case("hİKß"));
+ assert!(!"İ".eq_ignore_ascii_case("i"));
+ assert!(!"K".eq_ignore_ascii_case("k"));
+ assert!(!"ß".eq_ignore_ascii_case("s"));
+
+ for i in 0..501 {
+ let lower = if 'A' as u32 <= i && i <= 'Z' as u32 { i + 'a' as u32 - 'A' as u32 }
+ else { i };
+ assert!((from_u32(i).unwrap()).to_string().eq_ignore_ascii_case(
+ &from_u32(lower).unwrap().to_string()));
+ }
+ }
+
+ #[test]
+ fn inference_works() {
+ let x = "a".to_string();
+ x.eq_ignore_ascii_case("A");
+ }
+}
diff --git a/ctr-std/src/error.rs b/ctr-std/src/error.rs
new file mode 100644
index 0000000..454fa47
--- /dev/null
+++ b/ctr-std/src/error.rs
@@ -0,0 +1,497 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Traits for working with Errors.
+//!
+//! # The `Error` trait
+//!
+//! `Error` is a trait representing the basic expectations for error values,
+//! i.e. values of type `E` in [`Result<T, E>`]. At a minimum, errors must provide
+//! a description, but they may optionally provide additional detail (via
+//! [`Display`]) and cause chain information:
+//!
+//! ```
+//! use std::fmt::Display;
+//!
+//! trait Error: Display {
+//! fn description(&self) -> &str;
+//!
+//! fn cause(&self) -> Option<&Error> { None }
+//! }
+//! ```
+//!
+//! The [`cause`] method is generally used when errors cross "abstraction
+//! boundaries", i.e. when a one module must report an error that is "caused"
+//! by an error from a lower-level module. This setup makes it possible for the
+//! high-level module to provide its own errors that do not commit to any
+//! particular implementation, but also reveal some of its implementation for
+//! debugging via [`cause`] chains.
+//!
+//! [`Result<T, E>`]: ../result/enum.Result.html
+//! [`Display`]: ../fmt/trait.Display.html
+//! [`cause`]: trait.Error.html#method.cause
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+// A note about crates and the facade:
+//
+// Originally, the `Error` trait was defined in libcore, and the impls
+// were scattered about. However, coherence objected to this
+// arrangement, because to create the blanket impls for `Box` required
+// knowing that `&str: !Error`, and we have no means to deal with that
+// sort of conflict just now. Therefore, for the time being, we have
+// moved the `Error` trait into libstd. As we evolve a sol'n to the
+// coherence challenge (e.g., specialization, neg impls, etc) we can
+// reconsider what crate these items belong in.
+
+use any::TypeId;
+use cell;
+use char;
+use fmt::{self, Debug, Display};
+use mem::transmute;
+use num;
+use str;
+use string;
+
+/// Base functionality for all errors in Rust.
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Error: Debug + Display {
+ /// A short description of the error.
+ ///
+ /// The description should only be used for a simple message.
+ /// It should not contain newlines or sentence-ending punctuation,
+ /// to facilitate embedding in larger user-facing strings.
+ /// For showing formatted error messages with more information see
+ /// [`Display`].
+ ///
+ /// [`Display`]: ../fmt/trait.Display.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::error::Error;
+ ///
+ /// match "xc".parse::<u32>() {
+ /// Err(e) => {
+ /// println!("Error: {}", e.description());
+ /// }
+ /// _ => println!("No error"),
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn description(&self) -> &str;
+
+ /// The lower-level cause of this error, if any.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::error::Error;
+ /// use std::fmt;
+ ///
+ /// #[derive(Debug)]
+ /// struct SuperError {
+ /// side: SuperErrorSideKick,
+ /// }
+ ///
+ /// impl fmt::Display for SuperError {
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// write!(f, "SuperError is here!")
+ /// }
+ /// }
+ ///
+ /// impl Error for SuperError {
+ /// fn description(&self) -> &str {
+ /// "I'm the superhero of errors!"
+ /// }
+ ///
+ /// fn cause(&self) -> Option<&Error> {
+ /// Some(&self.side)
+ /// }
+ /// }
+ ///
+ /// #[derive(Debug)]
+ /// struct SuperErrorSideKick;
+ ///
+ /// impl fmt::Display for SuperErrorSideKick {
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// write!(f, "SuperErrorSideKick is here!")
+ /// }
+ /// }
+ ///
+ /// impl Error for SuperErrorSideKick {
+ /// fn description(&self) -> &str {
+ /// "I'm SuperError side kick!"
+ /// }
+ /// }
+ ///
+ /// fn get_super_error() -> Result<(), SuperError> {
+ /// Err(SuperError { side: SuperErrorSideKick })
+ /// }
+ ///
+ /// fn main() {
+ /// match get_super_error() {
+ /// Err(e) => {
+ /// println!("Error: {}", e.description());
+ /// println!("Caused by: {}", e.cause().unwrap());
+ /// }
+ /// _ => println!("No error"),
+ /// }
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn cause(&self) -> Option<&Error> { None }
+
+ /// Get the `TypeId` of `self`
+ #[doc(hidden)]
+ #[unstable(feature = "error_type_id",
+ reason = "unclear whether to commit to this public implementation detail",
+ issue = "27745")]
+ fn type_id(&self) -> TypeId where Self: 'static {
+ TypeId::of::<Self>()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, E: Error + 'a> From<E> for Box<Error + 'a> {
+ fn from(err: E) -> Box<Error + 'a> {
+ Box::new(err)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<Error + Send + Sync + 'a> {
+ fn from(err: E) -> Box<Error + Send + Sync + 'a> {
+ Box::new(err)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl From<String> for Box<Error + Send + Sync> {
+ fn from(err: String) -> Box<Error + Send + Sync> {
+ #[derive(Debug)]
+ struct StringError(String);
+
+ impl Error for StringError {
+ fn description(&self) -> &str { &self.0 }
+ }
+
+ impl Display for StringError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ Display::fmt(&self.0, f)
+ }
+ }
+
+ Box::new(StringError(err))
+ }
+}
+
+#[stable(feature = "string_box_error", since = "1.7.0")]
+impl From<String> for Box<Error> {
+ fn from(str_err: String) -> Box<Error> {
+ let err1: Box<Error + Send + Sync> = From::from(str_err);
+ let err2: Box<Error> = err1;
+ err2
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a> {
+ fn from(err: &'b str) -> Box<Error + Send + Sync + 'a> {
+ From::from(String::from(err))
+ }
+}
+
+#[stable(feature = "string_box_error", since = "1.7.0")]
+impl<'a> From<&'a str> for Box<Error> {
+ fn from(err: &'a str) -> Box<Error> {
+ From::from(String::from(err))
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Error for str::ParseBoolError {
+ fn description(&self) -> &str { "failed to parse bool" }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Error for str::Utf8Error {
+ fn description(&self) -> &str {
+ "invalid utf-8: corrupt contents"
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Error for num::ParseIntError {
+ fn description(&self) -> &str {
+ self.__description()
+ }
+}
+
+#[unstable(feature = "try_from", issue = "33417")]
+impl Error for num::TryFromIntError {
+ fn description(&self) -> &str {
+ self.__description()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Error for num::ParseFloatError {
+ fn description(&self) -> &str {
+ self.__description()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Error for string::FromUtf8Error {
+ fn description(&self) -> &str {
+ "invalid utf-8"
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Error for string::FromUtf16Error {
+ fn description(&self) -> &str {
+ "invalid utf-16"
+ }
+}
+
+#[stable(feature = "str_parse_error2", since = "1.8.0")]
+impl Error for string::ParseError {
+ fn description(&self) -> &str {
+ match *self {}
+ }
+}
+
+#[stable(feature = "decode_utf16", since = "1.9.0")]
+impl Error for char::DecodeUtf16Error {
+ fn description(&self) -> &str {
+ "unpaired surrogate found"
+ }
+}
+
+#[stable(feature = "box_error", since = "1.7.0")]
+impl<T: Error> Error for Box<T> {
+ fn description(&self) -> &str {
+ Error::description(&**self)
+ }
+
+ fn cause(&self) -> Option<&Error> {
+ Error::cause(&**self)
+ }
+}
+
+#[stable(feature = "fmt_error", since = "1.11.0")]
+impl Error for fmt::Error {
+ fn description(&self) -> &str {
+ "an error occurred when formatting an argument"
+ }
+}
+
+#[stable(feature = "try_borrow", since = "1.13.0")]
+impl Error for cell::BorrowError {
+ fn description(&self) -> &str {
+ "already mutably borrowed"
+ }
+}
+
+#[stable(feature = "try_borrow", since = "1.13.0")]
+impl Error for cell::BorrowMutError {
+ fn description(&self) -> &str {
+ "already borrowed"
+ }
+}
+
+#[unstable(feature = "try_from", issue = "33417")]
+impl Error for char::CharTryFromError {
+ fn description(&self) -> &str {
+ "converted integer out of range for `char`"
+ }
+}
+
+// copied from any.rs
+impl Error + 'static {
+ /// Returns true if the boxed type is the same as `T`
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ #[inline]
+ pub fn is<T: Error + 'static>(&self) -> bool {
+ // Get TypeId of the type this function is instantiated with
+ let t = TypeId::of::<T>();
+
+ // Get TypeId of the type in the trait object
+ let boxed = self.type_id();
+
+ // Compare both TypeIds on equality
+ t == boxed
+ }
+
+ /// Returns some reference to the boxed value if it is of type `T`, or
+ /// `None` if it isn't.
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ #[inline]
+ pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
+ if self.is::<T>() {
+ unsafe {
+ Some(&*(self as *const Error as *const T))
+ }
+ } else {
+ None
+ }
+ }
+
+ /// Returns some mutable reference to the boxed value if it is of type `T`, or
+ /// `None` if it isn't.
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ #[inline]
+ pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
+ if self.is::<T>() {
+ unsafe {
+ Some(&mut *(self as *mut Error as *mut T))
+ }
+ } else {
+ None
+ }
+ }
+}
+
+impl Error + 'static + Send {
+ /// Forwards to the method defined on the type `Any`.
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ #[inline]
+ pub fn is<T: Error + 'static>(&self) -> bool {
+ <Error + 'static>::is::<T>(self)
+ }
+
+ /// Forwards to the method defined on the type `Any`.
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ #[inline]
+ pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
+ <Error + 'static>::downcast_ref::<T>(self)
+ }
+
+ /// Forwards to the method defined on the type `Any`.
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ #[inline]
+ pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
+ <Error + 'static>::downcast_mut::<T>(self)
+ }
+}
+
+impl Error + 'static + Send + Sync {
+ /// Forwards to the method defined on the type `Any`.
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ #[inline]
+ pub fn is<T: Error + 'static>(&self) -> bool {
+ <Error + 'static>::is::<T>(self)
+ }
+
+ /// Forwards to the method defined on the type `Any`.
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ #[inline]
+ pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
+ <Error + 'static>::downcast_ref::<T>(self)
+ }
+
+ /// Forwards to the method defined on the type `Any`.
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ #[inline]
+ pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
+ <Error + 'static>::downcast_mut::<T>(self)
+ }
+}
+
+impl Error {
+ #[inline]
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ /// Attempt to downcast the box to a concrete type.
+ pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Error>> {
+ if self.is::<T>() {
+ unsafe {
+ let raw: *mut Error = Box::into_raw(self);
+ Ok(Box::from_raw(raw as *mut T))
+ }
+ } else {
+ Err(self)
+ }
+ }
+}
+
+impl Error + Send {
+ #[inline]
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ /// Attempt to downcast the box to a concrete type.
+ pub fn downcast<T: Error + 'static>(self: Box<Self>)
+ -> Result<Box<T>, Box<Error + Send>> {
+ let err: Box<Error> = self;
+ <Error>::downcast(err).map_err(|s| unsafe {
+ // reapply the Send marker
+ transmute::<Box<Error>, Box<Error + Send>>(s)
+ })
+ }
+}
+
+impl Error + Send + Sync {
+ #[inline]
+ #[stable(feature = "error_downcast", since = "1.3.0")]
+ /// Attempt to downcast the box to a concrete type.
+ pub fn downcast<T: Error + 'static>(self: Box<Self>)
+ -> Result<Box<T>, Box<Self>> {
+ let err: Box<Error> = self;
+ <Error>::downcast(err).map_err(|s| unsafe {
+ // reapply the Send+Sync marker
+ transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
+ })
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::Error;
+ use fmt;
+
+ #[derive(Debug, PartialEq)]
+ struct A;
+ #[derive(Debug, PartialEq)]
+ struct B;
+
+ impl fmt::Display for A {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "A")
+ }
+ }
+ impl fmt::Display for B {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "B")
+ }
+ }
+
+ impl Error for A {
+ fn description(&self) -> &str { "A-desc" }
+ }
+ impl Error for B {
+ fn description(&self) -> &str { "A-desc" }
+ }
+
+ #[test]
+ fn downcasting() {
+ let mut a = A;
+ let mut a = &mut a as &mut (Error + 'static);
+ assert_eq!(a.downcast_ref::<A>(), Some(&A));
+ assert_eq!(a.downcast_ref::<B>(), None);
+ assert_eq!(a.downcast_mut::<A>(), Some(&mut A));
+ assert_eq!(a.downcast_mut::<B>(), None);
+
+ let a: Box<Error> = Box::new(A);
+ match a.downcast::<B>() {
+ Ok(..) => panic!("expected error"),
+ Err(e) => assert_eq!(*e.downcast::<A>().unwrap(), A),
+ }
+ }
+}
diff --git a/ctr-std/src/f32.rs b/ctr-std/src/f32.rs
new file mode 100644
index 0000000..7a676c0
--- /dev/null
+++ b/ctr-std/src/f32.rs
@@ -0,0 +1,1871 @@
+// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! The 32-bit floating point type.
+//!
+//! *[See also the `f32` primitive type](../primitive.f32.html).*
+
+#![stable(feature = "rust1", since = "1.0.0")]
+#![allow(missing_docs)]
+
+#[cfg(not(test))]
+use core::num;
+#[cfg(not(test))]
+use intrinsics;
+#[cfg(not(test))]
+use libc::c_int;
+#[cfg(not(test))]
+use num::FpCategory;
+
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::f32::{MIN, MIN_POSITIVE, MAX};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::f32::consts;
+
+#[allow(dead_code)]
+mod cmath {
+ use libc::{c_float, c_int};
+
+ extern {
+ pub fn cbrtf(n: c_float) -> c_float;
+ pub fn erff(n: c_float) -> c_float;
+ pub fn erfcf(n: c_float) -> c_float;
+ pub fn expm1f(n: c_float) -> c_float;
+ pub fn fdimf(a: c_float, b: c_float) -> c_float;
+ pub fn fmaxf(a: c_float, b: c_float) -> c_float;
+ pub fn fminf(a: c_float, b: c_float) -> c_float;
+ pub fn fmodf(a: c_float, b: c_float) -> c_float;
+ pub fn ilogbf(n: c_float) -> c_int;
+ pub fn logbf(n: c_float) -> c_float;
+ pub fn log1pf(n: c_float) -> c_float;
+ pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
+ pub fn nextafterf(x: c_float, y: c_float) -> c_float;
+ pub fn tgammaf(n: c_float) -> c_float;
+
+ #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgammaf_r")]
+ pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
+ #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypotf")]
+ pub fn hypotf(x: c_float, y: c_float) -> c_float;
+ }
+
+ // See the comments in the `floor` function for why MSVC is special
+ // here.
+ #[cfg(not(target_env = "msvc"))]
+ extern {
+ pub fn acosf(n: c_float) -> c_float;
+ pub fn asinf(n: c_float) -> c_float;
+ pub fn atan2f(a: c_float, b: c_float) -> c_float;
+ pub fn atanf(n: c_float) -> c_float;
+ pub fn coshf(n: c_float) -> c_float;
+ pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
+ pub fn ldexpf(x: c_float, n: c_int) -> c_float;
+ pub fn sinhf(n: c_float) -> c_float;
+ pub fn tanf(n: c_float) -> c_float;
+ pub fn tanhf(n: c_float) -> c_float;
+ }
+
+ #[cfg(target_env = "msvc")]
+ pub use self::shims::*;
+ #[cfg(target_env = "msvc")]
+ mod shims {
+ use libc::{c_float, c_int};
+
+ #[inline]
+ pub unsafe fn acosf(n: c_float) -> c_float {
+ f64::acos(n as f64) as c_float
+ }
+
+ #[inline]
+ pub unsafe fn asinf(n: c_float) -> c_float {
+ f64::asin(n as f64) as c_float
+ }
+
+ #[inline]
+ pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float {
+ f64::atan2(n as f64, b as f64) as c_float
+ }
+
+ #[inline]
+ pub unsafe fn atanf(n: c_float) -> c_float {
+ f64::atan(n as f64) as c_float
+ }
+
+ #[inline]
+ pub unsafe fn coshf(n: c_float) -> c_float {
+ f64::cosh(n as f64) as c_float
+ }
+
+ #[inline]
+ #[allow(deprecated)]
+ pub unsafe fn frexpf(x: c_float, value: &mut c_int) -> c_float {
+ let (a, b) = f64::frexp(x as f64);
+ *value = b as c_int;
+ a as c_float
+ }
+
+ #[inline]
+ #[allow(deprecated)]
+ pub unsafe fn ldexpf(x: c_float, n: c_int) -> c_float {
+ f64::ldexp(x as f64, n as isize) as c_float
+ }
+
+ #[inline]
+ pub unsafe fn sinhf(n: c_float) -> c_float {
+ f64::sinh(n as f64) as c_float
+ }
+
+ #[inline]
+ pub unsafe fn tanf(n: c_float) -> c_float {
+ f64::tan(n as f64) as c_float
+ }
+
+ #[inline]
+ pub unsafe fn tanhf(n: c_float) -> c_float {
+ f64::tanh(n as f64) as c_float
+ }
+ }
+}
+
+#[cfg(not(test))]
+#[lang = "f32"]
+impl f32 {
+ /// Returns `true` if this value is `NaN` and false otherwise.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let nan = f32::NAN;
+ /// let f = 7.0_f32;
+ ///
+ /// assert!(nan.is_nan());
+ /// assert!(!f.is_nan());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
+
+ /// Returns `true` if this value is positive infinity or negative infinity and
+ /// false otherwise.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let f = 7.0f32;
+ /// let inf = f32::INFINITY;
+ /// let neg_inf = f32::NEG_INFINITY;
+ /// let nan = f32::NAN;
+ ///
+ /// assert!(!f.is_infinite());
+ /// assert!(!nan.is_infinite());
+ ///
+ /// assert!(inf.is_infinite());
+ /// assert!(neg_inf.is_infinite());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
+
+ /// Returns `true` if this number is neither infinite nor `NaN`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let f = 7.0f32;
+ /// let inf = f32::INFINITY;
+ /// let neg_inf = f32::NEG_INFINITY;
+ /// let nan = f32::NAN;
+ ///
+ /// assert!(f.is_finite());
+ ///
+ /// assert!(!nan.is_finite());
+ /// assert!(!inf.is_finite());
+ /// assert!(!neg_inf.is_finite());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
+
+ /// Returns `true` if the number is neither zero, infinite,
+ /// [subnormal][subnormal], or `NaN`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
+ /// let max = f32::MAX;
+ /// let lower_than_min = 1.0e-40_f32;
+ /// let zero = 0.0_f32;
+ ///
+ /// assert!(min.is_normal());
+ /// assert!(max.is_normal());
+ ///
+ /// assert!(!zero.is_normal());
+ /// assert!(!f32::NAN.is_normal());
+ /// assert!(!f32::INFINITY.is_normal());
+ /// // Values between `0` and `min` are Subnormal.
+ /// assert!(!lower_than_min.is_normal());
+ /// ```
+ /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
+
+ /// Returns the floating point category of the number. If only one property
+ /// is going to be tested, it is generally faster to use the specific
+ /// predicate instead.
+ ///
+ /// ```
+ /// use std::num::FpCategory;
+ /// use std::f32;
+ ///
+ /// let num = 12.4_f32;
+ /// let inf = f32::INFINITY;
+ ///
+ /// assert_eq!(num.classify(), FpCategory::Normal);
+ /// assert_eq!(inf.classify(), FpCategory::Infinite);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn classify(self) -> FpCategory { num::Float::classify(self) }
+
+ /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
+ /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
+ /// The floating point encoding is documented in the [Reference][floating-point].
+ ///
+ /// ```
+ /// #![feature(float_extras)]
+ ///
+ /// use std::f32;
+ ///
+ /// let num = 2.0f32;
+ ///
+ /// // (8388608, -22, 1)
+ /// let (mantissa, exponent, sign) = num.integer_decode();
+ /// let sign_f = sign as f32;
+ /// let mantissa_f = mantissa as f32;
+ /// let exponent_f = num.powf(exponent as f32);
+ ///
+ /// // 1 * 8388608 * 2^(-22) == 2
+ /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ /// [floating-point]: ../reference.html#machine-types
+ #[unstable(feature = "float_extras", reason = "signature is undecided",
+ issue = "27752")]
+ #[rustc_deprecated(since = "1.11.0",
+ reason = "never really came to fruition and easily \
+ implementable outside the standard library")]
+ #[inline]
+ #[allow(deprecated)]
+ pub fn integer_decode(self) -> (u64, i16, i8) {
+ num::Float::integer_decode(self)
+ }
+
+ /// Returns the largest integer less than or equal to a number.
+ ///
+ /// ```
+ /// let f = 3.99_f32;
+ /// let g = 3.0_f32;
+ ///
+ /// assert_eq!(f.floor(), 3.0);
+ /// assert_eq!(g.floor(), 3.0);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn floor(self) -> f32 {
+ // On MSVC LLVM will lower many math intrinsics to a call to the
+ // corresponding function. On MSVC, however, many of these functions
+ // aren't actually available as symbols to call, but rather they are all
+ // `static inline` functions in header files. This means that from a C
+ // perspective it's "compatible", but not so much from an ABI
+ // perspective (which we're worried about).
+ //
+ // The inline header functions always just cast to a f64 and do their
+ // operation, so we do that here as well, but only for MSVC targets.
+ //
+ // Note that there are many MSVC-specific float operations which
+ // redirect to this comment, so `floorf` is just one case of a missing
+ // function on MSVC, but there are many others elsewhere.
+ #[cfg(target_env = "msvc")]
+ return (self as f64).floor() as f32;
+ #[cfg(not(target_env = "msvc"))]
+ return unsafe { intrinsics::floorf32(self) };
+ }
+
+ /// Returns the smallest integer greater than or equal to a number.
+ ///
+ /// ```
+ /// let f = 3.01_f32;
+ /// let g = 4.0_f32;
+ ///
+ /// assert_eq!(f.ceil(), 4.0);
+ /// assert_eq!(g.ceil(), 4.0);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn ceil(self) -> f32 {
+ // see notes above in `floor`
+ #[cfg(target_env = "msvc")]
+ return (self as f64).ceil() as f32;
+ #[cfg(not(target_env = "msvc"))]
+ return unsafe { intrinsics::ceilf32(self) };
+ }
+
+ /// Returns the nearest integer to a number. Round half-way cases away from
+ /// `0.0`.
+ ///
+ /// ```
+ /// let f = 3.3_f32;
+ /// let g = -3.3_f32;
+ ///
+ /// assert_eq!(f.round(), 3.0);
+ /// assert_eq!(g.round(), -3.0);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn round(self) -> f32 {
+ unsafe { intrinsics::roundf32(self) }
+ }
+
+ /// Returns the integer part of a number.
+ ///
+ /// ```
+ /// let f = 3.3_f32;
+ /// let g = -3.7_f32;
+ ///
+ /// assert_eq!(f.trunc(), 3.0);
+ /// assert_eq!(g.trunc(), -3.0);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn trunc(self) -> f32 {
+ unsafe { intrinsics::truncf32(self) }
+ }
+
+ /// Returns the fractional part of a number.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 3.5_f32;
+ /// let y = -3.5_f32;
+ /// let abs_difference_x = (x.fract() - 0.5).abs();
+ /// let abs_difference_y = (y.fract() - (-0.5)).abs();
+ ///
+ /// assert!(abs_difference_x <= f32::EPSILON);
+ /// assert!(abs_difference_y <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn fract(self) -> f32 { self - self.trunc() }
+
+ /// Computes the absolute value of `self`. Returns `NAN` if the
+ /// number is `NAN`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 3.5_f32;
+ /// let y = -3.5_f32;
+ ///
+ /// let abs_difference_x = (x.abs() - x).abs();
+ /// let abs_difference_y = (y.abs() - (-y)).abs();
+ ///
+ /// assert!(abs_difference_x <= f32::EPSILON);
+ /// assert!(abs_difference_y <= f32::EPSILON);
+ ///
+ /// assert!(f32::NAN.abs().is_nan());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn abs(self) -> f32 { num::Float::abs(self) }
+
+ /// Returns a number that represents the sign of `self`.
+ ///
+ /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
+ /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
+ /// - `NAN` if the number is `NAN`
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let f = 3.5_f32;
+ ///
+ /// assert_eq!(f.signum(), 1.0);
+ /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
+ ///
+ /// assert!(f32::NAN.signum().is_nan());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn signum(self) -> f32 { num::Float::signum(self) }
+
+ /// Returns `true` if `self`'s sign bit is positive, including
+ /// `+0.0` and `INFINITY`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let nan = f32::NAN;
+ /// let f = 7.0_f32;
+ /// let g = -7.0_f32;
+ ///
+ /// assert!(f.is_sign_positive());
+ /// assert!(!g.is_sign_positive());
+ /// // Requires both tests to determine if is `NaN`
+ /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
+
+ /// Returns `true` if `self`'s sign is negative, including `-0.0`
+ /// and `NEG_INFINITY`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let nan = f32::NAN;
+ /// let f = 7.0f32;
+ /// let g = -7.0f32;
+ ///
+ /// assert!(!f.is_sign_negative());
+ /// assert!(g.is_sign_negative());
+ /// // Requires both tests to determine if is `NaN`.
+ /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
+
+ /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
+ /// error. This produces a more accurate result with better performance than
+ /// a separate multiplication operation followed by an add.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let m = 10.0_f32;
+ /// let x = 4.0_f32;
+ /// let b = 60.0_f32;
+ ///
+ /// // 100.0
+ /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn mul_add(self, a: f32, b: f32) -> f32 {
+ unsafe { intrinsics::fmaf32(self, a, b) }
+ }
+
+ /// Takes the reciprocal (inverse) of a number, `1/x`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 2.0_f32;
+ /// let abs_difference = (x.recip() - (1.0/x)).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn recip(self) -> f32 { num::Float::recip(self) }
+
+ /// Raises a number to an integer power.
+ ///
+ /// Using this function is generally faster than using `powf`
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 2.0_f32;
+ /// let abs_difference = (x.powi(2) - x*x).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
+
+ /// Raises a number to a floating point power.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 2.0_f32;
+ /// let abs_difference = (x.powf(2.0) - x*x).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn powf(self, n: f32) -> f32 {
+ // see notes above in `floor`
+ #[cfg(target_env = "msvc")]
+ return (self as f64).powf(n as f64) as f32;
+ #[cfg(not(target_env = "msvc"))]
+ return unsafe { intrinsics::powf32(self, n) };
+ }
+
+ /// Takes the square root of a number.
+ ///
+ /// Returns NaN if `self` is a negative number.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let positive = 4.0_f32;
+ /// let negative = -4.0_f32;
+ ///
+ /// let abs_difference = (positive.sqrt() - 2.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// assert!(negative.sqrt().is_nan());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn sqrt(self) -> f32 {
+ if self < 0.0 {
+ NAN
+ } else {
+ unsafe { intrinsics::sqrtf32(self) }
+ }
+ }
+
+ /// Returns `e^(self)`, (the exponential function).
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let one = 1.0f32;
+ /// // e^1
+ /// let e = one.exp();
+ ///
+ /// // ln(e) - 1 == 0
+ /// let abs_difference = (e.ln() - 1.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn exp(self) -> f32 {
+ // see notes above in `floor`
+ #[cfg(target_env = "msvc")]
+ return (self as f64).exp() as f32;
+ #[cfg(not(target_env = "msvc"))]
+ return unsafe { intrinsics::expf32(self) };
+ }
+
+ /// Returns `2^(self)`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let f = 2.0f32;
+ ///
+ /// // 2^2 - 4 == 0
+ /// let abs_difference = (f.exp2() - 4.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn exp2(self) -> f32 {
+ unsafe { intrinsics::exp2f32(self) }
+ }
+
+ /// Returns the natural logarithm of the number.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let one = 1.0f32;
+ /// // e^1
+ /// let e = one.exp();
+ ///
+ /// // ln(e) - 1 == 0
+ /// let abs_difference = (e.ln() - 1.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn ln(self) -> f32 {
+ // see notes above in `floor`
+ #[cfg(target_env = "msvc")]
+ return (self as f64).ln() as f32;
+ #[cfg(not(target_env = "msvc"))]
+ return unsafe { intrinsics::logf32(self) };
+ }
+
+ /// Returns the logarithm of the number with respect to an arbitrary base.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let ten = 10.0f32;
+ /// let two = 2.0f32;
+ ///
+ /// // log10(10) - 1 == 0
+ /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
+ ///
+ /// // log2(2) - 1 == 0
+ /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
+ ///
+ /// assert!(abs_difference_10 <= f32::EPSILON);
+ /// assert!(abs_difference_2 <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
+
+ /// Returns the base 2 logarithm of the number.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let two = 2.0f32;
+ ///
+ /// // log2(2) - 1 == 0
+ /// let abs_difference = (two.log2() - 1.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn log2(self) -> f32 {
+ #[cfg(target_os = "android")]
+ return ::sys::android::log2f32(self);
+ #[cfg(not(target_os = "android"))]
+ return unsafe { intrinsics::log2f32(self) };
+ }
+
+ /// Returns the base 10 logarithm of the number.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let ten = 10.0f32;
+ ///
+ /// // log10(10) - 1 == 0
+ /// let abs_difference = (ten.log10() - 1.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn log10(self) -> f32 {
+ // see notes above in `floor`
+ #[cfg(target_env = "msvc")]
+ return (self as f64).log10() as f32;
+ #[cfg(not(target_env = "msvc"))]
+ return unsafe { intrinsics::log10f32(self) };
+ }
+
+ /// Converts radians to degrees.
+ ///
+ /// ```
+ /// use std::f32::{self, consts};
+ ///
+ /// let angle = consts::PI;
+ ///
+ /// let abs_difference = (angle.to_degrees() - 180.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
+ #[inline]
+ pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
+
+ /// Converts degrees to radians.
+ ///
+ /// ```
+ /// use std::f32::{self, consts};
+ ///
+ /// let angle = 180.0f32;
+ ///
+ /// let abs_difference = (angle.to_radians() - consts::PI).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
+ #[inline]
+ pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
+
+ /// Constructs a floating point number of `x*2^exp`.
+ ///
+ /// ```
+ /// #![feature(float_extras)]
+ ///
+ /// use std::f32;
+ /// // 3*2^2 - 12 == 0
+ /// let abs_difference = (f32::ldexp(3.0, 2) - 12.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[unstable(feature = "float_extras",
+ reason = "pending integer conventions",
+ issue = "27752")]
+ #[rustc_deprecated(since = "1.11.0",
+ reason = "never really came to fruition and easily \
+ implementable outside the standard library")]
+ #[inline]
+ pub fn ldexp(x: f32, exp: isize) -> f32 {
+ unsafe { cmath::ldexpf(x, exp as c_int) }
+ }
+
+ /// Breaks the number into a normalized fraction and a base-2 exponent,
+ /// satisfying:
+ ///
+ /// * `self = x * 2^exp`
+ /// * `0.5 <= abs(x) < 1.0`
+ ///
+ /// ```
+ /// #![feature(float_extras)]
+ ///
+ /// use std::f32;
+ ///
+ /// let x = 4.0f32;
+ ///
+ /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
+ /// let f = x.frexp();
+ /// let abs_difference_0 = (f.0 - 0.5).abs();
+ /// let abs_difference_1 = (f.1 as f32 - 3.0).abs();
+ ///
+ /// assert!(abs_difference_0 <= f32::EPSILON);
+ /// assert!(abs_difference_1 <= f32::EPSILON);
+ /// ```
+ #[unstable(feature = "float_extras",
+ reason = "pending integer conventions",
+ issue = "27752")]
+ #[rustc_deprecated(since = "1.11.0",
+ reason = "never really came to fruition and easily \
+ implementable outside the standard library")]
+ #[inline]
+ pub fn frexp(self) -> (f32, isize) {
+ unsafe {
+ let mut exp = 0;
+ let x = cmath::frexpf(self, &mut exp);
+ (x, exp as isize)
+ }
+ }
+
+ /// Returns the next representable floating-point value in the direction of
+ /// `other`.
+ ///
+ /// ```
+ /// #![feature(float_extras)]
+ ///
+ /// use std::f32;
+ ///
+ /// let x = 1.0f32;
+ ///
+ /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
+ ///
+ /// assert!(abs_diff <= f32::EPSILON);
+ /// ```
+ #[unstable(feature = "float_extras",
+ reason = "unsure about its place in the world",
+ issue = "27752")]
+ #[rustc_deprecated(since = "1.11.0",
+ reason = "never really came to fruition and easily \
+ implementable outside the standard library")]
+ #[inline]
+ pub fn next_after(self, other: f32) -> f32 {
+ unsafe { cmath::nextafterf(self, other) }
+ }
+
+ /// Returns the maximum of the two numbers.
+ ///
+ /// ```
+ /// let x = 1.0f32;
+ /// let y = 2.0f32;
+ ///
+ /// assert_eq!(x.max(y), y);
+ /// ```
+ ///
+ /// If one of the arguments is NaN, then the other argument is returned.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn max(self, other: f32) -> f32 {
+ unsafe { cmath::fmaxf(self, other) }
+ }
+
+ /// Returns the minimum of the two numbers.
+ ///
+ /// ```
+ /// let x = 1.0f32;
+ /// let y = 2.0f32;
+ ///
+ /// assert_eq!(x.min(y), x);
+ /// ```
+ ///
+ /// If one of the arguments is NaN, then the other argument is returned.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn min(self, other: f32) -> f32 {
+ unsafe { cmath::fminf(self, other) }
+ }
+
+ /// The positive difference of two numbers.
+ ///
+ /// * If `self <= other`: `0:0`
+ /// * Else: `self - other`
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 3.0f32;
+ /// let y = -3.0f32;
+ ///
+ /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
+ /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
+ ///
+ /// assert!(abs_difference_x <= f32::EPSILON);
+ /// assert!(abs_difference_y <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ #[rustc_deprecated(since = "1.10.0",
+ reason = "you probably meant `(self - other).abs()`: \
+ this operation is `(self - other).max(0.0)` (also \
+ known as `fdimf` in C). If you truly need the positive \
+ difference, consider using that expression or the C function \
+ `fdimf`, depending on how you wish to handle NaN (please consider \
+ filing an issue describing your use-case too).")]
+ pub fn abs_sub(self, other: f32) -> f32 {
+ unsafe { cmath::fdimf(self, other) }
+ }
+
+ /// Takes the cubic root of a number.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 8.0f32;
+ ///
+ /// // x^(1/3) - 2 == 0
+ /// let abs_difference = (x.cbrt() - 2.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn cbrt(self) -> f32 {
+ unsafe { cmath::cbrtf(self) }
+ }
+
+ /// Calculates the length of the hypotenuse of a right-angle triangle given
+ /// legs of length `x` and `y`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 2.0f32;
+ /// let y = 3.0f32;
+ ///
+ /// // sqrt(x^2 + y^2)
+ /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn hypot(self, other: f32) -> f32 {
+ unsafe { cmath::hypotf(self, other) }
+ }
+
+ /// Computes the sine of a number (in radians).
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = f32::consts::PI/2.0;
+ ///
+ /// let abs_difference = (x.sin() - 1.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn sin(self) -> f32 {
+ // see notes in `core::f32::Float::floor`
+ #[cfg(target_env = "msvc")]
+ return (self as f64).sin() as f32;
+ #[cfg(not(target_env = "msvc"))]
+ return unsafe { intrinsics::sinf32(self) };
+ }
+
+ /// Computes the cosine of a number (in radians).
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 2.0*f32::consts::PI;
+ ///
+ /// let abs_difference = (x.cos() - 1.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn cos(self) -> f32 {
+ // see notes in `core::f32::Float::floor`
+ #[cfg(target_env = "msvc")]
+ return (self as f64).cos() as f32;
+ #[cfg(not(target_env = "msvc"))]
+ return unsafe { intrinsics::cosf32(self) };
+ }
+
+ /// Computes the tangent of a number (in radians).
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = f32::consts::PI / 4.0;
+ /// let abs_difference = (x.tan() - 1.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn tan(self) -> f32 {
+ unsafe { cmath::tanf(self) }
+ }
+
+ /// Computes the arcsine of a number. Return value is in radians in
+ /// the range [-pi/2, pi/2] or NaN if the number is outside the range
+ /// [-1, 1].
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let f = f32::consts::PI / 2.0;
+ ///
+ /// // asin(sin(pi/2))
+ /// let abs_difference = (f.sin().asin() - f32::consts::PI / 2.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn asin(self) -> f32 {
+ unsafe { cmath::asinf(self) }
+ }
+
+ /// Computes the arccosine of a number. Return value is in radians in
+ /// the range [0, pi] or NaN if the number is outside the range
+ /// [-1, 1].
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let f = f32::consts::PI / 4.0;
+ ///
+ /// // acos(cos(pi/4))
+ /// let abs_difference = (f.cos().acos() - f32::consts::PI / 4.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn acos(self) -> f32 {
+ unsafe { cmath::acosf(self) }
+ }
+
+ /// Computes the arctangent of a number. Return value is in radians in the
+ /// range [-pi/2, pi/2];
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let f = 1.0f32;
+ ///
+ /// // atan(tan(1))
+ /// let abs_difference = (f.tan().atan() - 1.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn atan(self) -> f32 {
+ unsafe { cmath::atanf(self) }
+ }
+
+ /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
+ ///
+ /// * `x = 0`, `y = 0`: `0`
+ /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
+ /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
+ /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let pi = f32::consts::PI;
+ /// // All angles from horizontal right (+x)
+ /// // 45 deg counter-clockwise
+ /// let x1 = 3.0f32;
+ /// let y1 = -3.0f32;
+ ///
+ /// // 135 deg clockwise
+ /// let x2 = -3.0f32;
+ /// let y2 = 3.0f32;
+ ///
+ /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
+ /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
+ ///
+ /// assert!(abs_difference_1 <= f32::EPSILON);
+ /// assert!(abs_difference_2 <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn atan2(self, other: f32) -> f32 {
+ unsafe { cmath::atan2f(self, other) }
+ }
+
+ /// Simultaneously computes the sine and cosine of the number, `x`. Returns
+ /// `(sin(x), cos(x))`.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = f32::consts::PI/4.0;
+ /// let f = x.sin_cos();
+ ///
+ /// let abs_difference_0 = (f.0 - x.sin()).abs();
+ /// let abs_difference_1 = (f.1 - x.cos()).abs();
+ ///
+ /// assert!(abs_difference_0 <= f32::EPSILON);
+ /// assert!(abs_difference_1 <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn sin_cos(self) -> (f32, f32) {
+ (self.sin(), self.cos())
+ }
+
+ /// Returns `e^(self) - 1` in a way that is accurate even if the
+ /// number is close to zero.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 6.0f32;
+ ///
+ /// // e^(ln(6)) - 1
+ /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn exp_m1(self) -> f32 {
+ unsafe { cmath::expm1f(self) }
+ }
+
+ /// Returns `ln(1+n)` (natural logarithm) more accurately than if
+ /// the operations were performed separately.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = f32::consts::E - 1.0;
+ ///
+ /// // ln(1 + (e - 1)) == ln(e) == 1
+ /// let abs_difference = (x.ln_1p() - 1.0).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn ln_1p(self) -> f32 {
+ unsafe { cmath::log1pf(self) }
+ }
+
+ /// Hyperbolic sine function.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let e = f32::consts::E;
+ /// let x = 1.0f32;
+ ///
+ /// let f = x.sinh();
+ /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
+ /// let g = (e*e - 1.0)/(2.0*e);
+ /// let abs_difference = (f - g).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn sinh(self) -> f32 {
+ unsafe { cmath::sinhf(self) }
+ }
+
+ /// Hyperbolic cosine function.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let e = f32::consts::E;
+ /// let x = 1.0f32;
+ /// let f = x.cosh();
+ /// // Solving cosh() at 1 gives this result
+ /// let g = (e*e + 1.0)/(2.0*e);
+ /// let abs_difference = (f - g).abs();
+ ///
+ /// // Same result
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn cosh(self) -> f32 {
+ unsafe { cmath::coshf(self) }
+ }
+
+ /// Hyperbolic tangent function.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let e = f32::consts::E;
+ /// let x = 1.0f32;
+ ///
+ /// let f = x.tanh();
+ /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
+ /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
+ /// let abs_difference = (f - g).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn tanh(self) -> f32 {
+ unsafe { cmath::tanhf(self) }
+ }
+
+ /// Inverse hyperbolic sine function.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 1.0f32;
+ /// let f = x.sinh().asinh();
+ ///
+ /// let abs_difference = (f - x).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn asinh(self) -> f32 {
+ if self == NEG_INFINITY {
+ NEG_INFINITY
+ } else {
+ (self + ((self * self) + 1.0).sqrt()).ln()
+ }
+ }
+
+ /// Inverse hyperbolic cosine function.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let x = 1.0f32;
+ /// let f = x.cosh().acosh();
+ ///
+ /// let abs_difference = (f - x).abs();
+ ///
+ /// assert!(abs_difference <= f32::EPSILON);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn acosh(self) -> f32 {
+ match self {
+ x if x < 1.0 => ::f32::NAN,
+ x => (x + ((x * x) - 1.0).sqrt()).ln(),
+ }
+ }
+
+ /// Inverse hyperbolic tangent function.
+ ///
+ /// ```
+ /// use std::f32;
+ ///
+ /// let e = f32::consts::E;
+ /// let f = e.tanh().atanh();
+ ///
+ /// let abs_difference = (f - e).abs();
+ ///
+ /// assert!(abs_difference <= 1e-5);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn atanh(self) -> f32 {
+ 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use f32;
+ use f32::*;
+ use num::*;
+ use num::FpCategory as Fp;
+
+ #[test]
+ fn test_num_f32() {
+ test_num(10f32, 2f32);
+ }
+
+ #[test]
+ fn test_min_nan() {
+ assert_eq!(NAN.min(2.0), 2.0);
+ assert_eq!(2.0f32.min(NAN), 2.0);
+ }
+
+ #[test]
+ fn test_max_nan() {
+ assert_eq!(NAN.max(2.0), 2.0);
+ assert_eq!(2.0f32.max(NAN), 2.0);
+ }
+
+ #[test]
+ fn test_nan() {
+ let nan: f32 = f32::NAN;
+ assert!(nan.is_nan());
+ assert!(!nan.is_infinite());
+ assert!(!nan.is_finite());
+ assert!(!nan.is_normal());
+ assert!(!nan.is_sign_positive());
+ assert!(!nan.is_sign_negative());
+ assert_eq!(Fp::Nan, nan.classify());
+ }
+
+ #[test]
+ fn test_infinity() {
+ let inf: f32 = f32::INFINITY;
+ assert!(inf.is_infinite());
+ assert!(!inf.is_finite());
+ assert!(inf.is_sign_positive());
+ assert!(!inf.is_sign_negative());
+ assert!(!inf.is_nan());
+ assert!(!inf.is_normal());
+ assert_eq!(Fp::Infinite, inf.classify());
+ }
+
+ #[test]
+ fn test_neg_infinity() {
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert!(neg_inf.is_infinite());
+ assert!(!neg_inf.is_finite());
+ assert!(!neg_inf.is_sign_positive());
+ assert!(neg_inf.is_sign_negative());
+ assert!(!neg_inf.is_nan());
+ assert!(!neg_inf.is_normal());
+ assert_eq!(Fp::Infinite, neg_inf.classify());
+ }
+
+ #[test]
+ fn test_zero() {
+ let zero: f32 = 0.0f32;
+ assert_eq!(0.0, zero);
+ assert!(!zero.is_infinite());
+ assert!(zero.is_finite());
+ assert!(zero.is_sign_positive());
+ assert!(!zero.is_sign_negative());
+ assert!(!zero.is_nan());
+ assert!(!zero.is_normal());
+ assert_eq!(Fp::Zero, zero.classify());
+ }
+
+ #[test]
+ fn test_neg_zero() {
+ let neg_zero: f32 = -0.0;
+ assert_eq!(0.0, neg_zero);
+ assert!(!neg_zero.is_infinite());
+ assert!(neg_zero.is_finite());
+ assert!(!neg_zero.is_sign_positive());
+ assert!(neg_zero.is_sign_negative());
+ assert!(!neg_zero.is_nan());
+ assert!(!neg_zero.is_normal());
+ assert_eq!(Fp::Zero, neg_zero.classify());
+ }
+
+ #[test]
+ fn test_one() {
+ let one: f32 = 1.0f32;
+ assert_eq!(1.0, one);
+ assert!(!one.is_infinite());
+ assert!(one.is_finite());
+ assert!(one.is_sign_positive());
+ assert!(!one.is_sign_negative());
+ assert!(!one.is_nan());
+ assert!(one.is_normal());
+ assert_eq!(Fp::Normal, one.classify());
+ }
+
+ #[test]
+ fn test_is_nan() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert!(nan.is_nan());
+ assert!(!0.0f32.is_nan());
+ assert!(!5.3f32.is_nan());
+ assert!(!(-10.732f32).is_nan());
+ assert!(!inf.is_nan());
+ assert!(!neg_inf.is_nan());
+ }
+
+ #[test]
+ fn test_is_infinite() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert!(!nan.is_infinite());
+ assert!(inf.is_infinite());
+ assert!(neg_inf.is_infinite());
+ assert!(!0.0f32.is_infinite());
+ assert!(!42.8f32.is_infinite());
+ assert!(!(-109.2f32).is_infinite());
+ }
+
+ #[test]
+ fn test_is_finite() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert!(!nan.is_finite());
+ assert!(!inf.is_finite());
+ assert!(!neg_inf.is_finite());
+ assert!(0.0f32.is_finite());
+ assert!(42.8f32.is_finite());
+ assert!((-109.2f32).is_finite());
+ }
+
+ #[test]
+ fn test_is_normal() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ let zero: f32 = 0.0f32;
+ let neg_zero: f32 = -0.0;
+ assert!(!nan.is_normal());
+ assert!(!inf.is_normal());
+ assert!(!neg_inf.is_normal());
+ assert!(!zero.is_normal());
+ assert!(!neg_zero.is_normal());
+ assert!(1f32.is_normal());
+ assert!(1e-37f32.is_normal());
+ assert!(!1e-38f32.is_normal());
+ }
+
+ #[test]
+ fn test_classify() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ let zero: f32 = 0.0f32;
+ let neg_zero: f32 = -0.0;
+ assert_eq!(nan.classify(), Fp::Nan);
+ assert_eq!(inf.classify(), Fp::Infinite);
+ assert_eq!(neg_inf.classify(), Fp::Infinite);
+ assert_eq!(zero.classify(), Fp::Zero);
+ assert_eq!(neg_zero.classify(), Fp::Zero);
+ assert_eq!(1f32.classify(), Fp::Normal);
+ assert_eq!(1e-37f32.classify(), Fp::Normal);
+ assert_eq!(1e-38f32.classify(), Fp::Subnormal);
+ }
+
+ #[test]
+ #[allow(deprecated)]
+ fn test_integer_decode() {
+ assert_eq!(3.14159265359f32.integer_decode(), (13176795, -22, 1));
+ assert_eq!((-8573.5918555f32).integer_decode(), (8779358, -10, -1));
+ assert_eq!(2f32.powf(100.0).integer_decode(), (8388608, 77, 1));
+ assert_eq!(0f32.integer_decode(), (0, -150, 1));
+ assert_eq!((-0f32).integer_decode(), (0, -150, -1));
+ assert_eq!(INFINITY.integer_decode(), (8388608, 105, 1));
+ assert_eq!(NEG_INFINITY.integer_decode(), (8388608, 105, -1));
+
+ // Ignore the "sign" (quiet / signalling flag) of NAN.
+ // It can vary between runtime operations and LLVM folding.
+ let (nan_m, nan_e, _nan_s) = NAN.integer_decode();
+ assert_eq!((nan_m, nan_e), (12582912, 105));
+ }
+
+ #[test]
+ fn test_floor() {
+ assert_approx_eq!(1.0f32.floor(), 1.0f32);
+ assert_approx_eq!(1.3f32.floor(), 1.0f32);
+ assert_approx_eq!(1.5f32.floor(), 1.0f32);
+ assert_approx_eq!(1.7f32.floor(), 1.0f32);
+ assert_approx_eq!(0.0f32.floor(), 0.0f32);
+ assert_approx_eq!((-0.0f32).floor(), -0.0f32);
+ assert_approx_eq!((-1.0f32).floor(), -1.0f32);
+ assert_approx_eq!((-1.3f32).floor(), -2.0f32);
+ assert_approx_eq!((-1.5f32).floor(), -2.0f32);
+ assert_approx_eq!((-1.7f32).floor(), -2.0f32);
+ }
+
+ #[test]
+ fn test_ceil() {
+ assert_approx_eq!(1.0f32.ceil(), 1.0f32);
+ assert_approx_eq!(1.3f32.ceil(), 2.0f32);
+ assert_approx_eq!(1.5f32.ceil(), 2.0f32);
+ assert_approx_eq!(1.7f32.ceil(), 2.0f32);
+ assert_approx_eq!(0.0f32.ceil(), 0.0f32);
+ assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
+ assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
+ assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
+ assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
+ assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
+ }
+
+ #[test]
+ fn test_round() {
+ assert_approx_eq!(1.0f32.round(), 1.0f32);
+ assert_approx_eq!(1.3f32.round(), 1.0f32);
+ assert_approx_eq!(1.5f32.round(), 2.0f32);
+ assert_approx_eq!(1.7f32.round(), 2.0f32);
+ assert_approx_eq!(0.0f32.round(), 0.0f32);
+ assert_approx_eq!((-0.0f32).round(), -0.0f32);
+ assert_approx_eq!((-1.0f32).round(), -1.0f32);
+ assert_approx_eq!((-1.3f32).round(), -1.0f32);
+ assert_approx_eq!((-1.5f32).round(), -2.0f32);
+ assert_approx_eq!((-1.7f32).round(), -2.0f32);
+ }
+
+ #[test]
+ fn test_trunc() {
+ assert_approx_eq!(1.0f32.trunc(), 1.0f32);
+ assert_approx_eq!(1.3f32.trunc(), 1.0f32);
+ assert_approx_eq!(1.5f32.trunc(), 1.0f32);
+ assert_approx_eq!(1.7f32.trunc(), 1.0f32);
+ assert_approx_eq!(0.0f32.trunc(), 0.0f32);
+ assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
+ assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
+ assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
+ assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
+ assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
+ }
+
+ #[test]
+ fn test_fract() {
+ assert_approx_eq!(1.0f32.fract(), 0.0f32);
+ assert_approx_eq!(1.3f32.fract(), 0.3f32);
+ assert_approx_eq!(1.5f32.fract(), 0.5f32);
+ assert_approx_eq!(1.7f32.fract(), 0.7f32);
+ assert_approx_eq!(0.0f32.fract(), 0.0f32);
+ assert_approx_eq!((-0.0f32).fract(), -0.0f32);
+ assert_approx_eq!((-1.0f32).fract(), -0.0f32);
+ assert_approx_eq!((-1.3f32).fract(), -0.3f32);
+ assert_approx_eq!((-1.5f32).fract(), -0.5f32);
+ assert_approx_eq!((-1.7f32).fract(), -0.7f32);
+ }
+
+ #[test]
+ fn test_abs() {
+ assert_eq!(INFINITY.abs(), INFINITY);
+ assert_eq!(1f32.abs(), 1f32);
+ assert_eq!(0f32.abs(), 0f32);
+ assert_eq!((-0f32).abs(), 0f32);
+ assert_eq!((-1f32).abs(), 1f32);
+ assert_eq!(NEG_INFINITY.abs(), INFINITY);
+ assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
+ assert!(NAN.abs().is_nan());
+ }
+
+ #[test]
+ fn test_signum() {
+ assert_eq!(INFINITY.signum(), 1f32);
+ assert_eq!(1f32.signum(), 1f32);
+ assert_eq!(0f32.signum(), 1f32);
+ assert_eq!((-0f32).signum(), -1f32);
+ assert_eq!((-1f32).signum(), -1f32);
+ assert_eq!(NEG_INFINITY.signum(), -1f32);
+ assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
+ assert!(NAN.signum().is_nan());
+ }
+
+ #[test]
+ fn test_is_sign_positive() {
+ assert!(INFINITY.is_sign_positive());
+ assert!(1f32.is_sign_positive());
+ assert!(0f32.is_sign_positive());
+ assert!(!(-0f32).is_sign_positive());
+ assert!(!(-1f32).is_sign_positive());
+ assert!(!NEG_INFINITY.is_sign_positive());
+ assert!(!(1f32/NEG_INFINITY).is_sign_positive());
+ assert!(!NAN.is_sign_positive());
+ }
+
+ #[test]
+ fn test_is_sign_negative() {
+ assert!(!INFINITY.is_sign_negative());
+ assert!(!1f32.is_sign_negative());
+ assert!(!0f32.is_sign_negative());
+ assert!((-0f32).is_sign_negative());
+ assert!((-1f32).is_sign_negative());
+ assert!(NEG_INFINITY.is_sign_negative());
+ assert!((1f32/NEG_INFINITY).is_sign_negative());
+ assert!(!NAN.is_sign_negative());
+ }
+
+ #[test]
+ fn test_mul_add() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
+ assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
+ assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
+ assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
+ assert!(nan.mul_add(7.8, 9.0).is_nan());
+ assert_eq!(inf.mul_add(7.8, 9.0), inf);
+ assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
+ assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
+ assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
+ }
+
+ #[test]
+ fn test_recip() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert_eq!(1.0f32.recip(), 1.0);
+ assert_eq!(2.0f32.recip(), 0.5);
+ assert_eq!((-0.4f32).recip(), -2.5);
+ assert_eq!(0.0f32.recip(), inf);
+ assert!(nan.recip().is_nan());
+ assert_eq!(inf.recip(), 0.0);
+ assert_eq!(neg_inf.recip(), 0.0);
+ }
+
+ #[test]
+ fn test_powi() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert_eq!(1.0f32.powi(1), 1.0);
+ assert_approx_eq!((-3.1f32).powi(2), 9.61);
+ assert_approx_eq!(5.9f32.powi(-2), 0.028727);
+ assert_eq!(8.3f32.powi(0), 1.0);
+ assert!(nan.powi(2).is_nan());
+ assert_eq!(inf.powi(3), inf);
+ assert_eq!(neg_inf.powi(2), inf);
+ }
+
+ #[test]
+ fn test_powf() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert_eq!(1.0f32.powf(1.0), 1.0);
+ assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
+ assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
+ assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
+ assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
+ assert_eq!(8.3f32.powf(0.0), 1.0);
+ assert!(nan.powf(2.0).is_nan());
+ assert_eq!(inf.powf(2.0), inf);
+ assert_eq!(neg_inf.powf(3.0), neg_inf);
+ }
+
+ #[test]
+ fn test_sqrt_domain() {
+ assert!(NAN.sqrt().is_nan());
+ assert!(NEG_INFINITY.sqrt().is_nan());
+ assert!((-1.0f32).sqrt().is_nan());
+ assert_eq!((-0.0f32).sqrt(), -0.0);
+ assert_eq!(0.0f32.sqrt(), 0.0);
+ assert_eq!(1.0f32.sqrt(), 1.0);
+ assert_eq!(INFINITY.sqrt(), INFINITY);
+ }
+
+ #[test]
+ fn test_exp() {
+ assert_eq!(1.0, 0.0f32.exp());
+ assert_approx_eq!(2.718282, 1.0f32.exp());
+ assert_approx_eq!(148.413162, 5.0f32.exp());
+
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ let nan: f32 = f32::NAN;
+ assert_eq!(inf, inf.exp());
+ assert_eq!(0.0, neg_inf.exp());
+ assert!(nan.exp().is_nan());
+ }
+
+ #[test]
+ fn test_exp2() {
+ assert_eq!(32.0, 5.0f32.exp2());
+ assert_eq!(1.0, 0.0f32.exp2());
+
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ let nan: f32 = f32::NAN;
+ assert_eq!(inf, inf.exp2());
+ assert_eq!(0.0, neg_inf.exp2());
+ assert!(nan.exp2().is_nan());
+ }
+
+ #[test]
+ fn test_ln() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert_approx_eq!(1.0f32.exp().ln(), 1.0);
+ assert!(nan.ln().is_nan());
+ assert_eq!(inf.ln(), inf);
+ assert!(neg_inf.ln().is_nan());
+ assert!((-2.3f32).ln().is_nan());
+ assert_eq!((-0.0f32).ln(), neg_inf);
+ assert_eq!(0.0f32.ln(), neg_inf);
+ assert_approx_eq!(4.0f32.ln(), 1.386294);
+ }
+
+ #[test]
+ fn test_log() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert_eq!(10.0f32.log(10.0), 1.0);
+ assert_approx_eq!(2.3f32.log(3.5), 0.664858);
+ assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
+ assert!(1.0f32.log(1.0).is_nan());
+ assert!(1.0f32.log(-13.9).is_nan());
+ assert!(nan.log(2.3).is_nan());
+ assert_eq!(inf.log(10.0), inf);
+ assert!(neg_inf.log(8.8).is_nan());
+ assert!((-2.3f32).log(0.1).is_nan());
+ assert_eq!((-0.0f32).log(2.0), neg_inf);
+ assert_eq!(0.0f32.log(7.0), neg_inf);
+ }
+
+ #[test]
+ fn test_log2() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert_approx_eq!(10.0f32.log2(), 3.321928);
+ assert_approx_eq!(2.3f32.log2(), 1.201634);
+ assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
+ assert!(nan.log2().is_nan());
+ assert_eq!(inf.log2(), inf);
+ assert!(neg_inf.log2().is_nan());
+ assert!((-2.3f32).log2().is_nan());
+ assert_eq!((-0.0f32).log2(), neg_inf);
+ assert_eq!(0.0f32.log2(), neg_inf);
+ }
+
+ #[test]
+ fn test_log10() {
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert_eq!(10.0f32.log10(), 1.0);
+ assert_approx_eq!(2.3f32.log10(), 0.361728);
+ assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
+ assert_eq!(1.0f32.log10(), 0.0);
+ assert!(nan.log10().is_nan());
+ assert_eq!(inf.log10(), inf);
+ assert!(neg_inf.log10().is_nan());
+ assert!((-2.3f32).log10().is_nan());
+ assert_eq!((-0.0f32).log10(), neg_inf);
+ assert_eq!(0.0f32.log10(), neg_inf);
+ }
+
+ #[test]
+ fn test_to_degrees() {
+ let pi: f32 = consts::PI;
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert_eq!(0.0f32.to_degrees(), 0.0);
+ assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
+ assert_eq!(pi.to_degrees(), 180.0);
+ assert!(nan.to_degrees().is_nan());
+ assert_eq!(inf.to_degrees(), inf);
+ assert_eq!(neg_inf.to_degrees(), neg_inf);
+ }
+
+ #[test]
+ fn test_to_radians() {
+ let pi: f32 = consts::PI;
+ let nan: f32 = f32::NAN;
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ assert_eq!(0.0f32.to_radians(), 0.0);
+ assert_approx_eq!(154.6f32.to_radians(), 2.698279);
+ assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
+ assert_eq!(180.0f32.to_radians(), pi);
+ assert!(nan.to_radians().is_nan());
+ assert_eq!(inf.to_radians(), inf);
+ assert_eq!(neg_inf.to_radians(), neg_inf);
+ }
+
+ #[test]
+ #[allow(deprecated)]
+ fn test_ldexp() {
+ let f1 = 2.0f32.powi(-123);
+ let f2 = 2.0f32.powi(-111);
+ let f3 = 1.75 * 2.0f32.powi(-12);
+ assert_eq!(f32::ldexp(1f32, -123), f1);
+ assert_eq!(f32::ldexp(1f32, -111), f2);
+ assert_eq!(f32::ldexp(1.75f32, -12), f3);
+
+ assert_eq!(f32::ldexp(0f32, -123), 0f32);
+ assert_eq!(f32::ldexp(-0f32, -123), -0f32);
+
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ let nan: f32 = f32::NAN;
+ assert_eq!(f32::ldexp(inf, -123), inf);
+ assert_eq!(f32::ldexp(neg_inf, -123), neg_inf);
+ assert!(f32::ldexp(nan, -123).is_nan());
+ }
+
+ #[test]
+ #[allow(deprecated)]
+ fn test_frexp() {
+ let f1 = 2.0f32.powi(-123);
+ let f2 = 2.0f32.powi(-111);
+ let f3 = 1.75 * 2.0f32.powi(-123);
+ let (x1, exp1) = f1.frexp();
+ let (x2, exp2) = f2.frexp();
+ let (x3, exp3) = f3.frexp();
+ assert_eq!((x1, exp1), (0.5f32, -122));
+ assert_eq!((x2, exp2), (0.5f32, -110));
+ assert_eq!((x3, exp3), (0.875f32, -122));
+ assert_eq!(f32::ldexp(x1, exp1), f1);
+ assert_eq!(f32::ldexp(x2, exp2), f2);
+ assert_eq!(f32::ldexp(x3, exp3), f3);
+
+ assert_eq!(0f32.frexp(), (0f32, 0));
+ assert_eq!((-0f32).frexp(), (-0f32, 0));
+ }
+
+ #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
+ #[allow(deprecated)]
+ fn test_frexp_nowin() {
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ let nan: f32 = f32::NAN;
+ assert_eq!(match inf.frexp() { (x, _) => x }, inf);
+ assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
+ assert!(match nan.frexp() { (x, _) => x.is_nan() })
+ }
+
+ #[test]
+ fn test_asinh() {
+ assert_eq!(0.0f32.asinh(), 0.0f32);
+ assert_eq!((-0.0f32).asinh(), -0.0f32);
+
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ let nan: f32 = f32::NAN;
+ assert_eq!(inf.asinh(), inf);
+ assert_eq!(neg_inf.asinh(), neg_inf);
+ assert!(nan.asinh().is_nan());
+ assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
+ assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
+ }
+
+ #[test]
+ fn test_acosh() {
+ assert_eq!(1.0f32.acosh(), 0.0f32);
+ assert!(0.999f32.acosh().is_nan());
+
+ let inf: f32 = f32::INFINITY;
+ let neg_inf: f32 = f32::NEG_INFINITY;
+ let nan: f32 = f32::NAN;
+ assert_eq!(inf.acosh(), inf);
+ assert!(neg_inf.acosh().is_nan());
+ assert!(nan.acosh().is_nan());
+ assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
+ assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
+ }
+
+ #[test]
+ fn test_atanh() {
+ assert_eq!(0.0f32.atanh(), 0.0f32);
+ assert_eq!((-0.0f32).atanh(), -0.0f32);
+
+ let inf32: f32 = f32::INFINITY;
+ let neg_inf32: f32 = f32::NEG_INFINITY;
+ assert_eq!(1.0f32.atanh(), inf32);
+ assert_eq!((-1.0f32).atanh(), neg_inf32);
+
+ assert!(2f64.atanh().atanh().is_nan());
+ assert!((-2f64).atanh().atanh().is_nan());
+
+ let inf64: f32 = f32::INFINITY;
+ let neg_inf64: f32 = f32::NEG_INFINITY;
+ let nan32: f32 = f32::NAN;
+ assert!(inf64.atanh().is_nan());
+ assert!(neg_inf64.atanh().is_nan());
+ assert!(nan32.atanh().is_nan());
+
+ assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
+ assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
+ }
+
+ #[test]
+ fn test_real_consts() {
+ use super::consts;
+
+ let pi: f32 = consts::PI;
+ let frac_pi_2: f32 = consts::FRAC_PI_2;
+ let frac_pi_3: f32 = consts::FRAC_PI_3;
+ let frac_pi_4: f32 = consts::FRAC_PI_4;
+ let frac_pi_6: f32 = consts::FRAC_PI_6;
+ let frac_pi_8: f32 = consts::FRAC_PI_8;
+ let frac_1_pi: f32 = consts::FRAC_1_PI;
+ let frac_2_pi: f32 = consts::FRAC_2_PI;
+ let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
+ let sqrt2: f32 = consts::SQRT_2;
+ let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
+ let e: f32 = consts::E;
+ let log2_e: f32 = consts::LOG2_E;
+ let log10_e: f32 = consts::LOG10_E;
+ let ln_2: f32 = consts::LN_2;
+ let ln_10: f32 = consts::LN_10;
+
+ assert_approx_eq!(frac_pi_2, pi / 2f32);
+ assert_approx_eq!(frac_pi_3, pi / 3f32);
+ assert_approx_eq!(frac_pi_4, pi / 4f32);
+ assert_approx_eq!(frac_pi_6, pi / 6f32);
+ assert_approx_eq!(frac_pi_8, pi / 8f32);
+ assert_approx_eq!(frac_1_pi, 1f32 / pi);
+ assert_approx_eq!(frac_2_pi, 2f32 / pi);
+ assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
+ assert_approx_eq!(sqrt2, 2f32.sqrt());
+ assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
+ assert_approx_eq!(log2_e, e.log2());
+ assert_approx_eq!(log10_e, e.log10());
+ assert_approx_eq!(ln_2, 2f32.ln());
+ assert_approx_eq!(ln_10, 10f32.ln());
+ }
+}
diff --git a/ctr-std/src/f64.rs b/ctr-std/src/f64.rs
new file mode 100644
index 0000000..67a1c30
--- /dev/null
+++ b/ctr-std/src/f64.rs
@@ -0,0 +1,1756 @@
+// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! The 64-bit floating point type.
+//!
+//! *[See also the `f64` primitive type](../primitive.f64.html).*
+
+#![stable(feature = "rust1", since = "1.0.0")]
+#![allow(missing_docs)]
+
+#[cfg(not(test))]
+use core::num;
+#[cfg(not(test))]
+use intrinsics;
+#[cfg(not(test))]
+use libc::c_int;
+#[cfg(not(test))]
+use num::FpCategory;
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::f64::{MIN, MIN_POSITIVE, MAX};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::f64::consts;
+
+#[allow(dead_code)]
+mod cmath {
+ use libc::{c_double, c_int};
+
+ #[link_name = "m"]
+ extern {
+ pub fn acos(n: c_double) -> c_double;
+ pub fn asin(n: c_double) -> c_double;
+ pub fn atan(n: c_double) -> c_double;
+ pub fn atan2(a: c_double, b: c_double) -> c_double;
+ pub fn cbrt(n: c_double) -> c_double;
+ pub fn cosh(n: c_double) -> c_double;
+ pub fn erf(n: c_double) -> c_double;
+ pub fn erfc(n: c_double) -> c_double;
+ pub fn expm1(n: c_double) -> c_double;
+ pub fn fdim(a: c_double, b: c_double) -> c_double;
+ pub fn fmax(a: c_double, b: c_double) -> c_double;
+ pub fn fmin(a: c_double, b: c_double) -> c_double;
+ pub fn fmod(a: c_double, b: c_double) -> c_double;
+ pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
+ pub fn ilogb(n: c_double) -> c_int;
+ pub fn ldexp(x: c_double, n: c_int) -> c_double;
+ pub fn logb(n: c_double) -> c_double;
+ pub fn log1p(n: c_double) -> c_double;
+ pub fn nextafter(x: c_double, y: c_double) -> c_double;
+ pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
+ pub fn sinh(n: c_double) -> c_double;
+ pub fn tan(n: c_double) -> c_double;
+ pub fn tanh(n: c_double) -> c_double;
+ pub fn tgamma(n: c_double) -> c_double;
+
+ // These are commonly only available for doubles
+
+ pub fn j0(n: c_double) -> c_double;
+ pub fn j1(n: c_double) -> c_double;
+ pub fn jn(i: c_int, n: c_double) -> c_double;
+
+ pub fn y0(n: c_double) -> c_double;
+ pub fn y1(n: c_double) -> c_double;
+ pub fn yn(i: c_int, n: c_double) -> c_double;
+
+ #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")]
+ pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
+
+ #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")]
+ pub fn hypot(x: c_double, y: c_double) -> c_double;
+ }
+}
+
+#[cfg(not(test))]
+#[lang = "f64"]
+impl f64 {
+ /// Returns `true` if this value is `NaN` and false otherwise.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let nan = f64::NAN;
+ /// let f = 7.0_f64;
+ ///
+ /// assert!(nan.is_nan());
+ /// assert!(!f.is_nan());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
+
+ /// Returns `true` if this value is positive infinity or negative infinity and
+ /// false otherwise.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let f = 7.0f64;
+ /// let inf = f64::INFINITY;
+ /// let neg_inf = f64::NEG_INFINITY;
+ /// let nan = f64::NAN;
+ ///
+ /// assert!(!f.is_infinite());
+ /// assert!(!nan.is_infinite());
+ ///
+ /// assert!(inf.is_infinite());
+ /// assert!(neg_inf.is_infinite());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
+
+ /// Returns `true` if this number is neither infinite nor `NaN`.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let f = 7.0f64;
+ /// let inf: f64 = f64::INFINITY;
+ /// let neg_inf: f64 = f64::NEG_INFINITY;
+ /// let nan: f64 = f64::NAN;
+ ///
+ /// assert!(f.is_finite());
+ ///
+ /// assert!(!nan.is_finite());
+ /// assert!(!inf.is_finite());
+ /// assert!(!neg_inf.is_finite());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
+
+ /// Returns `true` if the number is neither zero, infinite,
+ /// [subnormal][subnormal], or `NaN`.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
+ /// let max = f64::MAX;
+ /// let lower_than_min = 1.0e-308_f64;
+ /// let zero = 0.0f64;
+ ///
+ /// assert!(min.is_normal());
+ /// assert!(max.is_normal());
+ ///
+ /// assert!(!zero.is_normal());
+ /// assert!(!f64::NAN.is_normal());
+ /// assert!(!f64::INFINITY.is_normal());
+ /// // Values between `0` and `min` are Subnormal.
+ /// assert!(!lower_than_min.is_normal());
+ /// ```
+ /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
+
+ /// Returns the floating point category of the number. If only one property
+ /// is going to be tested, it is generally faster to use the specific
+ /// predicate instead.
+ ///
+ /// ```
+ /// use std::num::FpCategory;
+ /// use std::f64;
+ ///
+ /// let num = 12.4_f64;
+ /// let inf = f64::INFINITY;
+ ///
+ /// assert_eq!(num.classify(), FpCategory::Normal);
+ /// assert_eq!(inf.classify(), FpCategory::Infinite);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn classify(self) -> FpCategory { num::Float::classify(self) }
+
+ /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
+ /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
+ /// The floating point encoding is documented in the [Reference][floating-point].
+ ///
+ /// ```
+ /// #![feature(float_extras)]
+ ///
+ /// let num = 2.0f64;
+ ///
+ /// // (8388608, -22, 1)
+ /// let (mantissa, exponent, sign) = num.integer_decode();
+ /// let sign_f = sign as f64;
+ /// let mantissa_f = mantissa as f64;
+ /// let exponent_f = num.powf(exponent as f64);
+ ///
+ /// // 1 * 8388608 * 2^(-22) == 2
+ /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ /// [floating-point]: ../reference.html#machine-types
+ #[unstable(feature = "float_extras", reason = "signature is undecided",
+ issue = "27752")]
+ #[rustc_deprecated(since = "1.11.0",
+ reason = "never really came to fruition and easily \
+ implementable outside the standard library")]
+ #[inline]
+ #[allow(deprecated)]
+ pub fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) }
+
+ /// Returns the largest integer less than or equal to a number.
+ ///
+ /// ```
+ /// let f = 3.99_f64;
+ /// let g = 3.0_f64;
+ ///
+ /// assert_eq!(f.floor(), 3.0);
+ /// assert_eq!(g.floor(), 3.0);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn floor(self) -> f64 {
+ unsafe { intrinsics::floorf64(self) }
+ }
+
+ /// Returns the smallest integer greater than or equal to a number.
+ ///
+ /// ```
+ /// let f = 3.01_f64;
+ /// let g = 4.0_f64;
+ ///
+ /// assert_eq!(f.ceil(), 4.0);
+ /// assert_eq!(g.ceil(), 4.0);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn ceil(self) -> f64 {
+ unsafe { intrinsics::ceilf64(self) }
+ }
+
+ /// Returns the nearest integer to a number. Round half-way cases away from
+ /// `0.0`.
+ ///
+ /// ```
+ /// let f = 3.3_f64;
+ /// let g = -3.3_f64;
+ ///
+ /// assert_eq!(f.round(), 3.0);
+ /// assert_eq!(g.round(), -3.0);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn round(self) -> f64 {
+ unsafe { intrinsics::roundf64(self) }
+ }
+
+ /// Returns the integer part of a number.
+ ///
+ /// ```
+ /// let f = 3.3_f64;
+ /// let g = -3.7_f64;
+ ///
+ /// assert_eq!(f.trunc(), 3.0);
+ /// assert_eq!(g.trunc(), -3.0);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn trunc(self) -> f64 {
+ unsafe { intrinsics::truncf64(self) }
+ }
+
+ /// Returns the fractional part of a number.
+ ///
+ /// ```
+ /// let x = 3.5_f64;
+ /// let y = -3.5_f64;
+ /// let abs_difference_x = (x.fract() - 0.5).abs();
+ /// let abs_difference_y = (y.fract() - (-0.5)).abs();
+ ///
+ /// assert!(abs_difference_x < 1e-10);
+ /// assert!(abs_difference_y < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn fract(self) -> f64 { self - self.trunc() }
+
+ /// Computes the absolute value of `self`. Returns `NAN` if the
+ /// number is `NAN`.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let x = 3.5_f64;
+ /// let y = -3.5_f64;
+ ///
+ /// let abs_difference_x = (x.abs() - x).abs();
+ /// let abs_difference_y = (y.abs() - (-y)).abs();
+ ///
+ /// assert!(abs_difference_x < 1e-10);
+ /// assert!(abs_difference_y < 1e-10);
+ ///
+ /// assert!(f64::NAN.abs().is_nan());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn abs(self) -> f64 { num::Float::abs(self) }
+
+ /// Returns a number that represents the sign of `self`.
+ ///
+ /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
+ /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
+ /// - `NAN` if the number is `NAN`
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let f = 3.5_f64;
+ ///
+ /// assert_eq!(f.signum(), 1.0);
+ /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
+ ///
+ /// assert!(f64::NAN.signum().is_nan());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn signum(self) -> f64 { num::Float::signum(self) }
+
+ /// Returns `true` if `self`'s sign bit is positive, including
+ /// `+0.0` and `INFINITY`.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let nan: f64 = f64::NAN;
+ ///
+ /// let f = 7.0_f64;
+ /// let g = -7.0_f64;
+ ///
+ /// assert!(f.is_sign_positive());
+ /// assert!(!g.is_sign_positive());
+ /// // Requires both tests to determine if is `NaN`
+ /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
+
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
+ #[inline]
+ pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) }
+
+ /// Returns `true` if `self`'s sign is negative, including `-0.0`
+ /// and `NEG_INFINITY`.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let nan = f64::NAN;
+ ///
+ /// let f = 7.0_f64;
+ /// let g = -7.0_f64;
+ ///
+ /// assert!(!f.is_sign_negative());
+ /// assert!(g.is_sign_negative());
+ /// // Requires both tests to determine if is `NaN`.
+ /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
+
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
+ #[inline]
+ pub fn is_negative(self) -> bool { num::Float::is_sign_negative(self) }
+
+ /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
+ /// error. This produces a more accurate result with better performance than
+ /// a separate multiplication operation followed by an add.
+ ///
+ /// ```
+ /// let m = 10.0_f64;
+ /// let x = 4.0_f64;
+ /// let b = 60.0_f64;
+ ///
+ /// // 100.0
+ /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn mul_add(self, a: f64, b: f64) -> f64 {
+ unsafe { intrinsics::fmaf64(self, a, b) }
+ }
+
+ /// Takes the reciprocal (inverse) of a number, `1/x`.
+ ///
+ /// ```
+ /// let x = 2.0_f64;
+ /// let abs_difference = (x.recip() - (1.0/x)).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn recip(self) -> f64 { num::Float::recip(self) }
+
+ /// Raises a number to an integer power.
+ ///
+ /// Using this function is generally faster than using `powf`
+ ///
+ /// ```
+ /// let x = 2.0_f64;
+ /// let abs_difference = (x.powi(2) - x*x).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) }
+
+ /// Raises a number to a floating point power.
+ ///
+ /// ```
+ /// let x = 2.0_f64;
+ /// let abs_difference = (x.powf(2.0) - x*x).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn powf(self, n: f64) -> f64 {
+ unsafe { intrinsics::powf64(self, n) }
+ }
+
+ /// Takes the square root of a number.
+ ///
+ /// Returns NaN if `self` is a negative number.
+ ///
+ /// ```
+ /// let positive = 4.0_f64;
+ /// let negative = -4.0_f64;
+ ///
+ /// let abs_difference = (positive.sqrt() - 2.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// assert!(negative.sqrt().is_nan());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn sqrt(self) -> f64 {
+ if self < 0.0 {
+ NAN
+ } else {
+ unsafe { intrinsics::sqrtf64(self) }
+ }
+ }
+
+ /// Returns `e^(self)`, (the exponential function).
+ ///
+ /// ```
+ /// let one = 1.0_f64;
+ /// // e^1
+ /// let e = one.exp();
+ ///
+ /// // ln(e) - 1 == 0
+ /// let abs_difference = (e.ln() - 1.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn exp(self) -> f64 {
+ unsafe { intrinsics::expf64(self) }
+ }
+
+ /// Returns `2^(self)`.
+ ///
+ /// ```
+ /// let f = 2.0_f64;
+ ///
+ /// // 2^2 - 4 == 0
+ /// let abs_difference = (f.exp2() - 4.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn exp2(self) -> f64 {
+ unsafe { intrinsics::exp2f64(self) }
+ }
+
+ /// Returns the natural logarithm of the number.
+ ///
+ /// ```
+ /// let one = 1.0_f64;
+ /// // e^1
+ /// let e = one.exp();
+ ///
+ /// // ln(e) - 1 == 0
+ /// let abs_difference = (e.ln() - 1.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn ln(self) -> f64 {
+ self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
+ }
+
+ /// Returns the logarithm of the number with respect to an arbitrary base.
+ ///
+ /// ```
+ /// let ten = 10.0_f64;
+ /// let two = 2.0_f64;
+ ///
+ /// // log10(10) - 1 == 0
+ /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
+ ///
+ /// // log2(2) - 1 == 0
+ /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
+ ///
+ /// assert!(abs_difference_10 < 1e-10);
+ /// assert!(abs_difference_2 < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
+
+ /// Returns the base 2 logarithm of the number.
+ ///
+ /// ```
+ /// let two = 2.0_f64;
+ ///
+ /// // log2(2) - 1 == 0
+ /// let abs_difference = (two.log2() - 1.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn log2(self) -> f64 {
+ self.log_wrapper(|n| {
+ #[cfg(target_os = "android")]
+ return ::sys::android::log2f64(n);
+ #[cfg(not(target_os = "android"))]
+ return unsafe { intrinsics::log2f64(n) };
+ })
+ }
+
+ /// Returns the base 10 logarithm of the number.
+ ///
+ /// ```
+ /// let ten = 10.0_f64;
+ ///
+ /// // log10(10) - 1 == 0
+ /// let abs_difference = (ten.log10() - 1.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn log10(self) -> f64 {
+ self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
+ }
+
+ /// Converts radians to degrees.
+ ///
+ /// ```
+ /// use std::f64::consts;
+ ///
+ /// let angle = consts::PI;
+ ///
+ /// let abs_difference = (angle.to_degrees() - 180.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) }
+
+ /// Converts degrees to radians.
+ ///
+ /// ```
+ /// use std::f64::consts;
+ ///
+ /// let angle = 180.0_f64;
+ ///
+ /// let abs_difference = (angle.to_radians() - consts::PI).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn to_radians(self) -> f64 { num::Float::to_radians(self) }
+
+ /// Constructs a floating point number of `x*2^exp`.
+ ///
+ /// ```
+ /// #![feature(float_extras)]
+ ///
+ /// // 3*2^2 - 12 == 0
+ /// let abs_difference = (f64::ldexp(3.0, 2) - 12.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[unstable(feature = "float_extras",
+ reason = "pending integer conventions",
+ issue = "27752")]
+ #[rustc_deprecated(since = "1.11.0",
+ reason = "never really came to fruition and easily \
+ implementable outside the standard library")]
+ #[inline]
+ pub fn ldexp(x: f64, exp: isize) -> f64 {
+ unsafe { cmath::ldexp(x, exp as c_int) }
+ }
+
+ /// Breaks the number into a normalized fraction and a base-2 exponent,
+ /// satisfying:
+ ///
+ /// * `self = x * 2^exp`
+ /// * `0.5 <= abs(x) < 1.0`
+ ///
+ /// ```
+ /// #![feature(float_extras)]
+ ///
+ /// let x = 4.0_f64;
+ ///
+ /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
+ /// let f = x.frexp();
+ /// let abs_difference_0 = (f.0 - 0.5).abs();
+ /// let abs_difference_1 = (f.1 as f64 - 3.0).abs();
+ ///
+ /// assert!(abs_difference_0 < 1e-10);
+ /// assert!(abs_difference_1 < 1e-10);
+ /// ```
+ #[unstable(feature = "float_extras",
+ reason = "pending integer conventions",
+ issue = "27752")]
+ #[rustc_deprecated(since = "1.11.0",
+ reason = "never really came to fruition and easily \
+ implementable outside the standard library")]
+ #[inline]
+ pub fn frexp(self) -> (f64, isize) {
+ unsafe {
+ let mut exp = 0;
+ let x = cmath::frexp(self, &mut exp);
+ (x, exp as isize)
+ }
+ }
+
+ /// Returns the next representable floating-point value in the direction of
+ /// `other`.
+ ///
+ /// ```
+ /// #![feature(float_extras)]
+ ///
+ /// let x = 1.0f64;
+ ///
+ /// let abs_diff = (x.next_after(2.0) - 1.0000000000000002220446049250313_f64).abs();
+ ///
+ /// assert!(abs_diff < 1e-10);
+ /// ```
+ #[unstable(feature = "float_extras",
+ reason = "unsure about its place in the world",
+ issue = "27752")]
+ #[rustc_deprecated(since = "1.11.0",
+ reason = "never really came to fruition and easily \
+ implementable outside the standard library")]
+ #[inline]
+ pub fn next_after(self, other: f64) -> f64 {
+ unsafe { cmath::nextafter(self, other) }
+ }
+
+ /// Returns the maximum of the two numbers.
+ ///
+ /// ```
+ /// let x = 1.0_f64;
+ /// let y = 2.0_f64;
+ ///
+ /// assert_eq!(x.max(y), y);
+ /// ```
+ ///
+ /// If one of the arguments is NaN, then the other argument is returned.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn max(self, other: f64) -> f64 {
+ unsafe { cmath::fmax(self, other) }
+ }
+
+ /// Returns the minimum of the two numbers.
+ ///
+ /// ```
+ /// let x = 1.0_f64;
+ /// let y = 2.0_f64;
+ ///
+ /// assert_eq!(x.min(y), x);
+ /// ```
+ ///
+ /// If one of the arguments is NaN, then the other argument is returned.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn min(self, other: f64) -> f64 {
+ unsafe { cmath::fmin(self, other) }
+ }
+
+ /// The positive difference of two numbers.
+ ///
+ /// * If `self <= other`: `0:0`
+ /// * Else: `self - other`
+ ///
+ /// ```
+ /// let x = 3.0_f64;
+ /// let y = -3.0_f64;
+ ///
+ /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
+ /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
+ ///
+ /// assert!(abs_difference_x < 1e-10);
+ /// assert!(abs_difference_y < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ #[rustc_deprecated(since = "1.10.0",
+ reason = "you probably meant `(self - other).abs()`: \
+ this operation is `(self - other).max(0.0)` (also \
+ known as `fdim` in C). If you truly need the positive \
+ difference, consider using that expression or the C function \
+ `fdim`, depending on how you wish to handle NaN (please consider \
+ filing an issue describing your use-case too).")]
+ pub fn abs_sub(self, other: f64) -> f64 {
+ unsafe { cmath::fdim(self, other) }
+ }
+
+ /// Takes the cubic root of a number.
+ ///
+ /// ```
+ /// let x = 8.0_f64;
+ ///
+ /// // x^(1/3) - 2 == 0
+ /// let abs_difference = (x.cbrt() - 2.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn cbrt(self) -> f64 {
+ unsafe { cmath::cbrt(self) }
+ }
+
+ /// Calculates the length of the hypotenuse of a right-angle triangle given
+ /// legs of length `x` and `y`.
+ ///
+ /// ```
+ /// let x = 2.0_f64;
+ /// let y = 3.0_f64;
+ ///
+ /// // sqrt(x^2 + y^2)
+ /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn hypot(self, other: f64) -> f64 {
+ unsafe { cmath::hypot(self, other) }
+ }
+
+ /// Computes the sine of a number (in radians).
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let x = f64::consts::PI/2.0;
+ ///
+ /// let abs_difference = (x.sin() - 1.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn sin(self) -> f64 {
+ unsafe { intrinsics::sinf64(self) }
+ }
+
+ /// Computes the cosine of a number (in radians).
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let x = 2.0*f64::consts::PI;
+ ///
+ /// let abs_difference = (x.cos() - 1.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn cos(self) -> f64 {
+ unsafe { intrinsics::cosf64(self) }
+ }
+
+ /// Computes the tangent of a number (in radians).
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let x = f64::consts::PI/4.0;
+ /// let abs_difference = (x.tan() - 1.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-14);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn tan(self) -> f64 {
+ unsafe { cmath::tan(self) }
+ }
+
+ /// Computes the arcsine of a number. Return value is in radians in
+ /// the range [-pi/2, pi/2] or NaN if the number is outside the range
+ /// [-1, 1].
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let f = f64::consts::PI / 2.0;
+ ///
+ /// // asin(sin(pi/2))
+ /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn asin(self) -> f64 {
+ unsafe { cmath::asin(self) }
+ }
+
+ /// Computes the arccosine of a number. Return value is in radians in
+ /// the range [0, pi] or NaN if the number is outside the range
+ /// [-1, 1].
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let f = f64::consts::PI / 4.0;
+ ///
+ /// // acos(cos(pi/4))
+ /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn acos(self) -> f64 {
+ unsafe { cmath::acos(self) }
+ }
+
+ /// Computes the arctangent of a number. Return value is in radians in the
+ /// range [-pi/2, pi/2];
+ ///
+ /// ```
+ /// let f = 1.0_f64;
+ ///
+ /// // atan(tan(1))
+ /// let abs_difference = (f.tan().atan() - 1.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn atan(self) -> f64 {
+ unsafe { cmath::atan(self) }
+ }
+
+ /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
+ ///
+ /// * `x = 0`, `y = 0`: `0`
+ /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
+ /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
+ /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let pi = f64::consts::PI;
+ /// // All angles from horizontal right (+x)
+ /// // 45 deg counter-clockwise
+ /// let x1 = 3.0_f64;
+ /// let y1 = -3.0_f64;
+ ///
+ /// // 135 deg clockwise
+ /// let x2 = -3.0_f64;
+ /// let y2 = 3.0_f64;
+ ///
+ /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
+ /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
+ ///
+ /// assert!(abs_difference_1 < 1e-10);
+ /// assert!(abs_difference_2 < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn atan2(self, other: f64) -> f64 {
+ unsafe { cmath::atan2(self, other) }
+ }
+
+ /// Simultaneously computes the sine and cosine of the number, `x`. Returns
+ /// `(sin(x), cos(x))`.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let x = f64::consts::PI/4.0;
+ /// let f = x.sin_cos();
+ ///
+ /// let abs_difference_0 = (f.0 - x.sin()).abs();
+ /// let abs_difference_1 = (f.1 - x.cos()).abs();
+ ///
+ /// assert!(abs_difference_0 < 1e-10);
+ /// assert!(abs_difference_1 < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn sin_cos(self) -> (f64, f64) {
+ (self.sin(), self.cos())
+ }
+
+ /// Returns `e^(self) - 1` in a way that is accurate even if the
+ /// number is close to zero.
+ ///
+ /// ```
+ /// let x = 7.0_f64;
+ ///
+ /// // e^(ln(7)) - 1
+ /// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn exp_m1(self) -> f64 {
+ unsafe { cmath::expm1(self) }
+ }
+
+ /// Returns `ln(1+n)` (natural logarithm) more accurately than if
+ /// the operations were performed separately.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let x = f64::consts::E - 1.0;
+ ///
+ /// // ln(1 + (e - 1)) == ln(e) == 1
+ /// let abs_difference = (x.ln_1p() - 1.0).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn ln_1p(self) -> f64 {
+ unsafe { cmath::log1p(self) }
+ }
+
+ /// Hyperbolic sine function.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let e = f64::consts::E;
+ /// let x = 1.0_f64;
+ ///
+ /// let f = x.sinh();
+ /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
+ /// let g = (e*e - 1.0)/(2.0*e);
+ /// let abs_difference = (f - g).abs();
+ ///
+ /// assert!(abs_difference < 1e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn sinh(self) -> f64 {
+ unsafe { cmath::sinh(self) }
+ }
+
+ /// Hyperbolic cosine function.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let e = f64::consts::E;
+ /// let x = 1.0_f64;
+ /// let f = x.cosh();
+ /// // Solving cosh() at 1 gives this result
+ /// let g = (e*e + 1.0)/(2.0*e);
+ /// let abs_difference = (f - g).abs();
+ ///
+ /// // Same result
+ /// assert!(abs_difference < 1.0e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn cosh(self) -> f64 {
+ unsafe { cmath::cosh(self) }
+ }
+
+ /// Hyperbolic tangent function.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let e = f64::consts::E;
+ /// let x = 1.0_f64;
+ ///
+ /// let f = x.tanh();
+ /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
+ /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
+ /// let abs_difference = (f - g).abs();
+ ///
+ /// assert!(abs_difference < 1.0e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn tanh(self) -> f64 {
+ unsafe { cmath::tanh(self) }
+ }
+
+ /// Inverse hyperbolic sine function.
+ ///
+ /// ```
+ /// let x = 1.0_f64;
+ /// let f = x.sinh().asinh();
+ ///
+ /// let abs_difference = (f - x).abs();
+ ///
+ /// assert!(abs_difference < 1.0e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn asinh(self) -> f64 {
+ if self == NEG_INFINITY {
+ NEG_INFINITY
+ } else {
+ (self + ((self * self) + 1.0).sqrt()).ln()
+ }
+ }
+
+ /// Inverse hyperbolic cosine function.
+ ///
+ /// ```
+ /// let x = 1.0_f64;
+ /// let f = x.cosh().acosh();
+ ///
+ /// let abs_difference = (f - x).abs();
+ ///
+ /// assert!(abs_difference < 1.0e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn acosh(self) -> f64 {
+ match self {
+ x if x < 1.0 => NAN,
+ x => (x + ((x * x) - 1.0).sqrt()).ln(),
+ }
+ }
+
+ /// Inverse hyperbolic tangent function.
+ ///
+ /// ```
+ /// use std::f64;
+ ///
+ /// let e = f64::consts::E;
+ /// let f = e.tanh().atanh();
+ ///
+ /// let abs_difference = (f - e).abs();
+ ///
+ /// assert!(abs_difference < 1.0e-10);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[inline]
+ pub fn atanh(self) -> f64 {
+ 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
+ }
+
+ // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
+ // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
+ // of expected NaN).
+ fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
+ if !cfg!(target_os = "solaris") {
+ log_fn(self)
+ } else {
+ if self.is_finite() {
+ if self > 0.0 {
+ log_fn(self)
+ } else if self == 0.0 {
+ NEG_INFINITY // log(0) = -Inf
+ } else {
+ NAN // log(-n) = NaN
+ }
+ } else if self.is_nan() {
+ self // log(NaN) = NaN
+ } else if self > 0.0 {
+ self // log(Inf) = Inf
+ } else {
+ NAN // log(-Inf) = NaN
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use f64;
+ use f64::*;
+ use num::*;
+ use num::FpCategory as Fp;
+
+ #[test]
+ fn test_num_f64() {
+ test_num(10f64, 2f64);
+ }
+
+ #[test]
+ fn test_min_nan() {
+ assert_eq!(NAN.min(2.0), 2.0);
+ assert_eq!(2.0f64.min(NAN), 2.0);
+ }
+
+ #[test]
+ fn test_max_nan() {
+ assert_eq!(NAN.max(2.0), 2.0);
+ assert_eq!(2.0f64.max(NAN), 2.0);
+ }
+
+ #[test]
+ fn test_nan() {
+ let nan: f64 = NAN;
+ assert!(nan.is_nan());
+ assert!(!nan.is_infinite());
+ assert!(!nan.is_finite());
+ assert!(!nan.is_normal());
+ assert!(!nan.is_sign_positive());
+ assert!(!nan.is_sign_negative());
+ assert_eq!(Fp::Nan, nan.classify());
+ }
+
+ #[test]
+ fn test_infinity() {
+ let inf: f64 = INFINITY;
+ assert!(inf.is_infinite());
+ assert!(!inf.is_finite());
+ assert!(inf.is_sign_positive());
+ assert!(!inf.is_sign_negative());
+ assert!(!inf.is_nan());
+ assert!(!inf.is_normal());
+ assert_eq!(Fp::Infinite, inf.classify());
+ }
+
+ #[test]
+ fn test_neg_infinity() {
+ let neg_inf: f64 = NEG_INFINITY;
+ assert!(neg_inf.is_infinite());
+ assert!(!neg_inf.is_finite());
+ assert!(!neg_inf.is_sign_positive());
+ assert!(neg_inf.is_sign_negative());
+ assert!(!neg_inf.is_nan());
+ assert!(!neg_inf.is_normal());
+ assert_eq!(Fp::Infinite, neg_inf.classify());
+ }
+
+ #[test]
+ fn test_zero() {
+ let zero: f64 = 0.0f64;
+ assert_eq!(0.0, zero);
+ assert!(!zero.is_infinite());
+ assert!(zero.is_finite());
+ assert!(zero.is_sign_positive());
+ assert!(!zero.is_sign_negative());
+ assert!(!zero.is_nan());
+ assert!(!zero.is_normal());
+ assert_eq!(Fp::Zero, zero.classify());
+ }
+
+ #[test]
+ fn test_neg_zero() {
+ let neg_zero: f64 = -0.0;
+ assert_eq!(0.0, neg_zero);
+ assert!(!neg_zero.is_infinite());
+ assert!(neg_zero.is_finite());
+ assert!(!neg_zero.is_sign_positive());
+ assert!(neg_zero.is_sign_negative());
+ assert!(!neg_zero.is_nan());
+ assert!(!neg_zero.is_normal());
+ assert_eq!(Fp::Zero, neg_zero.classify());
+ }
+
+ #[test]
+ fn test_one() {
+ let one: f64 = 1.0f64;
+ assert_eq!(1.0, one);
+ assert!(!one.is_infinite());
+ assert!(one.is_finite());
+ assert!(one.is_sign_positive());
+ assert!(!one.is_sign_negative());
+ assert!(!one.is_nan());
+ assert!(one.is_normal());
+ assert_eq!(Fp::Normal, one.classify());
+ }
+
+ #[test]
+ fn test_is_nan() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert!(nan.is_nan());
+ assert!(!0.0f64.is_nan());
+ assert!(!5.3f64.is_nan());
+ assert!(!(-10.732f64).is_nan());
+ assert!(!inf.is_nan());
+ assert!(!neg_inf.is_nan());
+ }
+
+ #[test]
+ fn test_is_infinite() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert!(!nan.is_infinite());
+ assert!(inf.is_infinite());
+ assert!(neg_inf.is_infinite());
+ assert!(!0.0f64.is_infinite());
+ assert!(!42.8f64.is_infinite());
+ assert!(!(-109.2f64).is_infinite());
+ }
+
+ #[test]
+ fn test_is_finite() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert!(!nan.is_finite());
+ assert!(!inf.is_finite());
+ assert!(!neg_inf.is_finite());
+ assert!(0.0f64.is_finite());
+ assert!(42.8f64.is_finite());
+ assert!((-109.2f64).is_finite());
+ }
+
+ #[test]
+ fn test_is_normal() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ let zero: f64 = 0.0f64;
+ let neg_zero: f64 = -0.0;
+ assert!(!nan.is_normal());
+ assert!(!inf.is_normal());
+ assert!(!neg_inf.is_normal());
+ assert!(!zero.is_normal());
+ assert!(!neg_zero.is_normal());
+ assert!(1f64.is_normal());
+ assert!(1e-307f64.is_normal());
+ assert!(!1e-308f64.is_normal());
+ }
+
+ #[test]
+ fn test_classify() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ let zero: f64 = 0.0f64;
+ let neg_zero: f64 = -0.0;
+ assert_eq!(nan.classify(), Fp::Nan);
+ assert_eq!(inf.classify(), Fp::Infinite);
+ assert_eq!(neg_inf.classify(), Fp::Infinite);
+ assert_eq!(zero.classify(), Fp::Zero);
+ assert_eq!(neg_zero.classify(), Fp::Zero);
+ assert_eq!(1e-307f64.classify(), Fp::Normal);
+ assert_eq!(1e-308f64.classify(), Fp::Subnormal);
+ }
+
+ #[test]
+ #[allow(deprecated)]
+ fn test_integer_decode() {
+ assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1));
+ assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1));
+ assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1));
+ assert_eq!(0f64.integer_decode(), (0, -1075, 1));
+ assert_eq!((-0f64).integer_decode(), (0, -1075, -1));
+ assert_eq!(INFINITY.integer_decode(), (4503599627370496, 972, 1));
+ assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1));
+
+ // Ignore the "sign" (quiet / signalling flag) of NAN.
+ // It can vary between runtime operations and LLVM folding.
+ let (nan_m, nan_e, _nan_s) = NAN.integer_decode();
+ assert_eq!((nan_m, nan_e), (6755399441055744, 972));
+ }
+
+ #[test]
+ fn test_floor() {
+ assert_approx_eq!(1.0f64.floor(), 1.0f64);
+ assert_approx_eq!(1.3f64.floor(), 1.0f64);
+ assert_approx_eq!(1.5f64.floor(), 1.0f64);
+ assert_approx_eq!(1.7f64.floor(), 1.0f64);
+ assert_approx_eq!(0.0f64.floor(), 0.0f64);
+ assert_approx_eq!((-0.0f64).floor(), -0.0f64);
+ assert_approx_eq!((-1.0f64).floor(), -1.0f64);
+ assert_approx_eq!((-1.3f64).floor(), -2.0f64);
+ assert_approx_eq!((-1.5f64).floor(), -2.0f64);
+ assert_approx_eq!((-1.7f64).floor(), -2.0f64);
+ }
+
+ #[test]
+ fn test_ceil() {
+ assert_approx_eq!(1.0f64.ceil(), 1.0f64);
+ assert_approx_eq!(1.3f64.ceil(), 2.0f64);
+ assert_approx_eq!(1.5f64.ceil(), 2.0f64);
+ assert_approx_eq!(1.7f64.ceil(), 2.0f64);
+ assert_approx_eq!(0.0f64.ceil(), 0.0f64);
+ assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
+ assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
+ assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
+ assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
+ assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
+ }
+
+ #[test]
+ fn test_round() {
+ assert_approx_eq!(1.0f64.round(), 1.0f64);
+ assert_approx_eq!(1.3f64.round(), 1.0f64);
+ assert_approx_eq!(1.5f64.round(), 2.0f64);
+ assert_approx_eq!(1.7f64.round(), 2.0f64);
+ assert_approx_eq!(0.0f64.round(), 0.0f64);
+ assert_approx_eq!((-0.0f64).round(), -0.0f64);
+ assert_approx_eq!((-1.0f64).round(), -1.0f64);
+ assert_approx_eq!((-1.3f64).round(), -1.0f64);
+ assert_approx_eq!((-1.5f64).round(), -2.0f64);
+ assert_approx_eq!((-1.7f64).round(), -2.0f64);
+ }
+
+ #[test]
+ fn test_trunc() {
+ assert_approx_eq!(1.0f64.trunc(), 1.0f64);
+ assert_approx_eq!(1.3f64.trunc(), 1.0f64);
+ assert_approx_eq!(1.5f64.trunc(), 1.0f64);
+ assert_approx_eq!(1.7f64.trunc(), 1.0f64);
+ assert_approx_eq!(0.0f64.trunc(), 0.0f64);
+ assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
+ assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
+ assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
+ assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
+ assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
+ }
+
+ #[test]
+ fn test_fract() {
+ assert_approx_eq!(1.0f64.fract(), 0.0f64);
+ assert_approx_eq!(1.3f64.fract(), 0.3f64);
+ assert_approx_eq!(1.5f64.fract(), 0.5f64);
+ assert_approx_eq!(1.7f64.fract(), 0.7f64);
+ assert_approx_eq!(0.0f64.fract(), 0.0f64);
+ assert_approx_eq!((-0.0f64).fract(), -0.0f64);
+ assert_approx_eq!((-1.0f64).fract(), -0.0f64);
+ assert_approx_eq!((-1.3f64).fract(), -0.3f64);
+ assert_approx_eq!((-1.5f64).fract(), -0.5f64);
+ assert_approx_eq!((-1.7f64).fract(), -0.7f64);
+ }
+
+ #[test]
+ fn test_abs() {
+ assert_eq!(INFINITY.abs(), INFINITY);
+ assert_eq!(1f64.abs(), 1f64);
+ assert_eq!(0f64.abs(), 0f64);
+ assert_eq!((-0f64).abs(), 0f64);
+ assert_eq!((-1f64).abs(), 1f64);
+ assert_eq!(NEG_INFINITY.abs(), INFINITY);
+ assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
+ assert!(NAN.abs().is_nan());
+ }
+
+ #[test]
+ fn test_signum() {
+ assert_eq!(INFINITY.signum(), 1f64);
+ assert_eq!(1f64.signum(), 1f64);
+ assert_eq!(0f64.signum(), 1f64);
+ assert_eq!((-0f64).signum(), -1f64);
+ assert_eq!((-1f64).signum(), -1f64);
+ assert_eq!(NEG_INFINITY.signum(), -1f64);
+ assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
+ assert!(NAN.signum().is_nan());
+ }
+
+ #[test]
+ fn test_is_sign_positive() {
+ assert!(INFINITY.is_sign_positive());
+ assert!(1f64.is_sign_positive());
+ assert!(0f64.is_sign_positive());
+ assert!(!(-0f64).is_sign_positive());
+ assert!(!(-1f64).is_sign_positive());
+ assert!(!NEG_INFINITY.is_sign_positive());
+ assert!(!(1f64/NEG_INFINITY).is_sign_positive());
+ assert!(!NAN.is_sign_positive());
+ }
+
+ #[test]
+ fn test_is_sign_negative() {
+ assert!(!INFINITY.is_sign_negative());
+ assert!(!1f64.is_sign_negative());
+ assert!(!0f64.is_sign_negative());
+ assert!((-0f64).is_sign_negative());
+ assert!((-1f64).is_sign_negative());
+ assert!(NEG_INFINITY.is_sign_negative());
+ assert!((1f64/NEG_INFINITY).is_sign_negative());
+ assert!(!NAN.is_sign_negative());
+ }
+
+ #[test]
+ fn test_mul_add() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05);
+ assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65);
+ assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2);
+ assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6);
+ assert!(nan.mul_add(7.8, 9.0).is_nan());
+ assert_eq!(inf.mul_add(7.8, 9.0), inf);
+ assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
+ assert_eq!(8.9f64.mul_add(inf, 3.2), inf);
+ assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
+ }
+
+ #[test]
+ fn test_recip() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert_eq!(1.0f64.recip(), 1.0);
+ assert_eq!(2.0f64.recip(), 0.5);
+ assert_eq!((-0.4f64).recip(), -2.5);
+ assert_eq!(0.0f64.recip(), inf);
+ assert!(nan.recip().is_nan());
+ assert_eq!(inf.recip(), 0.0);
+ assert_eq!(neg_inf.recip(), 0.0);
+ }
+
+ #[test]
+ fn test_powi() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert_eq!(1.0f64.powi(1), 1.0);
+ assert_approx_eq!((-3.1f64).powi(2), 9.61);
+ assert_approx_eq!(5.9f64.powi(-2), 0.028727);
+ assert_eq!(8.3f64.powi(0), 1.0);
+ assert!(nan.powi(2).is_nan());
+ assert_eq!(inf.powi(3), inf);
+ assert_eq!(neg_inf.powi(2), inf);
+ }
+
+ #[test]
+ fn test_powf() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert_eq!(1.0f64.powf(1.0), 1.0);
+ assert_approx_eq!(3.4f64.powf(4.5), 246.408183);
+ assert_approx_eq!(2.7f64.powf(-3.2), 0.041652);
+ assert_approx_eq!((-3.1f64).powf(2.0), 9.61);
+ assert_approx_eq!(5.9f64.powf(-2.0), 0.028727);
+ assert_eq!(8.3f64.powf(0.0), 1.0);
+ assert!(nan.powf(2.0).is_nan());
+ assert_eq!(inf.powf(2.0), inf);
+ assert_eq!(neg_inf.powf(3.0), neg_inf);
+ }
+
+ #[test]
+ fn test_sqrt_domain() {
+ assert!(NAN.sqrt().is_nan());
+ assert!(NEG_INFINITY.sqrt().is_nan());
+ assert!((-1.0f64).sqrt().is_nan());
+ assert_eq!((-0.0f64).sqrt(), -0.0);
+ assert_eq!(0.0f64.sqrt(), 0.0);
+ assert_eq!(1.0f64.sqrt(), 1.0);
+ assert_eq!(INFINITY.sqrt(), INFINITY);
+ }
+
+ #[test]
+ fn test_exp() {
+ assert_eq!(1.0, 0.0f64.exp());
+ assert_approx_eq!(2.718282, 1.0f64.exp());
+ assert_approx_eq!(148.413159, 5.0f64.exp());
+
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ let nan: f64 = NAN;
+ assert_eq!(inf, inf.exp());
+ assert_eq!(0.0, neg_inf.exp());
+ assert!(nan.exp().is_nan());
+ }
+
+ #[test]
+ fn test_exp2() {
+ assert_eq!(32.0, 5.0f64.exp2());
+ assert_eq!(1.0, 0.0f64.exp2());
+
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ let nan: f64 = NAN;
+ assert_eq!(inf, inf.exp2());
+ assert_eq!(0.0, neg_inf.exp2());
+ assert!(nan.exp2().is_nan());
+ }
+
+ #[test]
+ fn test_ln() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert_approx_eq!(1.0f64.exp().ln(), 1.0);
+ assert!(nan.ln().is_nan());
+ assert_eq!(inf.ln(), inf);
+ assert!(neg_inf.ln().is_nan());
+ assert!((-2.3f64).ln().is_nan());
+ assert_eq!((-0.0f64).ln(), neg_inf);
+ assert_eq!(0.0f64.ln(), neg_inf);
+ assert_approx_eq!(4.0f64.ln(), 1.386294);
+ }
+
+ #[test]
+ fn test_log() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert_eq!(10.0f64.log(10.0), 1.0);
+ assert_approx_eq!(2.3f64.log(3.5), 0.664858);
+ assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0);
+ assert!(1.0f64.log(1.0).is_nan());
+ assert!(1.0f64.log(-13.9).is_nan());
+ assert!(nan.log(2.3).is_nan());
+ assert_eq!(inf.log(10.0), inf);
+ assert!(neg_inf.log(8.8).is_nan());
+ assert!((-2.3f64).log(0.1).is_nan());
+ assert_eq!((-0.0f64).log(2.0), neg_inf);
+ assert_eq!(0.0f64.log(7.0), neg_inf);
+ }
+
+ #[test]
+ fn test_log2() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert_approx_eq!(10.0f64.log2(), 3.321928);
+ assert_approx_eq!(2.3f64.log2(), 1.201634);
+ assert_approx_eq!(1.0f64.exp().log2(), 1.442695);
+ assert!(nan.log2().is_nan());
+ assert_eq!(inf.log2(), inf);
+ assert!(neg_inf.log2().is_nan());
+ assert!((-2.3f64).log2().is_nan());
+ assert_eq!((-0.0f64).log2(), neg_inf);
+ assert_eq!(0.0f64.log2(), neg_inf);
+ }
+
+ #[test]
+ fn test_log10() {
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert_eq!(10.0f64.log10(), 1.0);
+ assert_approx_eq!(2.3f64.log10(), 0.361728);
+ assert_approx_eq!(1.0f64.exp().log10(), 0.434294);
+ assert_eq!(1.0f64.log10(), 0.0);
+ assert!(nan.log10().is_nan());
+ assert_eq!(inf.log10(), inf);
+ assert!(neg_inf.log10().is_nan());
+ assert!((-2.3f64).log10().is_nan());
+ assert_eq!((-0.0f64).log10(), neg_inf);
+ assert_eq!(0.0f64.log10(), neg_inf);
+ }
+
+ #[test]
+ fn test_to_degrees() {
+ let pi: f64 = consts::PI;
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert_eq!(0.0f64.to_degrees(), 0.0);
+ assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
+ assert_eq!(pi.to_degrees(), 180.0);
+ assert!(nan.to_degrees().is_nan());
+ assert_eq!(inf.to_degrees(), inf);
+ assert_eq!(neg_inf.to_degrees(), neg_inf);
+ }
+
+ #[test]
+ fn test_to_radians() {
+ let pi: f64 = consts::PI;
+ let nan: f64 = NAN;
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ assert_eq!(0.0f64.to_radians(), 0.0);
+ assert_approx_eq!(154.6f64.to_radians(), 2.698279);
+ assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
+ assert_eq!(180.0f64.to_radians(), pi);
+ assert!(nan.to_radians().is_nan());
+ assert_eq!(inf.to_radians(), inf);
+ assert_eq!(neg_inf.to_radians(), neg_inf);
+ }
+
+ #[test]
+ #[allow(deprecated)]
+ fn test_ldexp() {
+ let f1 = 2.0f64.powi(-123);
+ let f2 = 2.0f64.powi(-111);
+ let f3 = 1.75 * 2.0f64.powi(-12);
+ assert_eq!(f64::ldexp(1f64, -123), f1);
+ assert_eq!(f64::ldexp(1f64, -111), f2);
+ assert_eq!(f64::ldexp(1.75f64, -12), f3);
+
+ assert_eq!(f64::ldexp(0f64, -123), 0f64);
+ assert_eq!(f64::ldexp(-0f64, -123), -0f64);
+
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ let nan: f64 = NAN;
+ assert_eq!(f64::ldexp(inf, -123), inf);
+ assert_eq!(f64::ldexp(neg_inf, -123), neg_inf);
+ assert!(f64::ldexp(nan, -123).is_nan());
+ }
+
+ #[test]
+ #[allow(deprecated)]
+ fn test_frexp() {
+ let f1 = 2.0f64.powi(-123);
+ let f2 = 2.0f64.powi(-111);
+ let f3 = 1.75 * 2.0f64.powi(-123);
+ let (x1, exp1) = f1.frexp();
+ let (x2, exp2) = f2.frexp();
+ let (x3, exp3) = f3.frexp();
+ assert_eq!((x1, exp1), (0.5f64, -122));
+ assert_eq!((x2, exp2), (0.5f64, -110));
+ assert_eq!((x3, exp3), (0.875f64, -122));
+ assert_eq!(f64::ldexp(x1, exp1), f1);
+ assert_eq!(f64::ldexp(x2, exp2), f2);
+ assert_eq!(f64::ldexp(x3, exp3), f3);
+
+ assert_eq!(0f64.frexp(), (0f64, 0));
+ assert_eq!((-0f64).frexp(), (-0f64, 0));
+ }
+
+ #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
+ #[allow(deprecated)]
+ fn test_frexp_nowin() {
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ let nan: f64 = NAN;
+ assert_eq!(match inf.frexp() { (x, _) => x }, inf);
+ assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
+ assert!(match nan.frexp() { (x, _) => x.is_nan() })
+ }
+
+ #[test]
+ fn test_asinh() {
+ assert_eq!(0.0f64.asinh(), 0.0f64);
+ assert_eq!((-0.0f64).asinh(), -0.0f64);
+
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ let nan: f64 = NAN;
+ assert_eq!(inf.asinh(), inf);
+ assert_eq!(neg_inf.asinh(), neg_inf);
+ assert!(nan.asinh().is_nan());
+ assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
+ assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
+ }
+
+ #[test]
+ fn test_acosh() {
+ assert_eq!(1.0f64.acosh(), 0.0f64);
+ assert!(0.999f64.acosh().is_nan());
+
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ let nan: f64 = NAN;
+ assert_eq!(inf.acosh(), inf);
+ assert!(neg_inf.acosh().is_nan());
+ assert!(nan.acosh().is_nan());
+ assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
+ assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
+ }
+
+ #[test]
+ fn test_atanh() {
+ assert_eq!(0.0f64.atanh(), 0.0f64);
+ assert_eq!((-0.0f64).atanh(), -0.0f64);
+
+ let inf: f64 = INFINITY;
+ let neg_inf: f64 = NEG_INFINITY;
+ let nan: f64 = NAN;
+ assert_eq!(1.0f64.atanh(), inf);
+ assert_eq!((-1.0f64).atanh(), neg_inf);
+ assert!(2f64.atanh().atanh().is_nan());
+ assert!((-2f64).atanh().atanh().is_nan());
+ assert!(inf.atanh().is_nan());
+ assert!(neg_inf.atanh().is_nan());
+ assert!(nan.atanh().is_nan());
+ assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
+ assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
+ }
+
+ #[test]
+ fn test_real_consts() {
+ use super::consts;
+ let pi: f64 = consts::PI;
+ let frac_pi_2: f64 = consts::FRAC_PI_2;
+ let frac_pi_3: f64 = consts::FRAC_PI_3;
+ let frac_pi_4: f64 = consts::FRAC_PI_4;
+ let frac_pi_6: f64 = consts::FRAC_PI_6;
+ let frac_pi_8: f64 = consts::FRAC_PI_8;
+ let frac_1_pi: f64 = consts::FRAC_1_PI;
+ let frac_2_pi: f64 = consts::FRAC_2_PI;
+ let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI;
+ let sqrt2: f64 = consts::SQRT_2;
+ let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2;
+ let e: f64 = consts::E;
+ let log2_e: f64 = consts::LOG2_E;
+ let log10_e: f64 = consts::LOG10_E;
+ let ln_2: f64 = consts::LN_2;
+ let ln_10: f64 = consts::LN_10;
+
+ assert_approx_eq!(frac_pi_2, pi / 2f64);
+ assert_approx_eq!(frac_pi_3, pi / 3f64);
+ assert_approx_eq!(frac_pi_4, pi / 4f64);
+ assert_approx_eq!(frac_pi_6, pi / 6f64);
+ assert_approx_eq!(frac_pi_8, pi / 8f64);
+ assert_approx_eq!(frac_1_pi, 1f64 / pi);
+ assert_approx_eq!(frac_2_pi, 2f64 / pi);
+ assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt());
+ assert_approx_eq!(sqrt2, 2f64.sqrt());
+ assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt());
+ assert_approx_eq!(log2_e, e.log2());
+ assert_approx_eq!(log10_e, e.log10());
+ assert_approx_eq!(ln_2, 2f64.ln());
+ assert_approx_eq!(ln_10, 10f64.ln());
+ }
+}
diff --git a/ctr-std/src/ffi/c_str.rs b/ctr-std/src/ffi/c_str.rs
new file mode 100644
index 0000000..ad40660
--- /dev/null
+++ b/ctr-std/src/ffi/c_str.rs
@@ -0,0 +1,849 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use ascii;
+use borrow::{Cow, Borrow};
+use cmp::Ordering;
+use error::Error;
+use fmt::{self, Write};
+use io;
+use libc::{self, c_char};
+use mem;
+use memchr;
+use ops;
+use ptr;
+use slice;
+use str::{self, Utf8Error};
+
+/// A type representing an owned C-compatible string
+///
+/// This type serves the primary purpose of being able to safely generate a
+/// C-compatible string from a Rust byte slice or vector. An instance of this
+/// type is a static guarantee that the underlying bytes contain no interior 0
+/// bytes and the final byte is 0.
+///
+/// A `CString` is created from either a byte slice or a byte vector. After
+/// being created, a `CString` predominately inherits all of its methods from
+/// the `Deref` implementation to `[c_char]`. Note that the underlying array
+/// is represented as an array of `c_char` as opposed to `u8`. A `u8` slice
+/// can be obtained with the `as_bytes` method. Slices produced from a `CString`
+/// do *not* contain the trailing nul terminator unless otherwise specified.
+///
+/// # Examples
+///
+/// ```no_run
+/// # fn main() {
+/// use std::ffi::CString;
+/// use std::os::raw::c_char;
+///
+/// extern {
+/// fn my_printer(s: *const c_char);
+/// }
+///
+/// let c_to_print = CString::new("Hello, world!").unwrap();
+/// unsafe {
+/// my_printer(c_to_print.as_ptr());
+/// }
+/// # }
+/// ```
+///
+/// # Safety
+///
+/// `CString` is intended for working with traditional C-style strings
+/// (a sequence of non-null bytes terminated by a single null byte); the
+/// primary use case for these kinds of strings is interoperating with C-like
+/// code. Often you will need to transfer ownership to/from that external
+/// code. It is strongly recommended that you thoroughly read through the
+/// documentation of `CString` before use, as improper ownership management
+/// of `CString` instances can lead to invalid memory accesses, memory leaks,
+/// and other memory errors.
+
+#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct CString {
+ // Invariant 1: the slice ends with a zero byte and has a length of at least one.
+ // Invariant 2: the slice contains only one zero byte.
+ // Improper usage of unsafe function can break Invariant 2, but not Invariant 1.
+ inner: Box<[u8]>,
+}
+
+/// Representation of a borrowed C string.
+///
+/// This dynamically sized type is only safely constructed via a borrowed
+/// version of an instance of `CString`. This type can be constructed from a raw
+/// C string as well and represents a C string borrowed from another location.
+///
+/// Note that this structure is **not** `repr(C)` and is not recommended to be
+/// placed in the signatures of FFI functions. Instead safe wrappers of FFI
+/// functions may leverage the unsafe `from_ptr` constructor to provide a safe
+/// interface to other consumers.
+///
+/// # Examples
+///
+/// Inspecting a foreign C string
+///
+/// ```no_run
+/// use std::ffi::CStr;
+/// use std::os::raw::c_char;
+///
+/// extern { fn my_string() -> *const c_char; }
+///
+/// unsafe {
+/// let slice = CStr::from_ptr(my_string());
+/// println!("string length: {}", slice.to_bytes().len());
+/// }
+/// ```
+///
+/// Passing a Rust-originating C string
+///
+/// ```no_run
+/// use std::ffi::{CString, CStr};
+/// use std::os::raw::c_char;
+///
+/// fn work(data: &CStr) {
+/// extern { fn work_with(data: *const c_char); }
+///
+/// unsafe { work_with(data.as_ptr()) }
+/// }
+///
+/// let s = CString::new("data data data data").unwrap();
+/// work(&s);
+/// ```
+///
+/// Converting a foreign C string into a Rust `String`
+///
+/// ```no_run
+/// use std::ffi::CStr;
+/// use std::os::raw::c_char;
+///
+/// extern { fn my_string() -> *const c_char; }
+///
+/// fn my_string_safe() -> String {
+/// unsafe {
+/// CStr::from_ptr(my_string()).to_string_lossy().into_owned()
+/// }
+/// }
+///
+/// println!("string: {}", my_string_safe());
+/// ```
+#[derive(Hash)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct CStr {
+ // FIXME: this should not be represented with a DST slice but rather with
+ // just a raw `c_char` along with some form of marker to make
+ // this an unsized type. Essentially `sizeof(&CStr)` should be the
+ // same as `sizeof(&c_char)` but `CStr` should be an unsized type.
+ inner: [c_char]
+}
+
+/// An error returned from `CString::new` to indicate that a nul byte was found
+/// in the vector provided.
+#[derive(Clone, PartialEq, Eq, Debug)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct NulError(usize, Vec<u8>);
+
+/// An error returned from `CStr::from_bytes_with_nul` to indicate that a nul
+/// byte was found too early in the slice provided or one wasn't found at all.
+#[derive(Clone, PartialEq, Eq, Debug)]
+#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
+pub struct FromBytesWithNulError { _a: () }
+
+/// An error returned from `CString::into_string` to indicate that a UTF-8 error
+/// was encountered during the conversion.
+#[derive(Clone, PartialEq, Eq, Debug)]
+#[stable(feature = "cstring_into", since = "1.7.0")]
+pub struct IntoStringError {
+ inner: CString,
+ error: Utf8Error,
+}
+
+impl CString {
+ /// Creates a new C-compatible string from a container of bytes.
+ ///
+ /// This method will consume the provided data and use the underlying bytes
+ /// to construct a new string, ensuring that there is a trailing 0 byte.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::ffi::CString;
+ /// use std::os::raw::c_char;
+ ///
+ /// extern { fn puts(s: *const c_char); }
+ ///
+ /// let to_print = CString::new("Hello!").unwrap();
+ /// unsafe {
+ /// puts(to_print.as_ptr());
+ /// }
+ /// ```
+ ///
+ /// # Errors
+ ///
+ /// This function will return an error if the bytes yielded contain an
+ /// internal 0 byte. The error returned will contain the bytes as well as
+ /// the position of the nul byte.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError> {
+ Self::_new(t.into())
+ }
+
+ fn _new(bytes: Vec<u8>) -> Result<CString, NulError> {
+ match memchr::memchr(0, &bytes) {
+ Some(i) => Err(NulError(i, bytes)),
+ None => Ok(unsafe { CString::from_vec_unchecked(bytes) }),
+ }
+ }
+
+ /// Creates a C-compatible string from a byte vector without checking for
+ /// interior 0 bytes.
+ ///
+ /// This method is equivalent to `new` except that no runtime assertion
+ /// is made that `v` contains no 0 bytes, and it requires an actual
+ /// byte vector, not anything that can be converted to one with Into.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ffi::CString;
+ ///
+ /// let raw = b"foo".to_vec();
+ /// unsafe {
+ /// let c_string = CString::from_vec_unchecked(raw);
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> CString {
+ v.reserve_exact(1);
+ v.push(0);
+ CString { inner: v.into_boxed_slice() }
+ }
+
+ /// Retakes ownership of a `CString` that was transferred to C.
+ ///
+ /// Additionally, the length of the string will be recalculated from the pointer.
+ ///
+ /// # Safety
+ ///
+ /// This should only ever be called with a pointer that was earlier
+ /// obtained by calling `into_raw` on a `CString`. Other usage (e.g. trying to take
+ /// ownership of a string that was allocated by foreign code) is likely to lead
+ /// to undefined behavior or allocator corruption.
+ #[stable(feature = "cstr_memory", since = "1.4.0")]
+ pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
+ let len = libc::strlen(ptr) + 1; // Including the NUL byte
+ let slice = slice::from_raw_parts(ptr, len as usize);
+ CString { inner: mem::transmute(slice) }
+ }
+
+ /// Transfers ownership of the string to a C caller.
+ ///
+ /// The pointer must be returned to Rust and reconstituted using
+ /// `from_raw` to be properly deallocated. Specifically, one
+ /// should *not* use the standard C `free` function to deallocate
+ /// this string.
+ ///
+ /// Failure to call `from_raw` will lead to a memory leak.
+ #[stable(feature = "cstr_memory", since = "1.4.0")]
+ pub fn into_raw(self) -> *mut c_char {
+ Box::into_raw(self.into_inner()) as *mut c_char
+ }
+
+ /// Converts the `CString` into a `String` if it contains valid Unicode data.
+ ///
+ /// On failure, ownership of the original `CString` is returned.
+ #[stable(feature = "cstring_into", since = "1.7.0")]
+ pub fn into_string(self) -> Result<String, IntoStringError> {
+ String::from_utf8(self.into_bytes())
+ .map_err(|e| IntoStringError {
+ error: e.utf8_error(),
+ inner: unsafe { CString::from_vec_unchecked(e.into_bytes()) },
+ })
+ }
+
+ /// Returns the underlying byte buffer.
+ ///
+ /// The returned buffer does **not** contain the trailing nul separator and
+ /// it is guaranteed to not have any interior nul bytes.
+ #[stable(feature = "cstring_into", since = "1.7.0")]
+ pub fn into_bytes(self) -> Vec<u8> {
+ let mut vec = self.into_inner().into_vec();
+ let _nul = vec.pop();
+ debug_assert_eq!(_nul, Some(0u8));
+ vec
+ }
+
+ /// Equivalent to the `into_bytes` function except that the returned vector
+ /// includes the trailing nul byte.
+ #[stable(feature = "cstring_into", since = "1.7.0")]
+ pub fn into_bytes_with_nul(self) -> Vec<u8> {
+ self.into_inner().into_vec()
+ }
+
+ /// Returns the contents of this `CString` as a slice of bytes.
+ ///
+ /// The returned slice does **not** contain the trailing nul separator and
+ /// it is guaranteed to not have any interior nul bytes.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn as_bytes(&self) -> &[u8] {
+ &self.inner[..self.inner.len() - 1]
+ }
+
+ /// Equivalent to the `as_bytes` function except that the returned slice
+ /// includes the trailing nul byte.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn as_bytes_with_nul(&self) -> &[u8] {
+ &self.inner
+ }
+
+ // Bypass "move out of struct which implements `Drop` trait" restriction.
+ fn into_inner(self) -> Box<[u8]> {
+ unsafe {
+ let result = ptr::read(&self.inner);
+ mem::forget(self);
+ result
+ }
+ }
+}
+
+// Turns this `CString` into an empty string to prevent
+// memory unsafe code from working by accident. Inline
+// to prevent LLVM from optimizing it away in debug builds.
+#[stable(feature = "cstring_drop", since = "1.13.0")]
+impl Drop for CString {
+ #[inline]
+ fn drop(&mut self) {
+ unsafe { *self.inner.get_unchecked_mut(0) = 0; }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl ops::Deref for CString {
+ type Target = CStr;
+
+ fn deref(&self) -> &CStr {
+ unsafe { mem::transmute(self.as_bytes_with_nul()) }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl fmt::Debug for CString {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Debug::fmt(&**self, f)
+ }
+}
+
+#[stable(feature = "cstring_into", since = "1.7.0")]
+impl From<CString> for Vec<u8> {
+ fn from(s: CString) -> Vec<u8> {
+ s.into_bytes()
+ }
+}
+
+#[stable(feature = "cstr_debug", since = "1.3.0")]
+impl fmt::Debug for CStr {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "\"")?;
+ for byte in self.to_bytes().iter().flat_map(|&b| ascii::escape_default(b)) {
+ f.write_char(byte as char)?;
+ }
+ write!(f, "\"")
+ }
+}
+
+#[stable(feature = "cstr_default", since = "1.10.0")]
+impl<'a> Default for &'a CStr {
+ fn default() -> &'a CStr {
+ static SLICE: &'static [c_char] = &[0];
+ unsafe { CStr::from_ptr(SLICE.as_ptr()) }
+ }
+}
+
+#[stable(feature = "cstr_default", since = "1.10.0")]
+impl Default for CString {
+ /// Creates an empty `CString`.
+ fn default() -> CString {
+ let a: &CStr = Default::default();
+ a.to_owned()
+ }
+}
+
+#[stable(feature = "cstr_borrow", since = "1.3.0")]
+impl Borrow<CStr> for CString {
+ fn borrow(&self) -> &CStr { self }
+}
+
+impl NulError {
+ /// Returns the position of the nul byte in the slice that was provided to
+ /// `CString::new`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ffi::CString;
+ ///
+ /// let nul_error = CString::new("foo\0bar").unwrap_err();
+ /// assert_eq!(nul_error.nul_position(), 3);
+ ///
+ /// let nul_error = CString::new("foo bar\0").unwrap_err();
+ /// assert_eq!(nul_error.nul_position(), 7);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn nul_position(&self) -> usize { self.0 }
+
+ /// Consumes this error, returning the underlying vector of bytes which
+ /// generated the error in the first place.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ffi::CString;
+ ///
+ /// let nul_error = CString::new("foo\0bar").unwrap_err();
+ /// assert_eq!(nul_error.into_vec(), b"foo\0bar");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_vec(self) -> Vec<u8> { self.1 }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Error for NulError {
+ fn description(&self) -> &str { "nul byte found in data" }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl fmt::Display for NulError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "nul byte found in provided data at position: {}", self.0)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl From<NulError> for io::Error {
+ fn from(_: NulError) -> io::Error {
+ io::Error::new(io::ErrorKind::InvalidInput,
+ "data provided contains a nul byte")
+ }
+}
+
+impl IntoStringError {
+ /// Consumes this error, returning original `CString` which generated the
+ /// error.
+ #[stable(feature = "cstring_into", since = "1.7.0")]
+ pub fn into_cstring(self) -> CString {
+ self.inner
+ }
+
+ /// Access the underlying UTF-8 error that was the cause of this error.
+ #[stable(feature = "cstring_into", since = "1.7.0")]
+ pub fn utf8_error(&self) -> Utf8Error {
+ self.error
+ }
+}
+
+#[stable(feature = "cstring_into", since = "1.7.0")]
+impl Error for IntoStringError {
+ fn description(&self) -> &str {
+ "C string contained non-utf8 bytes"
+ }
+
+ fn cause(&self) -> Option<&Error> {
+ Some(&self.error)
+ }
+}
+
+#[stable(feature = "cstring_into", since = "1.7.0")]
+impl fmt::Display for IntoStringError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ self.description().fmt(f)
+ }
+}
+
+impl CStr {
+ /// Casts a raw C string to a safe C string wrapper.
+ ///
+ /// This function will cast the provided `ptr` to the `CStr` wrapper which
+ /// allows inspection and interoperation of non-owned C strings. This method
+ /// is unsafe for a number of reasons:
+ ///
+ /// * There is no guarantee to the validity of `ptr`
+ /// * The returned lifetime is not guaranteed to be the actual lifetime of
+ /// `ptr`
+ /// * There is no guarantee that the memory pointed to by `ptr` contains a
+ /// valid nul terminator byte at the end of the string.
+ ///
+ /// > **Note**: This operation is intended to be a 0-cost cast but it is
+ /// > currently implemented with an up-front calculation of the length of
+ /// > the string. This is not guaranteed to always be the case.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// # fn main() {
+ /// use std::ffi::CStr;
+ /// use std::os::raw::c_char;
+ ///
+ /// extern {
+ /// fn my_string() -> *const c_char;
+ /// }
+ ///
+ /// unsafe {
+ /// let slice = CStr::from_ptr(my_string());
+ /// println!("string returned: {}", slice.to_str().unwrap());
+ /// }
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
+ let len = libc::strlen(ptr);
+ mem::transmute(slice::from_raw_parts(ptr, len as usize + 1))
+ }
+
+ /// Creates a C string wrapper from a byte slice.
+ ///
+ /// This function will cast the provided `bytes` to a `CStr` wrapper after
+ /// ensuring that it is null terminated and does not contain any interior
+ /// nul bytes.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ffi::CStr;
+ ///
+ /// let cstr = CStr::from_bytes_with_nul(b"hello\0");
+ /// assert!(cstr.is_ok());
+ /// ```
+ #[stable(feature = "cstr_from_bytes", since = "1.10.0")]
+ pub fn from_bytes_with_nul(bytes: &[u8])
+ -> Result<&CStr, FromBytesWithNulError> {
+ if bytes.is_empty() || memchr::memchr(0, &bytes) != Some(bytes.len() - 1) {
+ Err(FromBytesWithNulError { _a: () })
+ } else {
+ Ok(unsafe { Self::from_bytes_with_nul_unchecked(bytes) })
+ }
+ }
+
+ /// Unsafely creates a C string wrapper from a byte slice.
+ ///
+ /// This function will cast the provided `bytes` to a `CStr` wrapper without
+ /// performing any sanity checks. The provided slice must be null terminated
+ /// and not contain any interior nul bytes.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ffi::{CStr, CString};
+ ///
+ /// unsafe {
+ /// let cstring = CString::new("hello").unwrap();
+ /// let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul());
+ /// assert_eq!(cstr, &*cstring);
+ /// }
+ /// ```
+ #[stable(feature = "cstr_from_bytes", since = "1.10.0")]
+ pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
+ mem::transmute(bytes)
+ }
+
+ /// Returns the inner pointer to this C string.
+ ///
+ /// The returned pointer will be valid for as long as `self` is and points
+ /// to a contiguous region of memory terminated with a 0 byte to represent
+ /// the end of the string.
+ ///
+ /// **WARNING**
+ ///
+ /// It is your responsibility to make sure that the underlying memory is not
+ /// freed too early. For example, the following code will cause undefined
+ /// behaviour when `ptr` is used inside the `unsafe` block:
+ ///
+ /// ```no_run
+ /// use std::ffi::{CString};
+ ///
+ /// let ptr = CString::new("Hello").unwrap().as_ptr();
+ /// unsafe {
+ /// // `ptr` is dangling
+ /// *ptr;
+ /// }
+ /// ```
+ ///
+ /// This happens because the pointer returned by `as_ptr` does not carry any
+ /// lifetime information and the string is deallocated immediately after
+ /// the `CString::new("Hello").unwrap().as_ptr()` expression is evaluated.
+ /// To fix the problem, bind the string to a local variable:
+ ///
+ /// ```no_run
+ /// use std::ffi::{CString};
+ ///
+ /// let hello = CString::new("Hello").unwrap();
+ /// let ptr = hello.as_ptr();
+ /// unsafe {
+ /// // `ptr` is valid because `hello` is in scope
+ /// *ptr;
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn as_ptr(&self) -> *const c_char {
+ self.inner.as_ptr()
+ }
+
+ /// Converts this C string to a byte slice.
+ ///
+ /// This function will calculate the length of this string (which normally
+ /// requires a linear amount of work to be done) and then return the
+ /// resulting slice of `u8` elements.
+ ///
+ /// The returned slice will **not** contain the trailing nul that this C
+ /// string has.
+ ///
+ /// > **Note**: This method is currently implemented as a 0-cost cast, but
+ /// > it is planned to alter its definition in the future to perform the
+ /// > length calculation whenever this method is called.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn to_bytes(&self) -> &[u8] {
+ let bytes = self.to_bytes_with_nul();
+ &bytes[..bytes.len() - 1]
+ }
+
+ /// Converts this C string to a byte slice containing the trailing 0 byte.
+ ///
+ /// This function is the equivalent of `to_bytes` except that it will retain
+ /// the trailing nul instead of chopping it off.
+ ///
+ /// > **Note**: This method is currently implemented as a 0-cost cast, but
+ /// > it is planned to alter its definition in the future to perform the
+ /// > length calculation whenever this method is called.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn to_bytes_with_nul(&self) -> &[u8] {
+ unsafe { mem::transmute(&self.inner) }
+ }
+
+ /// Yields a `&str` slice if the `CStr` contains valid UTF-8.
+ ///
+ /// This function will calculate the length of this string and check for
+ /// UTF-8 validity, and then return the `&str` if it's valid.
+ ///
+ /// > **Note**: This method is currently implemented to check for validity
+ /// > after a 0-cost cast, but it is planned to alter its definition in the
+ /// > future to perform the length calculation in addition to the UTF-8
+ /// > check whenever this method is called.
+ #[stable(feature = "cstr_to_str", since = "1.4.0")]
+ pub fn to_str(&self) -> Result<&str, str::Utf8Error> {
+ // NB: When CStr is changed to perform the length check in .to_bytes()
+ // instead of in from_ptr(), it may be worth considering if this should
+ // be rewritten to do the UTF-8 check inline with the length calculation
+ // instead of doing it afterwards.
+ str::from_utf8(self.to_bytes())
+ }
+
+ /// Converts a `CStr` into a `Cow<str>`.
+ ///
+ /// This function will calculate the length of this string (which normally
+ /// requires a linear amount of work to be done) and then return the
+ /// resulting slice as a `Cow<str>`, replacing any invalid UTF-8 sequences
+ /// with `U+FFFD REPLACEMENT CHARACTER`.
+ ///
+ /// > **Note**: This method is currently implemented to check for validity
+ /// > after a 0-cost cast, but it is planned to alter its definition in the
+ /// > future to perform the length calculation in addition to the UTF-8
+ /// > check whenever this method is called.
+ #[stable(feature = "cstr_to_str", since = "1.4.0")]
+ pub fn to_string_lossy(&self) -> Cow<str> {
+ String::from_utf8_lossy(self.to_bytes())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialEq for CStr {
+ fn eq(&self, other: &CStr) -> bool {
+ self.to_bytes().eq(other.to_bytes())
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Eq for CStr {}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialOrd for CStr {
+ fn partial_cmp(&self, other: &CStr) -> Option<Ordering> {
+ self.to_bytes().partial_cmp(&other.to_bytes())
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Ord for CStr {
+ fn cmp(&self, other: &CStr) -> Ordering {
+ self.to_bytes().cmp(&other.to_bytes())
+ }
+}
+
+#[stable(feature = "cstr_borrow", since = "1.3.0")]
+impl ToOwned for CStr {
+ type Owned = CString;
+
+ fn to_owned(&self) -> CString {
+ unsafe { CString::from_vec_unchecked(self.to_bytes().to_vec()) }
+ }
+}
+
+#[stable(feature = "cstring_asref", since = "1.7.0")]
+impl<'a> From<&'a CStr> for CString {
+ fn from(s: &'a CStr) -> CString {
+ s.to_owned()
+ }
+}
+
+#[stable(feature = "cstring_asref", since = "1.7.0")]
+impl ops::Index<ops::RangeFull> for CString {
+ type Output = CStr;
+
+ #[inline]
+ fn index(&self, _index: ops::RangeFull) -> &CStr {
+ self
+ }
+}
+
+#[stable(feature = "cstring_asref", since = "1.7.0")]
+impl AsRef<CStr> for CStr {
+ fn as_ref(&self) -> &CStr {
+ self
+ }
+}
+
+#[stable(feature = "cstring_asref", since = "1.7.0")]
+impl AsRef<CStr> for CString {
+ fn as_ref(&self) -> &CStr {
+ self
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use os::raw::c_char;
+ use borrow::Cow::{Borrowed, Owned};
+ use hash::{Hash, Hasher};
+ use collections::hash_map::DefaultHasher;
+
+ #[test]
+ fn c_to_rust() {
+ let data = b"123\0";
+ let ptr = data.as_ptr() as *const c_char;
+ unsafe {
+ assert_eq!(CStr::from_ptr(ptr).to_bytes(), b"123");
+ assert_eq!(CStr::from_ptr(ptr).to_bytes_with_nul(), b"123\0");
+ }
+ }
+
+ #[test]
+ fn simple() {
+ let s = CString::new("1234").unwrap();
+ assert_eq!(s.as_bytes(), b"1234");
+ assert_eq!(s.as_bytes_with_nul(), b"1234\0");
+ }
+
+ #[test]
+ fn build_with_zero1() {
+ assert!(CString::new(&b"\0"[..]).is_err());
+ }
+ #[test]
+ fn build_with_zero2() {
+ assert!(CString::new(vec![0]).is_err());
+ }
+
+ #[test]
+ fn build_with_zero3() {
+ unsafe {
+ let s = CString::from_vec_unchecked(vec![0]);
+ assert_eq!(s.as_bytes(), b"\0");
+ }
+ }
+
+ #[test]
+ fn formatted() {
+ let s = CString::new(&b"abc\x01\x02\n\xE2\x80\xA6\xFF"[..]).unwrap();
+ assert_eq!(format!("{:?}", s), r#""abc\x01\x02\n\xe2\x80\xa6\xff""#);
+ }
+
+ #[test]
+ fn borrowed() {
+ unsafe {
+ let s = CStr::from_ptr(b"12\0".as_ptr() as *const _);
+ assert_eq!(s.to_bytes(), b"12");
+ assert_eq!(s.to_bytes_with_nul(), b"12\0");
+ }
+ }
+
+ #[test]
+ fn to_str() {
+ let data = b"123\xE2\x80\xA6\0";
+ let ptr = data.as_ptr() as *const c_char;
+ unsafe {
+ assert_eq!(CStr::from_ptr(ptr).to_str(), Ok("123…"));
+ assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Borrowed("123…"));
+ }
+ let data = b"123\xE2\0";
+ let ptr = data.as_ptr() as *const c_char;
+ unsafe {
+ assert!(CStr::from_ptr(ptr).to_str().is_err());
+ assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Owned::<str>(format!("123\u{FFFD}")));
+ }
+ }
+
+ #[test]
+ fn to_owned() {
+ let data = b"123\0";
+ let ptr = data.as_ptr() as *const c_char;
+
+ let owned = unsafe { CStr::from_ptr(ptr).to_owned() };
+ assert_eq!(owned.as_bytes_with_nul(), data);
+ }
+
+ #[test]
+ fn equal_hash() {
+ let data = b"123\xE2\xFA\xA6\0";
+ let ptr = data.as_ptr() as *const c_char;
+ let cstr: &'static CStr = unsafe { CStr::from_ptr(ptr) };
+
+ let mut s = DefaultHasher::new();
+ cstr.hash(&mut s);
+ let cstr_hash = s.finish();
+ let mut s = DefaultHasher::new();
+ CString::new(&data[..data.len() - 1]).unwrap().hash(&mut s);
+ let cstring_hash = s.finish();
+
+ assert_eq!(cstr_hash, cstring_hash);
+ }
+
+ #[test]
+ fn from_bytes_with_nul() {
+ let data = b"123\0";
+ let cstr = CStr::from_bytes_with_nul(data);
+ assert_eq!(cstr.map(CStr::to_bytes), Ok(&b"123"[..]));
+ let cstr = CStr::from_bytes_with_nul(data);
+ assert_eq!(cstr.map(CStr::to_bytes_with_nul), Ok(&b"123\0"[..]));
+
+ unsafe {
+ let cstr = CStr::from_bytes_with_nul(data);
+ let cstr_unchecked = CStr::from_bytes_with_nul_unchecked(data);
+ assert_eq!(cstr, Ok(cstr_unchecked));
+ }
+ }
+
+ #[test]
+ fn from_bytes_with_nul_unterminated() {
+ let data = b"123";
+ let cstr = CStr::from_bytes_with_nul(data);
+ assert!(cstr.is_err());
+ }
+
+ #[test]
+ fn from_bytes_with_nul_interior() {
+ let data = b"1\023\0";
+ let cstr = CStr::from_bytes_with_nul(data);
+ assert!(cstr.is_err());
+ }
+}
diff --git a/ctr-std/src/ffi/mod.rs b/ctr-std/src/ffi/mod.rs
new file mode 100644
index 0000000..ca1ff18
--- /dev/null
+++ b/ctr-std/src/ffi/mod.rs
@@ -0,0 +1,24 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Utilities related to FFI bindings.
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use self::c_str::{CString, CStr, NulError, IntoStringError};
+#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
+pub use self::c_str::{FromBytesWithNulError};
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use self::os_str::{OsString, OsStr};
+
+mod c_str;
+mod os_str;
diff --git a/ctr-std/src/ffi/os_str.rs b/ctr-std/src/ffi/os_str.rs
new file mode 100644
index 0000000..84b50f0
--- /dev/null
+++ b/ctr-std/src/ffi/os_str.rs
@@ -0,0 +1,629 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use borrow::{Borrow, Cow};
+use fmt::{self, Debug};
+use mem;
+use ops;
+use cmp;
+use hash::{Hash, Hasher};
+
+use sys::os_str::{Buf, Slice};
+use sys_common::{AsInner, IntoInner, FromInner};
+
+/// A type that can represent owned, mutable platform-native strings, but is
+/// cheaply inter-convertible with Rust strings.
+///
+/// The need for this type arises from the fact that:
+///
+/// * On Unix systems, strings are often arbitrary sequences of non-zero
+/// bytes, in many cases interpreted as UTF-8.
+///
+/// * On Windows, strings are often arbitrary sequences of non-zero 16-bit
+/// values, interpreted as UTF-16 when it is valid to do so.
+///
+/// * In Rust, strings are always valid UTF-8, but may contain zeros.
+///
+/// `OsString` and `OsStr` bridge this gap by simultaneously representing Rust
+/// and platform-native string values, and in particular allowing a Rust string
+/// to be converted into an "OS" string with no cost.
+#[derive(Clone)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct OsString {
+ inner: Buf
+}
+
+/// Slices into OS strings (see `OsString`).
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct OsStr {
+ inner: Slice
+}
+
+impl OsString {
+ /// Constructs a new empty `OsString`.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new() -> OsString {
+ OsString { inner: Buf::from_string(String::new()) }
+ }
+
+ /// Converts to an `OsStr` slice.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn as_os_str(&self) -> &OsStr {
+ self
+ }
+
+ /// Converts the `OsString` into a `String` if it contains valid Unicode data.
+ ///
+ /// On failure, ownership of the original `OsString` is returned.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_string(self) -> Result<String, OsString> {
+ self.inner.into_string().map_err(|buf| OsString { inner: buf} )
+ }
+
+ /// Extends the string with the given `&OsStr` slice.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
+ self.inner.push_slice(&s.as_ref().inner)
+ }
+
+ /// Creates a new `OsString` with the given capacity.
+ ///
+ /// The string will be able to hold exactly `capacity` lenth units of other
+ /// OS strings without reallocating. If `capacity` is 0, the string will not
+ /// allocate.
+ ///
+ /// See main `OsString` documentation information about encoding.
+ #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
+ pub fn with_capacity(capacity: usize) -> OsString {
+ OsString {
+ inner: Buf::with_capacity(capacity)
+ }
+ }
+
+ /// Truncates the `OsString` to zero length.
+ #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
+ pub fn clear(&mut self) {
+ self.inner.clear()
+ }
+
+ /// Returns the capacity this `OsString` can hold without reallocating.
+ ///
+ /// See `OsString` introduction for information about encoding.
+ #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
+ pub fn capacity(&self) -> usize {
+ self.inner.capacity()
+ }
+
+ /// Reserves capacity for at least `additional` more capacity to be inserted
+ /// in the given `OsString`.
+ ///
+ /// The collection may reserve more space to avoid frequent reallocations.
+ #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
+ pub fn reserve(&mut self, additional: usize) {
+ self.inner.reserve(additional)
+ }
+
+ /// Reserves the minimum capacity for exactly `additional` more capacity to
+ /// be inserted in the given `OsString`. Does nothing if the capacity is
+ /// already sufficient.
+ ///
+ /// Note that the allocator may give the collection more space than it
+ /// requests. Therefore capacity can not be relied upon to be precisely
+ /// minimal. Prefer reserve if future insertions are expected.
+ #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
+ pub fn reserve_exact(&mut self, additional: usize) {
+ self.inner.reserve_exact(additional)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl From<String> for OsString {
+ fn from(s: String) -> OsString {
+ OsString { inner: Buf::from_string(s) }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, T: ?Sized + AsRef<OsStr>> From<&'a T> for OsString {
+ fn from(s: &'a T) -> OsString {
+ s.as_ref().to_os_string()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl ops::Index<ops::RangeFull> for OsString {
+ type Output = OsStr;
+
+ #[inline]
+ fn index(&self, _index: ops::RangeFull) -> &OsStr {
+ OsStr::from_inner(self.inner.as_slice())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl ops::Deref for OsString {
+ type Target = OsStr;
+
+ #[inline]
+ fn deref(&self) -> &OsStr {
+ &self[..]
+ }
+}
+
+#[stable(feature = "osstring_default", since = "1.9.0")]
+impl Default for OsString {
+ /// Constructs an empty `OsString`.
+ #[inline]
+ fn default() -> OsString {
+ OsString::new()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Debug for OsString {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ fmt::Debug::fmt(&**self, formatter)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialEq for OsString {
+ fn eq(&self, other: &OsString) -> bool {
+ &**self == &**other
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialEq<str> for OsString {
+ fn eq(&self, other: &str) -> bool {
+ &**self == other
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialEq<OsString> for str {
+ fn eq(&self, other: &OsString) -> bool {
+ &**other == self
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Eq for OsString {}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialOrd for OsString {
+ #[inline]
+ fn partial_cmp(&self, other: &OsString) -> Option<cmp::Ordering> {
+ (&**self).partial_cmp(&**other)
+ }
+ #[inline]
+ fn lt(&self, other: &OsString) -> bool { &**self < &**other }
+ #[inline]
+ fn le(&self, other: &OsString) -> bool { &**self <= &**other }
+ #[inline]
+ fn gt(&self, other: &OsString) -> bool { &**self > &**other }
+ #[inline]
+ fn ge(&self, other: &OsString) -> bool { &**self >= &**other }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialOrd<str> for OsString {
+ #[inline]
+ fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
+ (&**self).partial_cmp(other)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Ord for OsString {
+ #[inline]
+ fn cmp(&self, other: &OsString) -> cmp::Ordering {
+ (&**self).cmp(&**other)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Hash for OsString {
+ #[inline]
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ (&**self).hash(state)
+ }
+}
+
+impl OsStr {
+ /// Coerces into an `OsStr` slice.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ffi::OsStr;
+ ///
+ /// let os_str = OsStr::new("foo");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &OsStr {
+ s.as_ref()
+ }
+
+ fn from_inner(inner: &Slice) -> &OsStr {
+ unsafe { mem::transmute(inner) }
+ }
+
+ /// Yields a `&str` slice if the `OsStr` is valid Unicode.
+ ///
+ /// This conversion may entail doing a check for UTF-8 validity.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn to_str(&self) -> Option<&str> {
+ self.inner.to_str()
+ }
+
+ /// Converts an `OsStr` to a `Cow<str>`.
+ ///
+ /// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn to_string_lossy(&self) -> Cow<str> {
+ self.inner.to_string_lossy()
+ }
+
+ /// Copies the slice into an owned `OsString`.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn to_os_string(&self) -> OsString {
+ OsString { inner: self.inner.to_owned() }
+ }
+
+ /// Checks whether the `OsStr` is empty.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ffi::OsStr;
+ ///
+ /// let os_str = OsStr::new("");
+ /// assert!(os_str.is_empty());
+ ///
+ /// let os_str = OsStr::new("foo");
+ /// assert!(!os_str.is_empty());
+ /// ```
+ #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
+ pub fn is_empty(&self) -> bool {
+ self.inner.inner.is_empty()
+ }
+
+ /// Returns the length of this `OsStr`.
+ ///
+ /// Note that this does **not** return the number of bytes in this string
+ /// as, for example, OS strings on Windows are encoded as a list of `u16`
+ /// rather than a list of bytes. This number is simply useful for passing to
+ /// other methods like `OsString::with_capacity` to avoid reallocations.
+ ///
+ /// See `OsStr` introduction for more information about encoding.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ffi::OsStr;
+ ///
+ /// let os_str = OsStr::new("");
+ /// assert_eq!(os_str.len(), 0);
+ ///
+ /// let os_str = OsStr::new("foo");
+ /// assert_eq!(os_str.len(), 3);
+ /// ```
+ #[stable(feature = "osstring_simple_functions", since = "1.9.0")]
+ pub fn len(&self) -> usize {
+ self.inner.inner.len()
+ }
+
+ /// Gets the underlying byte representation.
+ ///
+ /// Note: it is *crucial* that this API is private, to avoid
+ /// revealing the internal, platform-specific encodings.
+ fn bytes(&self) -> &[u8] {
+ unsafe { mem::transmute(&self.inner) }
+ }
+}
+
+#[stable(feature = "osstring_default", since = "1.9.0")]
+impl<'a> Default for &'a OsStr {
+ /// Creates an empty `OsStr`.
+ #[inline]
+ fn default() -> &'a OsStr {
+ OsStr::new("")
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialEq for OsStr {
+ fn eq(&self, other: &OsStr) -> bool {
+ self.bytes().eq(other.bytes())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialEq<str> for OsStr {
+ fn eq(&self, other: &str) -> bool {
+ *self == *OsStr::new(other)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialEq<OsStr> for str {
+ fn eq(&self, other: &OsStr) -> bool {
+ *other == *OsStr::new(self)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Eq for OsStr {}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialOrd for OsStr {
+ #[inline]
+ fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
+ self.bytes().partial_cmp(other.bytes())
+ }
+ #[inline]
+ fn lt(&self, other: &OsStr) -> bool { self.bytes().lt(other.bytes()) }
+ #[inline]
+ fn le(&self, other: &OsStr) -> bool { self.bytes().le(other.bytes()) }
+ #[inline]
+ fn gt(&self, other: &OsStr) -> bool { self.bytes().gt(other.bytes()) }
+ #[inline]
+ fn ge(&self, other: &OsStr) -> bool { self.bytes().ge(other.bytes()) }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl PartialOrd<str> for OsStr {
+ #[inline]
+ fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
+ self.partial_cmp(OsStr::new(other))
+ }
+}
+
+// FIXME (#19470): cannot provide PartialOrd<OsStr> for str until we
+// have more flexible coherence rules.
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Ord for OsStr {
+ #[inline]
+ fn cmp(&self, other: &OsStr) -> cmp::Ordering { self.bytes().cmp(other.bytes()) }
+}
+
+macro_rules! impl_cmp {
+ ($lhs:ty, $rhs: ty) => {
+ #[stable(feature = "cmp_os_str", since = "1.8.0")]
+ impl<'a, 'b> PartialEq<$rhs> for $lhs {
+ #[inline]
+ fn eq(&self, other: &$rhs) -> bool { <OsStr as PartialEq>::eq(self, other) }
+ }
+
+ #[stable(feature = "cmp_os_str", since = "1.8.0")]
+ impl<'a, 'b> PartialEq<$lhs> for $rhs {
+ #[inline]
+ fn eq(&self, other: &$lhs) -> bool { <OsStr as PartialEq>::eq(self, other) }
+ }
+
+ #[stable(feature = "cmp_os_str", since = "1.8.0")]
+ impl<'a, 'b> PartialOrd<$rhs> for $lhs {
+ #[inline]
+ fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
+ <OsStr as PartialOrd>::partial_cmp(self, other)
+ }
+ }
+
+ #[stable(feature = "cmp_os_str", since = "1.8.0")]
+ impl<'a, 'b> PartialOrd<$lhs> for $rhs {
+ #[inline]
+ fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
+ <OsStr as PartialOrd>::partial_cmp(self, other)
+ }
+ }
+ }
+}
+
+impl_cmp!(OsString, OsStr);
+impl_cmp!(OsString, &'a OsStr);
+impl_cmp!(Cow<'a, OsStr>, OsStr);
+impl_cmp!(Cow<'a, OsStr>, &'b OsStr);
+impl_cmp!(Cow<'a, OsStr>, OsString);
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Hash for OsStr {
+ #[inline]
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ self.bytes().hash(state)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Debug for OsStr {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ self.inner.fmt(formatter)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Borrow<OsStr> for OsString {
+ fn borrow(&self) -> &OsStr { &self[..] }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl ToOwned for OsStr {
+ type Owned = OsString;
+ fn to_owned(&self) -> OsString { self.to_os_string() }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<OsStr> for OsStr {
+ fn as_ref(&self) -> &OsStr {
+ self
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<OsStr> for OsString {
+ fn as_ref(&self) -> &OsStr {
+ self
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<OsStr> for str {
+ fn as_ref(&self) -> &OsStr {
+ OsStr::from_inner(Slice::from_str(self))
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<OsStr> for String {
+ fn as_ref(&self) -> &OsStr {
+ (&**self).as_ref()
+ }
+}
+
+impl FromInner<Buf> for OsString {
+ fn from_inner(buf: Buf) -> OsString {
+ OsString { inner: buf }
+ }
+}
+
+impl IntoInner<Buf> for OsString {
+ fn into_inner(self) -> Buf {
+ self.inner
+ }
+}
+
+impl AsInner<Slice> for OsStr {
+ fn as_inner(&self) -> &Slice {
+ &self.inner
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use sys_common::{AsInner, IntoInner};
+
+ #[test]
+ fn test_os_string_with_capacity() {
+ let os_string = OsString::with_capacity(0);
+ assert_eq!(0, os_string.inner.into_inner().capacity());
+
+ let os_string = OsString::with_capacity(10);
+ assert_eq!(10, os_string.inner.into_inner().capacity());
+
+ let mut os_string = OsString::with_capacity(0);
+ os_string.push("abc");
+ assert!(os_string.inner.into_inner().capacity() >= 3);
+ }
+
+ #[test]
+ fn test_os_string_clear() {
+ let mut os_string = OsString::from("abc");
+ assert_eq!(3, os_string.inner.as_inner().len());
+
+ os_string.clear();
+ assert_eq!(&os_string, "");
+ assert_eq!(0, os_string.inner.as_inner().len());
+ }
+
+ #[test]
+ fn test_os_string_capacity() {
+ let os_string = OsString::with_capacity(0);
+ assert_eq!(0, os_string.capacity());
+
+ let os_string = OsString::with_capacity(10);
+ assert_eq!(10, os_string.capacity());
+
+ let mut os_string = OsString::with_capacity(0);
+ os_string.push("abc");
+ assert!(os_string.capacity() >= 3);
+ }
+
+ #[test]
+ fn test_os_string_reserve() {
+ let mut os_string = OsString::new();
+ assert_eq!(os_string.capacity(), 0);
+
+ os_string.reserve(2);
+ assert!(os_string.capacity() >= 2);
+
+ for _ in 0..16 {
+ os_string.push("a");
+ }
+
+ assert!(os_string.capacity() >= 16);
+ os_string.reserve(16);
+ assert!(os_string.capacity() >= 32);
+
+ os_string.push("a");
+
+ os_string.reserve(16);
+ assert!(os_string.capacity() >= 33)
+ }
+
+ #[test]
+ fn test_os_string_reserve_exact() {
+ let mut os_string = OsString::new();
+ assert_eq!(os_string.capacity(), 0);
+
+ os_string.reserve_exact(2);
+ assert!(os_string.capacity() >= 2);
+
+ for _ in 0..16 {
+ os_string.push("a");
+ }
+
+ assert!(os_string.capacity() >= 16);
+ os_string.reserve_exact(16);
+ assert!(os_string.capacity() >= 32);
+
+ os_string.push("a");
+
+ os_string.reserve_exact(16);
+ assert!(os_string.capacity() >= 33)
+ }
+
+ #[test]
+ fn test_os_string_default() {
+ let os_string: OsString = Default::default();
+ assert_eq!("", &os_string);
+ }
+
+ #[test]
+ fn test_os_str_is_empty() {
+ let mut os_string = OsString::new();
+ assert!(os_string.is_empty());
+
+ os_string.push("abc");
+ assert!(!os_string.is_empty());
+
+ os_string.clear();
+ assert!(os_string.is_empty());
+ }
+
+ #[test]
+ fn test_os_str_len() {
+ let mut os_string = OsString::new();
+ assert_eq!(0, os_string.len());
+
+ os_string.push("abc");
+ assert_eq!(3, os_string.len());
+
+ os_string.clear();
+ assert_eq!(0, os_string.len());
+ }
+
+ #[test]
+ fn test_os_str_default() {
+ let os_str: &OsStr = Default::default();
+ assert_eq!("", os_str);
+ }
+}
diff --git a/ctr-std/src/io/buffered.rs b/ctr-std/src/io/buffered.rs
new file mode 100644
index 0000000..44dd4e9
--- /dev/null
+++ b/ctr-std/src/io/buffered.rs
@@ -0,0 +1,1153 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Buffering wrappers for I/O traits
+
+use io::prelude::*;
+
+use cmp;
+use error;
+use fmt;
+use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom};
+use memchr;
+
+/// The `BufReader` struct adds buffering to any reader.
+///
+/// It can be excessively inefficient to work directly with a [`Read`] instance.
+/// For example, every call to [`read`] on [`TcpStream`] results in a system call.
+/// A `BufReader` performs large, infrequent reads on the underlying [`Read`]
+/// and maintains an in-memory buffer of the results.
+///
+/// [`Read`]: ../../std/io/trait.Read.html
+/// [`read`]: ../../std/net/struct.TcpStream.html#method.read
+/// [`TcpStream`]: ../../std/net/struct.TcpStream.html
+///
+/// # Examples
+///
+/// ```
+/// use std::io::prelude::*;
+/// use std::io::BufReader;
+/// use std::fs::File;
+///
+/// # fn foo() -> std::io::Result<()> {
+/// let mut f = try!(File::open("log.txt"));
+/// let mut reader = BufReader::new(f);
+///
+/// let mut line = String::new();
+/// let len = try!(reader.read_line(&mut line));
+/// println!("First line is {} bytes long", len);
+/// # Ok(())
+/// # }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct BufReader<R> {
+ inner: R,
+ buf: Box<[u8]>,
+ pos: usize,
+ cap: usize,
+}
+
+impl<R: Read> BufReader<R> {
+ /// Creates a new `BufReader` with a default buffer capacity.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::BufReader;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let mut f = try!(File::open("log.txt"));
+ /// let mut reader = BufReader::new(f);
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new(inner: R) -> BufReader<R> {
+ BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
+ }
+
+ /// Creates a new `BufReader` with the specified buffer capacity.
+ ///
+ /// # Examples
+ ///
+ /// Creating a buffer with ten bytes of capacity:
+ ///
+ /// ```
+ /// use std::io::BufReader;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let mut f = try!(File::open("log.txt"));
+ /// let mut reader = BufReader::with_capacity(10, f);
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn with_capacity(cap: usize, inner: R) -> BufReader<R> {
+ BufReader {
+ inner: inner,
+ buf: vec![0; cap].into_boxed_slice(),
+ pos: 0,
+ cap: 0,
+ }
+ }
+
+ /// Gets a reference to the underlying reader.
+ ///
+ /// It is inadvisable to directly read from the underlying reader.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::BufReader;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let mut f1 = try!(File::open("log.txt"));
+ /// let mut reader = BufReader::new(f1);
+ ///
+ /// let f2 = reader.get_ref();
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_ref(&self) -> &R { &self.inner }
+
+ /// Gets a mutable reference to the underlying reader.
+ ///
+ /// It is inadvisable to directly read from the underlying reader.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::BufReader;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let mut f1 = try!(File::open("log.txt"));
+ /// let mut reader = BufReader::new(f1);
+ ///
+ /// let f2 = reader.get_mut();
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
+
+ /// Unwraps this `BufReader`, returning the underlying reader.
+ ///
+ /// Note that any leftover data in the internal buffer is lost.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::BufReader;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let mut f1 = try!(File::open("log.txt"));
+ /// let mut reader = BufReader::new(f1);
+ ///
+ /// let f2 = reader.into_inner();
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_inner(self) -> R { self.inner }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Read> Read for BufReader<R> {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ // If we don't have any buffered data and we're doing a massive read
+ // (larger than our internal buffer), bypass our internal buffer
+ // entirely.
+ if self.pos == self.cap && buf.len() >= self.buf.len() {
+ return self.inner.read(buf);
+ }
+ let nread = {
+ let mut rem = self.fill_buf()?;
+ rem.read(buf)?
+ };
+ self.consume(nread);
+ Ok(nread)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Read> BufRead for BufReader<R> {
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ // If we've reached the end of our internal buffer then we need to fetch
+ // some more data from the underlying reader.
+ if self.pos == self.cap {
+ self.cap = self.inner.read(&mut self.buf)?;
+ self.pos = 0;
+ }
+ Ok(&self.buf[self.pos..self.cap])
+ }
+
+ fn consume(&mut self, amt: usize) {
+ self.pos = cmp::min(self.pos + amt, self.cap);
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R> fmt::Debug for BufReader<R> where R: fmt::Debug {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ fmt.debug_struct("BufReader")
+ .field("reader", &self.inner)
+ .field("buffer", &format_args!("{}/{}", self.cap - self.pos, self.buf.len()))
+ .finish()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Seek> Seek for BufReader<R> {
+ /// Seek to an offset, in bytes, in the underlying reader.
+ ///
+ /// The position used for seeking with `SeekFrom::Current(_)` is the
+ /// position the underlying reader would be at if the `BufReader` had no
+ /// internal buffer.
+ ///
+ /// Seeking always discards the internal buffer, even if the seek position
+ /// would otherwise fall within it. This guarantees that calling
+ /// `.into_inner()` immediately after a seek yields the underlying reader
+ /// at the same position.
+ ///
+ /// See `std::io::Seek` for more details.
+ ///
+ /// Note: In the edge case where you're seeking with `SeekFrom::Current(n)`
+ /// where `n` minus the internal buffer length underflows an `i64`, two
+ /// seeks will be performed instead of one. If the second seek returns
+ /// `Err`, the underlying reader will be left at the same position it would
+ /// have if you seeked to `SeekFrom::Current(0)`.
+ fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+ let result: u64;
+ if let SeekFrom::Current(n) = pos {
+ let remainder = (self.cap - self.pos) as i64;
+ // it should be safe to assume that remainder fits within an i64 as the alternative
+ // means we managed to allocate 8 exbibytes and that's absurd.
+ // But it's not out of the realm of possibility for some weird underlying reader to
+ // support seeking by i64::min_value() so we need to handle underflow when subtracting
+ // remainder.
+ if let Some(offset) = n.checked_sub(remainder) {
+ result = self.inner.seek(SeekFrom::Current(offset))?;
+ } else {
+ // seek backwards by our remainder, and then by the offset
+ self.inner.seek(SeekFrom::Current(-remainder))?;
+ self.pos = self.cap; // empty the buffer
+ result = self.inner.seek(SeekFrom::Current(n))?;
+ }
+ } else {
+ // Seeking with Start/End doesn't care about our buffer length.
+ result = self.inner.seek(pos)?;
+ }
+ self.pos = self.cap; // empty the buffer
+ Ok(result)
+ }
+}
+
+/// Wraps a writer and buffers its output.
+///
+/// It can be excessively inefficient to work directly with something that
+/// implements [`Write`]. For example, every call to [`write`] on [`TcpStream`]
+/// results in a system call. A `BufWriter` keeps an in-memory buffer of data
+/// and writes it to an underlying writer in large, infrequent batches.
+///
+/// The buffer will be written out when the writer is dropped.
+///
+/// # Examples
+///
+/// Let's write the numbers one through ten to a [`TcpStream`]:
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::net::TcpStream;
+///
+/// let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap();
+///
+/// for i in 1..10 {
+/// stream.write(&[i]).unwrap();
+/// }
+/// ```
+///
+/// Because we're not buffering, we write each one in turn, incurring the
+/// overhead of a system call per byte written. We can fix this with a
+/// `BufWriter`:
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::io::BufWriter;
+/// use std::net::TcpStream;
+///
+/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+///
+/// for i in 1..10 {
+/// stream.write(&[i]).unwrap();
+/// }
+/// ```
+///
+/// By wrapping the stream with a `BufWriter`, these ten writes are all grouped
+/// together by the buffer, and will all be written out in one system call when
+/// the `stream` is dropped.
+///
+/// [`Write`]: ../../std/io/trait.Write.html
+/// [`write`]: ../../std/net/struct.TcpStream.html#method.write
+/// [`TcpStream`]: ../../std/net/struct.TcpStream.html
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct BufWriter<W: Write> {
+ inner: Option<W>,
+ buf: Vec<u8>,
+ // #30888: If the inner writer panics in a call to write, we don't want to
+ // write the buffered data a second time in BufWriter's destructor. This
+ // flag tells the Drop impl if it should skip the flush.
+ panicked: bool,
+}
+
+/// An error returned by `into_inner` which combines an error that
+/// happened while writing out the buffer, and the buffered writer object
+/// which may be used to recover from the condition.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::io::BufWriter;
+/// use std::net::TcpStream;
+///
+/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+///
+/// // do stuff with the stream
+///
+/// // we want to get our `TcpStream` back, so let's try:
+///
+/// let stream = match stream.into_inner() {
+/// Ok(s) => s,
+/// Err(e) => {
+/// // Here, e is an IntoInnerError
+/// panic!("An error occurred");
+/// }
+/// };
+/// ```
+#[derive(Debug)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct IntoInnerError<W>(W, Error);
+
+impl<W: Write> BufWriter<W> {
+ /// Creates a new `BufWriter` with a default buffer capacity.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new(inner: W) -> BufWriter<W> {
+ BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
+ }
+
+ /// Creates a new `BufWriter` with the specified buffer capacity.
+ ///
+ /// # Examples
+ ///
+ /// Creating a buffer with a buffer of a hundred bytes.
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let stream = TcpStream::connect("127.0.0.1:34254").unwrap();
+ /// let mut buffer = BufWriter::with_capacity(100, stream);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn with_capacity(cap: usize, inner: W) -> BufWriter<W> {
+ BufWriter {
+ inner: Some(inner),
+ buf: Vec::with_capacity(cap),
+ panicked: false,
+ }
+ }
+
+ fn flush_buf(&mut self) -> io::Result<()> {
+ let mut written = 0;
+ let len = self.buf.len();
+ let mut ret = Ok(());
+ while written < len {
+ self.panicked = true;
+ let r = self.inner.as_mut().unwrap().write(&self.buf[written..]);
+ self.panicked = false;
+
+ match r {
+ Ok(0) => {
+ ret = Err(Error::new(ErrorKind::WriteZero,
+ "failed to write the buffered data"));
+ break;
+ }
+ Ok(n) => written += n,
+ Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
+ Err(e) => { ret = Err(e); break }
+
+ }
+ }
+ if written > 0 {
+ self.buf.drain(..written);
+ }
+ ret
+ }
+
+ /// Gets a reference to the underlying writer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // we can use reference just like buffer
+ /// let reference = buffer.get_ref();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_ref(&self) -> &W { self.inner.as_ref().unwrap() }
+
+ /// Gets a mutable reference to the underlying writer.
+ ///
+ /// It is inadvisable to directly write to the underlying writer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // we can use reference just like buffer
+ /// let reference = buffer.get_mut();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() }
+
+ /// Unwraps this `BufWriter`, returning the underlying writer.
+ ///
+ /// The buffer is written out before returning the writer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // unwrap the TcpStream and flush the buffer
+ /// let stream = buffer.into_inner().unwrap();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
+ match self.flush_buf() {
+ Err(e) => Err(IntoInnerError(self, e)),
+ Ok(()) => Ok(self.inner.take().unwrap())
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write> Write for BufWriter<W> {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ if self.buf.len() + buf.len() > self.buf.capacity() {
+ self.flush_buf()?;
+ }
+ if buf.len() >= self.buf.capacity() {
+ self.panicked = true;
+ let r = self.inner.as_mut().unwrap().write(buf);
+ self.panicked = false;
+ r
+ } else {
+ Write::write(&mut self.buf, buf)
+ }
+ }
+ fn flush(&mut self) -> io::Result<()> {
+ self.flush_buf().and_then(|()| self.get_mut().flush())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write> fmt::Debug for BufWriter<W> where W: fmt::Debug {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ fmt.debug_struct("BufWriter")
+ .field("writer", &self.inner.as_ref().unwrap())
+ .field("buffer", &format_args!("{}/{}", self.buf.len(), self.buf.capacity()))
+ .finish()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write + Seek> Seek for BufWriter<W> {
+ /// Seek to the offset, in bytes, in the underlying writer.
+ ///
+ /// Seeking always writes out the internal buffer before seeking.
+ fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+ self.flush_buf().and_then(|_| self.get_mut().seek(pos))
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write> Drop for BufWriter<W> {
+ fn drop(&mut self) {
+ if self.inner.is_some() && !self.panicked {
+ // dtors should not panic, so we ignore a failed flush
+ let _r = self.flush_buf();
+ }
+ }
+}
+
+impl<W> IntoInnerError<W> {
+ /// Returns the error which caused the call to `into_inner()` to fail.
+ ///
+ /// This error was returned when attempting to write the internal buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // do stuff with the stream
+ ///
+ /// // we want to get our `TcpStream` back, so let's try:
+ ///
+ /// let stream = match stream.into_inner() {
+ /// Ok(s) => s,
+ /// Err(e) => {
+ /// // Here, e is an IntoInnerError, let's log the inner error.
+ /// //
+ /// // We'll just 'log' to stdout for this example.
+ /// println!("{}", e.error());
+ ///
+ /// panic!("An unexpected error occurred.");
+ /// }
+ /// };
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn error(&self) -> &Error { &self.1 }
+
+ /// Returns the buffered writer instance which generated the error.
+ ///
+ /// The returned object can be used for error recovery, such as
+ /// re-inspecting the buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::io::BufWriter;
+ /// use std::net::TcpStream;
+ ///
+ /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+ ///
+ /// // do stuff with the stream
+ ///
+ /// // we want to get our `TcpStream` back, so let's try:
+ ///
+ /// let stream = match stream.into_inner() {
+ /// Ok(s) => s,
+ /// Err(e) => {
+ /// // Here, e is an IntoInnerError, let's re-examine the buffer:
+ /// let buffer = e.into_inner();
+ ///
+ /// // do stuff to try to recover
+ ///
+ /// // afterwards, let's just return the stream
+ /// buffer.into_inner().unwrap()
+ /// }
+ /// };
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_inner(self) -> W { self.0 }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W> From<IntoInnerError<W>> for Error {
+ fn from(iie: IntoInnerError<W>) -> Error { iie.1 }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
+ fn description(&self) -> &str {
+ error::Error::description(self.error())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W> fmt::Display for IntoInnerError<W> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ self.error().fmt(f)
+ }
+}
+
+/// Wraps a writer and buffers output to it, flushing whenever a newline
+/// (`0x0a`, `'\n'`) is detected.
+///
+/// The [`BufWriter`][bufwriter] struct wraps a writer and buffers its output.
+/// But it only does this batched write when it goes out of scope, or when the
+/// internal buffer is full. Sometimes, you'd prefer to write each line as it's
+/// completed, rather than the entire buffer at once. Enter `LineWriter`. It
+/// does exactly that.
+///
+/// [bufwriter]: struct.BufWriter.html
+///
+/// If there's still a partial line in the buffer when the `LineWriter` is
+/// dropped, it will flush those contents.
+///
+/// # Examples
+///
+/// We can use `LineWriter` to write one line at a time, significantly
+/// reducing the number of actual writes to the file.
+///
+/// ```
+/// use std::fs::File;
+/// use std::io::prelude::*;
+/// use std::io::LineWriter;
+///
+/// # fn foo() -> std::io::Result<()> {
+/// let road_not_taken = b"I shall be telling this with a sigh
+/// Somewhere ages and ages hence:
+/// Two roads diverged in a wood, and I -
+/// I took the one less traveled by,
+/// And that has made all the difference.";
+///
+/// let file = try!(File::create("poem.txt"));
+/// let mut file = LineWriter::new(file);
+///
+/// for &byte in road_not_taken.iter() {
+/// file.write(&[byte]).unwrap();
+/// }
+///
+/// // let's check we did the right thing.
+/// let mut file = try!(File::open("poem.txt"));
+/// let mut contents = String::new();
+///
+/// try!(file.read_to_string(&mut contents));
+///
+/// assert_eq!(contents.as_bytes(), &road_not_taken[..]);
+/// # Ok(())
+/// # }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct LineWriter<W: Write> {
+ inner: BufWriter<W>,
+}
+
+impl<W: Write> LineWriter<W> {
+ /// Creates a new `LineWriter`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::fs::File;
+ /// use std::io::LineWriter;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let file = try!(File::create("poem.txt"));
+ /// let file = LineWriter::new(file);
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new(inner: W) -> LineWriter<W> {
+ // Lines typically aren't that long, don't use a giant buffer
+ LineWriter::with_capacity(1024, inner)
+ }
+
+ /// Creates a new `LineWriter` with a specified capacity for the internal
+ /// buffer.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::fs::File;
+ /// use std::io::LineWriter;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let file = try!(File::create("poem.txt"));
+ /// let file = LineWriter::with_capacity(100, file);
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn with_capacity(cap: usize, inner: W) -> LineWriter<W> {
+ LineWriter { inner: BufWriter::with_capacity(cap, inner) }
+ }
+
+ /// Gets a reference to the underlying writer.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::fs::File;
+ /// use std::io::LineWriter;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let file = try!(File::create("poem.txt"));
+ /// let file = LineWriter::new(file);
+ ///
+ /// let reference = file.get_ref();
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_ref(&self) -> &W { self.inner.get_ref() }
+
+ /// Gets a mutable reference to the underlying writer.
+ ///
+ /// Caution must be taken when calling methods on the mutable reference
+ /// returned as extra writes could corrupt the output stream.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::fs::File;
+ /// use std::io::LineWriter;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let file = try!(File::create("poem.txt"));
+ /// let mut file = LineWriter::new(file);
+ ///
+ /// // we can use reference just like file
+ /// let reference = file.get_mut();
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_mut(&mut self) -> &mut W { self.inner.get_mut() }
+
+ /// Unwraps this `LineWriter`, returning the underlying writer.
+ ///
+ /// The internal buffer is written out before returning the writer.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::fs::File;
+ /// use std::io::LineWriter;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let file = try!(File::create("poem.txt"));
+ ///
+ /// let writer: LineWriter<File> = LineWriter::new(file);
+ ///
+ /// let file: File = try!(writer.into_inner());
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
+ self.inner.into_inner().map_err(|IntoInnerError(buf, e)| {
+ IntoInnerError(LineWriter { inner: buf }, e)
+ })
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write> Write for LineWriter<W> {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ match memchr::memrchr(b'\n', buf) {
+ Some(i) => {
+ let n = self.inner.write(&buf[..i + 1])?;
+ if n != i + 1 || self.inner.flush().is_err() {
+ // Do not return errors on partial writes.
+ return Ok(n);
+ }
+ self.inner.write(&buf[i + 1..]).map(|i| n + i)
+ }
+ None => self.inner.write(buf),
+ }
+ }
+
+ fn flush(&mut self) -> io::Result<()> { self.inner.flush() }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write> fmt::Debug for LineWriter<W> where W: fmt::Debug {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ fmt.debug_struct("LineWriter")
+ .field("writer", &self.inner.inner)
+ .field("buffer",
+ &format_args!("{}/{}", self.inner.buf.len(), self.inner.buf.capacity()))
+ .finish()
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use io::prelude::*;
+ use io::{self, BufReader, BufWriter, LineWriter, SeekFrom};
+ use sync::atomic::{AtomicUsize, Ordering};
+ use thread;
+ use test;
+
+ /// A dummy reader intended at testing short-reads propagation.
+ pub struct ShortReader {
+ lengths: Vec<usize>,
+ }
+
+ impl Read for ShortReader {
+ fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
+ if self.lengths.is_empty() {
+ Ok(0)
+ } else {
+ Ok(self.lengths.remove(0))
+ }
+ }
+ }
+
+ #[test]
+ fn test_buffered_reader() {
+ let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
+ let mut reader = BufReader::with_capacity(2, inner);
+
+ let mut buf = [0, 0, 0];
+ let nread = reader.read(&mut buf);
+ assert_eq!(nread.unwrap(), 3);
+ let b: &[_] = &[5, 6, 7];
+ assert_eq!(buf, b);
+
+ let mut buf = [0, 0];
+ let nread = reader.read(&mut buf);
+ assert_eq!(nread.unwrap(), 2);
+ let b: &[_] = &[0, 1];
+ assert_eq!(buf, b);
+
+ let mut buf = [0];
+ let nread = reader.read(&mut buf);
+ assert_eq!(nread.unwrap(), 1);
+ let b: &[_] = &[2];
+ assert_eq!(buf, b);
+
+ let mut buf = [0, 0, 0];
+ let nread = reader.read(&mut buf);
+ assert_eq!(nread.unwrap(), 1);
+ let b: &[_] = &[3, 0, 0];
+ assert_eq!(buf, b);
+
+ let nread = reader.read(&mut buf);
+ assert_eq!(nread.unwrap(), 1);
+ let b: &[_] = &[4, 0, 0];
+ assert_eq!(buf, b);
+
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ }
+
+ #[test]
+ fn test_buffered_reader_seek() {
+ let inner: &[u8] = &[5, 6, 7, 0, 1, 2, 3, 4];
+ let mut reader = BufReader::with_capacity(2, io::Cursor::new(inner));
+
+ assert_eq!(reader.seek(SeekFrom::Start(3)).ok(), Some(3));
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1][..]));
+ assert_eq!(reader.seek(SeekFrom::Current(0)).ok(), Some(3));
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1][..]));
+ assert_eq!(reader.seek(SeekFrom::Current(1)).ok(), Some(4));
+ assert_eq!(reader.fill_buf().ok(), Some(&[1, 2][..]));
+ reader.consume(1);
+ assert_eq!(reader.seek(SeekFrom::Current(-2)).ok(), Some(3));
+ }
+
+ #[test]
+ fn test_buffered_reader_seek_underflow() {
+ // gimmick reader that yields its position modulo 256 for each byte
+ struct PositionReader {
+ pos: u64
+ }
+ impl Read for PositionReader {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ let len = buf.len();
+ for x in buf {
+ *x = self.pos as u8;
+ self.pos = self.pos.wrapping_add(1);
+ }
+ Ok(len)
+ }
+ }
+ impl Seek for PositionReader {
+ fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+ match pos {
+ SeekFrom::Start(n) => {
+ self.pos = n;
+ }
+ SeekFrom::Current(n) => {
+ self.pos = self.pos.wrapping_add(n as u64);
+ }
+ SeekFrom::End(n) => {
+ self.pos = u64::max_value().wrapping_add(n as u64);
+ }
+ }
+ Ok(self.pos)
+ }
+ }
+
+ let mut reader = BufReader::with_capacity(5, PositionReader { pos: 0 });
+ assert_eq!(reader.fill_buf().ok(), Some(&[0, 1, 2, 3, 4][..]));
+ assert_eq!(reader.seek(SeekFrom::End(-5)).ok(), Some(u64::max_value()-5));
+ assert_eq!(reader.fill_buf().ok().map(|s| s.len()), Some(5));
+ // the following seek will require two underlying seeks
+ let expected = 9223372036854775802;
+ assert_eq!(reader.seek(SeekFrom::Current(i64::min_value())).ok(), Some(expected));
+ assert_eq!(reader.fill_buf().ok().map(|s| s.len()), Some(5));
+ // seeking to 0 should empty the buffer.
+ assert_eq!(reader.seek(SeekFrom::Current(0)).ok(), Some(expected));
+ assert_eq!(reader.get_ref().pos, expected);
+ }
+
+ #[test]
+ fn test_buffered_writer() {
+ let inner = Vec::new();
+ let mut writer = BufWriter::with_capacity(2, inner);
+
+ writer.write(&[0, 1]).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1]);
+
+ writer.write(&[2]).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1]);
+
+ writer.write(&[3]).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1]);
+
+ writer.flush().unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3]);
+
+ writer.write(&[4]).unwrap();
+ writer.write(&[5]).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3]);
+
+ writer.write(&[6]).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5]);
+
+ writer.write(&[7, 8]).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]);
+
+ writer.write(&[9, 10, 11]).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
+
+ writer.flush().unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
+ }
+
+ #[test]
+ fn test_buffered_writer_inner_flushes() {
+ let mut w = BufWriter::with_capacity(3, Vec::new());
+ w.write(&[0, 1]).unwrap();
+ assert_eq!(*w.get_ref(), []);
+ let w = w.into_inner().unwrap();
+ assert_eq!(w, [0, 1]);
+ }
+
+ #[test]
+ fn test_buffered_writer_seek() {
+ let mut w = BufWriter::with_capacity(3, io::Cursor::new(Vec::new()));
+ w.write_all(&[0, 1, 2, 3, 4, 5]).unwrap();
+ w.write_all(&[6, 7]).unwrap();
+ assert_eq!(w.seek(SeekFrom::Current(0)).ok(), Some(8));
+ assert_eq!(&w.get_ref().get_ref()[..], &[0, 1, 2, 3, 4, 5, 6, 7][..]);
+ assert_eq!(w.seek(SeekFrom::Start(2)).ok(), Some(2));
+ w.write_all(&[8, 9]).unwrap();
+ assert_eq!(&w.into_inner().unwrap().into_inner()[..], &[0, 1, 8, 9, 4, 5, 6, 7]);
+ }
+
+ #[test]
+ fn test_read_until() {
+ let inner: &[u8] = &[0, 1, 2, 1, 0];
+ let mut reader = BufReader::with_capacity(2, inner);
+ let mut v = Vec::new();
+ reader.read_until(0, &mut v).unwrap();
+ assert_eq!(v, [0]);
+ v.truncate(0);
+ reader.read_until(2, &mut v).unwrap();
+ assert_eq!(v, [1, 2]);
+ v.truncate(0);
+ reader.read_until(1, &mut v).unwrap();
+ assert_eq!(v, [1]);
+ v.truncate(0);
+ reader.read_until(8, &mut v).unwrap();
+ assert_eq!(v, [0]);
+ v.truncate(0);
+ reader.read_until(9, &mut v).unwrap();
+ assert_eq!(v, []);
+ }
+
+ #[test]
+ fn test_line_buffer_fail_flush() {
+ // Issue #32085
+ struct FailFlushWriter<'a>(&'a mut Vec<u8>);
+
+ impl<'a> Write for FailFlushWriter<'a> {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ self.0.extend_from_slice(buf);
+ Ok(buf.len())
+ }
+ fn flush(&mut self) -> io::Result<()> {
+ Err(io::Error::new(io::ErrorKind::Other, "flush failed"))
+ }
+ }
+
+ let mut buf = Vec::new();
+ {
+ let mut writer = LineWriter::new(FailFlushWriter(&mut buf));
+ let to_write = b"abc\ndef";
+ if let Ok(written) = writer.write(to_write) {
+ assert!(written < to_write.len(), "didn't flush on new line");
+ // PASS
+ return;
+ }
+ }
+ assert!(buf.is_empty(), "write returned an error but wrote data");
+ }
+
+ #[test]
+ fn test_line_buffer() {
+ let mut writer = LineWriter::new(Vec::new());
+ writer.write(&[0]).unwrap();
+ assert_eq!(*writer.get_ref(), []);
+ writer.write(&[1]).unwrap();
+ assert_eq!(*writer.get_ref(), []);
+ writer.flush().unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1]);
+ writer.write(&[0, b'\n', 1, b'\n', 2]).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n']);
+ writer.flush().unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2]);
+ writer.write(&[3, b'\n']).unwrap();
+ assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n']);
+ }
+
+ #[test]
+ fn test_read_line() {
+ let in_buf: &[u8] = b"a\nb\nc";
+ let mut reader = BufReader::with_capacity(2, in_buf);
+ let mut s = String::new();
+ reader.read_line(&mut s).unwrap();
+ assert_eq!(s, "a\n");
+ s.truncate(0);
+ reader.read_line(&mut s).unwrap();
+ assert_eq!(s, "b\n");
+ s.truncate(0);
+ reader.read_line(&mut s).unwrap();
+ assert_eq!(s, "c");
+ s.truncate(0);
+ reader.read_line(&mut s).unwrap();
+ assert_eq!(s, "");
+ }
+
+ #[test]
+ fn test_lines() {
+ let in_buf: &[u8] = b"a\nb\nc";
+ let reader = BufReader::with_capacity(2, in_buf);
+ let mut it = reader.lines();
+ assert_eq!(it.next().unwrap().unwrap(), "a".to_string());
+ assert_eq!(it.next().unwrap().unwrap(), "b".to_string());
+ assert_eq!(it.next().unwrap().unwrap(), "c".to_string());
+ assert!(it.next().is_none());
+ }
+
+ #[test]
+ fn test_short_reads() {
+ let inner = ShortReader{lengths: vec![0, 1, 2, 0, 1, 0]};
+ let mut reader = BufReader::new(inner);
+ let mut buf = [0, 0];
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.read(&mut buf).unwrap(), 2);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ }
+
+ #[test]
+ fn read_char_buffered() {
+ let buf = [195, 159];
+ let reader = BufReader::with_capacity(1, &buf[..]);
+ assert_eq!(reader.chars().next().unwrap().unwrap(), 'ß');
+ }
+
+ #[test]
+ fn test_chars() {
+ let buf = [195, 159, b'a'];
+ let reader = BufReader::with_capacity(1, &buf[..]);
+ let mut it = reader.chars();
+ assert_eq!(it.next().unwrap().unwrap(), 'ß');
+ assert_eq!(it.next().unwrap().unwrap(), 'a');
+ assert!(it.next().is_none());
+ }
+
+ #[test]
+ #[should_panic]
+ fn dont_panic_in_drop_on_panicked_flush() {
+ struct FailFlushWriter;
+
+ impl Write for FailFlushWriter {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> { Ok(buf.len()) }
+ fn flush(&mut self) -> io::Result<()> {
+ Err(io::Error::last_os_error())
+ }
+ }
+
+ let writer = FailFlushWriter;
+ let _writer = BufWriter::new(writer);
+
+ // If writer panics *again* due to the flush error then the process will
+ // abort.
+ panic!();
+ }
+
+ #[test]
+ #[cfg_attr(target_os = "emscripten", ignore)]
+ fn panic_in_write_doesnt_flush_in_drop() {
+ static WRITES: AtomicUsize = AtomicUsize::new(0);
+
+ struct PanicWriter;
+
+ impl Write for PanicWriter {
+ fn write(&mut self, _: &[u8]) -> io::Result<usize> {
+ WRITES.fetch_add(1, Ordering::SeqCst);
+ panic!();
+ }
+ fn flush(&mut self) -> io::Result<()> { Ok(()) }
+ }
+
+ thread::spawn(|| {
+ let mut writer = BufWriter::new(PanicWriter);
+ let _ = writer.write(b"hello world");
+ let _ = writer.flush();
+ }).join().unwrap_err();
+
+ assert_eq!(WRITES.load(Ordering::SeqCst), 1);
+ }
+
+ #[bench]
+ fn bench_buffered_reader(b: &mut test::Bencher) {
+ b.iter(|| {
+ BufReader::new(io::empty())
+ });
+ }
+
+ #[bench]
+ fn bench_buffered_writer(b: &mut test::Bencher) {
+ b.iter(|| {
+ BufWriter::new(io::sink())
+ });
+ }
+}
diff --git a/ctr-std/src/io/cursor.rs b/ctr-std/src/io/cursor.rs
new file mode 100644
index 0000000..1b50233
--- /dev/null
+++ b/ctr-std/src/io/cursor.rs
@@ -0,0 +1,600 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use io::prelude::*;
+
+use core::convert::TryInto;
+use cmp;
+use io::{self, SeekFrom, Error, ErrorKind};
+
+/// A `Cursor` wraps another type and provides it with a
+/// [`Seek`] implementation.
+///
+/// `Cursor`s are typically used with in-memory buffers to allow them to
+/// implement [`Read`] and/or [`Write`], allowing these buffers to be used
+/// anywhere you might use a reader or writer that does actual I/O.
+///
+/// The standard library implements some I/O traits on various types which
+/// are commonly used as a buffer, like `Cursor<`[`Vec`]`<u8>>` and
+/// `Cursor<`[`&[u8]`][bytes]`>`.
+///
+/// # Examples
+///
+/// We may want to write bytes to a [`File`] in our production
+/// code, but use an in-memory buffer in our tests. We can do this with
+/// `Cursor`:
+///
+/// [`Seek`]: trait.Seek.html
+/// [`Read`]: ../../std/io/trait.Read.html
+/// [`Write`]: ../../std/io/trait.Write.html
+/// [`Vec`]: ../../std/vec/struct.Vec.html
+/// [bytes]: ../../std/primitive.slice.html
+/// [`File`]: ../fs/struct.File.html
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::io::{self, SeekFrom};
+/// use std::fs::File;
+///
+/// // a library function we've written
+/// fn write_ten_bytes_at_end<W: Write + Seek>(writer: &mut W) -> io::Result<()> {
+/// try!(writer.seek(SeekFrom::End(-10)));
+///
+/// for i in 0..10 {
+/// try!(writer.write(&[i]));
+/// }
+///
+/// // all went well
+/// Ok(())
+/// }
+///
+/// # fn foo() -> io::Result<()> {
+/// // Here's some code that uses this library function.
+/// //
+/// // We might want to use a BufReader here for efficiency, but let's
+/// // keep this example focused.
+/// let mut file = try!(File::create("foo.txt"));
+///
+/// try!(write_ten_bytes_at_end(&mut file));
+/// # Ok(())
+/// # }
+///
+/// // now let's write a test
+/// #[test]
+/// fn test_writes_bytes() {
+/// // setting up a real File is much more slow than an in-memory buffer,
+/// // let's use a cursor instead
+/// use std::io::Cursor;
+/// let mut buff = Cursor::new(vec![0; 15]);
+///
+/// write_ten_bytes_at_end(&mut buff).unwrap();
+///
+/// assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
+/// }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+#[derive(Clone, Debug)]
+pub struct Cursor<T> {
+ inner: T,
+ pos: u64,
+}
+
+impl<T> Cursor<T> {
+ /// Creates a new cursor wrapping the provided underlying I/O object.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ ///
+ /// let buff = Cursor::new(Vec::new());
+ /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+ /// # force_inference(&buff);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new(inner: T) -> Cursor<T> {
+ Cursor { pos: 0, inner: inner }
+ }
+
+ /// Consumes this cursor, returning the underlying value.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ ///
+ /// let buff = Cursor::new(Vec::new());
+ /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+ /// # force_inference(&buff);
+ ///
+ /// let vec = buff.into_inner();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_inner(self) -> T { self.inner }
+
+ /// Gets a reference to the underlying value in this cursor.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ ///
+ /// let buff = Cursor::new(Vec::new());
+ /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+ /// # force_inference(&buff);
+ ///
+ /// let reference = buff.get_ref();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_ref(&self) -> &T { &self.inner }
+
+ /// Gets a mutable reference to the underlying value in this cursor.
+ ///
+ /// Care should be taken to avoid modifying the internal I/O state of the
+ /// underlying value as it may corrupt this cursor's position.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ ///
+ /// let mut buff = Cursor::new(Vec::new());
+ /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+ /// # force_inference(&buff);
+ ///
+ /// let reference = buff.get_mut();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn get_mut(&mut self) -> &mut T { &mut self.inner }
+
+ /// Returns the current position of this cursor.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ /// use std::io::prelude::*;
+ /// use std::io::SeekFrom;
+ ///
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
+ ///
+ /// assert_eq!(buff.position(), 0);
+ ///
+ /// buff.seek(SeekFrom::Current(2)).unwrap();
+ /// assert_eq!(buff.position(), 2);
+ ///
+ /// buff.seek(SeekFrom::Current(-1)).unwrap();
+ /// assert_eq!(buff.position(), 1);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn position(&self) -> u64 { self.pos }
+
+ /// Sets the position of this cursor.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Cursor;
+ ///
+ /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
+ ///
+ /// assert_eq!(buff.position(), 0);
+ ///
+ /// buff.set_position(2);
+ /// assert_eq!(buff.position(), 2);
+ ///
+ /// buff.set_position(4);
+ /// assert_eq!(buff.position(), 4);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn set_position(&mut self, pos: u64) { self.pos = pos; }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> io::Seek for Cursor<T> where T: AsRef<[u8]> {
+ fn seek(&mut self, style: SeekFrom) -> io::Result<u64> {
+ let pos = match style {
+ SeekFrom::Start(n) => { self.pos = n; return Ok(n) }
+ SeekFrom::End(n) => self.inner.as_ref().len() as i64 + n,
+ SeekFrom::Current(n) => self.pos as i64 + n,
+ };
+
+ if pos < 0 {
+ Err(Error::new(ErrorKind::InvalidInput,
+ "invalid seek to a negative position"))
+ } else {
+ self.pos = pos as u64;
+ Ok(self.pos)
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> Read for Cursor<T> where T: AsRef<[u8]> {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ let n = Read::read(&mut self.fill_buf()?, buf)?;
+ self.pos += n as u64;
+ Ok(n)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> BufRead for Cursor<T> where T: AsRef<[u8]> {
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ let amt = cmp::min(self.pos, self.inner.as_ref().len() as u64);
+ Ok(&self.inner.as_ref()[(amt as usize)..])
+ }
+ fn consume(&mut self, amt: usize) { self.pos += amt as u64; }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> Write for Cursor<&'a mut [u8]> {
+ #[inline]
+ fn write(&mut self, data: &[u8]) -> io::Result<usize> {
+ let pos = cmp::min(self.pos, self.inner.len() as u64);
+ let amt = (&mut self.inner[(pos as usize)..]).write(data)?;
+ self.pos += amt as u64;
+ Ok(amt)
+ }
+ fn flush(&mut self) -> io::Result<()> { Ok(()) }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Write for Cursor<Vec<u8>> {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ let pos: usize = self.position().try_into().map_err(|_| {
+ Error::new(ErrorKind::InvalidInput,
+ "cursor position exceeds maximum possible vector length")
+ })?;
+ // Make sure the internal buffer is as least as big as where we
+ // currently are
+ let len = self.inner.len();
+ if len < pos {
+ // use `resize` so that the zero filling is as efficient as possible
+ self.inner.resize(pos, 0);
+ }
+ // Figure out what bytes will be used to overwrite what's currently
+ // there (left), and what will be appended on the end (right)
+ {
+ let space = self.inner.len() - pos;
+ let (left, right) = buf.split_at(cmp::min(space, buf.len()));
+ self.inner[pos..pos + left.len()].copy_from_slice(left);
+ self.inner.extend_from_slice(right);
+ }
+
+ // Bump us forward
+ self.set_position((pos + buf.len()) as u64);
+ Ok(buf.len())
+ }
+ fn flush(&mut self) -> io::Result<()> { Ok(()) }
+}
+
+#[stable(feature = "cursor_box_slice", since = "1.5.0")]
+impl Write for Cursor<Box<[u8]>> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ let pos = cmp::min(self.pos, self.inner.len() as u64);
+ let amt = (&mut self.inner[(pos as usize)..]).write(buf)?;
+ self.pos += amt as u64;
+ Ok(amt)
+ }
+ fn flush(&mut self) -> io::Result<()> { Ok(()) }
+}
+
+#[cfg(test)]
+mod tests {
+ use io::prelude::*;
+ use io::{Cursor, SeekFrom};
+
+ #[test]
+ fn test_vec_writer() {
+ let mut writer = Vec::new();
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
+ assert_eq!(writer, b);
+ }
+
+ #[test]
+ fn test_mem_writer() {
+ let mut writer = Cursor::new(Vec::new());
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
+ assert_eq!(&writer.get_ref()[..], b);
+ }
+
+ #[test]
+ fn test_box_slice_writer() {
+ let mut writer = Cursor::new(vec![0u8; 9].into_boxed_slice());
+ assert_eq!(writer.position(), 0);
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.position(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ assert_eq!(writer.position(), 8);
+ assert_eq!(writer.write(&[]).unwrap(), 0);
+ assert_eq!(writer.position(), 8);
+
+ assert_eq!(writer.write(&[8, 9]).unwrap(), 1);
+ assert_eq!(writer.write(&[10]).unwrap(), 0);
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
+ assert_eq!(&**writer.get_ref(), b);
+ }
+
+ #[test]
+ fn test_buf_writer() {
+ let mut buf = [0 as u8; 9];
+ {
+ let mut writer = Cursor::new(&mut buf[..]);
+ assert_eq!(writer.position(), 0);
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.position(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ assert_eq!(writer.position(), 8);
+ assert_eq!(writer.write(&[]).unwrap(), 0);
+ assert_eq!(writer.position(), 8);
+
+ assert_eq!(writer.write(&[8, 9]).unwrap(), 1);
+ assert_eq!(writer.write(&[10]).unwrap(), 0);
+ }
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
+ assert_eq!(buf, b);
+ }
+
+ #[test]
+ fn test_buf_writer_seek() {
+ let mut buf = [0 as u8; 8];
+ {
+ let mut writer = Cursor::new(&mut buf[..]);
+ assert_eq!(writer.position(), 0);
+ assert_eq!(writer.write(&[1]).unwrap(), 1);
+ assert_eq!(writer.position(), 1);
+
+ assert_eq!(writer.seek(SeekFrom::Start(2)).unwrap(), 2);
+ assert_eq!(writer.position(), 2);
+ assert_eq!(writer.write(&[2]).unwrap(), 1);
+ assert_eq!(writer.position(), 3);
+
+ assert_eq!(writer.seek(SeekFrom::Current(-2)).unwrap(), 1);
+ assert_eq!(writer.position(), 1);
+ assert_eq!(writer.write(&[3]).unwrap(), 1);
+ assert_eq!(writer.position(), 2);
+
+ assert_eq!(writer.seek(SeekFrom::End(-1)).unwrap(), 7);
+ assert_eq!(writer.position(), 7);
+ assert_eq!(writer.write(&[4]).unwrap(), 1);
+ assert_eq!(writer.position(), 8);
+
+ }
+ let b: &[_] = &[1, 3, 2, 0, 0, 0, 0, 4];
+ assert_eq!(buf, b);
+ }
+
+ #[test]
+ fn test_buf_writer_error() {
+ let mut buf = [0 as u8; 2];
+ let mut writer = Cursor::new(&mut buf[..]);
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.write(&[0, 0]).unwrap(), 1);
+ assert_eq!(writer.write(&[0, 0]).unwrap(), 0);
+ }
+
+ #[test]
+ fn test_mem_reader() {
+ let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7]);
+ let mut buf = [];
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.position(), 0);
+ let mut buf = [0];
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.position(), 1);
+ let b: &[_] = &[0];
+ assert_eq!(buf, b);
+ let mut buf = [0; 4];
+ assert_eq!(reader.read(&mut buf).unwrap(), 4);
+ assert_eq!(reader.position(), 5);
+ let b: &[_] = &[1, 2, 3, 4];
+ assert_eq!(buf, b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 3);
+ let b: &[_] = &[5, 6, 7];
+ assert_eq!(&buf[..3], b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ }
+
+ #[test]
+ fn test_boxed_slice_reader() {
+ let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7].into_boxed_slice());
+ let mut buf = [];
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.position(), 0);
+ let mut buf = [0];
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.position(), 1);
+ let b: &[_] = &[0];
+ assert_eq!(buf, b);
+ let mut buf = [0; 4];
+ assert_eq!(reader.read(&mut buf).unwrap(), 4);
+ assert_eq!(reader.position(), 5);
+ let b: &[_] = &[1, 2, 3, 4];
+ assert_eq!(buf, b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 3);
+ let b: &[_] = &[5, 6, 7];
+ assert_eq!(&buf[..3], b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ }
+
+ #[test]
+ fn read_to_end() {
+ let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7]);
+ let mut v = Vec::new();
+ reader.read_to_end(&mut v).unwrap();
+ assert_eq!(v, [0, 1, 2, 3, 4, 5, 6, 7]);
+ }
+
+ #[test]
+ fn test_slice_reader() {
+ let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
+ let mut reader = &mut &in_buf[..];
+ let mut buf = [];
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ let mut buf = [0];
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.len(), 7);
+ let b: &[_] = &[0];
+ assert_eq!(&buf[..], b);
+ let mut buf = [0; 4];
+ assert_eq!(reader.read(&mut buf).unwrap(), 4);
+ assert_eq!(reader.len(), 3);
+ let b: &[_] = &[1, 2, 3, 4];
+ assert_eq!(&buf[..], b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 3);
+ let b: &[_] = &[5, 6, 7];
+ assert_eq!(&buf[..3], b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ }
+
+ #[test]
+ fn test_buf_reader() {
+ let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
+ let mut reader = Cursor::new(&in_buf[..]);
+ let mut buf = [];
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ assert_eq!(reader.position(), 0);
+ let mut buf = [0];
+ assert_eq!(reader.read(&mut buf).unwrap(), 1);
+ assert_eq!(reader.position(), 1);
+ let b: &[_] = &[0];
+ assert_eq!(buf, b);
+ let mut buf = [0; 4];
+ assert_eq!(reader.read(&mut buf).unwrap(), 4);
+ assert_eq!(reader.position(), 5);
+ let b: &[_] = &[1, 2, 3, 4];
+ assert_eq!(buf, b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 3);
+ let b: &[_] = &[5, 6, 7];
+ assert_eq!(&buf[..3], b);
+ assert_eq!(reader.read(&mut buf).unwrap(), 0);
+ }
+
+ #[test]
+ fn test_read_char() {
+ let b = &b"Vi\xE1\xBB\x87t"[..];
+ let mut c = Cursor::new(b).chars();
+ assert_eq!(c.next().unwrap().unwrap(), 'V');
+ assert_eq!(c.next().unwrap().unwrap(), 'i');
+ assert_eq!(c.next().unwrap().unwrap(), 'ệ');
+ assert_eq!(c.next().unwrap().unwrap(), 't');
+ assert!(c.next().is_none());
+ }
+
+ #[test]
+ fn test_read_bad_char() {
+ let b = &b"\x80"[..];
+ let mut c = Cursor::new(b).chars();
+ assert!(c.next().unwrap().is_err());
+ }
+
+ #[test]
+ fn seek_past_end() {
+ let buf = [0xff];
+ let mut r = Cursor::new(&buf[..]);
+ assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
+ assert_eq!(r.read(&mut [0]).unwrap(), 0);
+
+ let mut r = Cursor::new(vec![10]);
+ assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
+ assert_eq!(r.read(&mut [0]).unwrap(), 0);
+
+ let mut buf = [0];
+ let mut r = Cursor::new(&mut buf[..]);
+ assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
+ assert_eq!(r.write(&[3]).unwrap(), 0);
+
+ let mut r = Cursor::new(vec![10].into_boxed_slice());
+ assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
+ assert_eq!(r.write(&[3]).unwrap(), 0);
+ }
+
+ #[test]
+ fn seek_before_0() {
+ let buf = [0xff];
+ let mut r = Cursor::new(&buf[..]);
+ assert!(r.seek(SeekFrom::End(-2)).is_err());
+
+ let mut r = Cursor::new(vec![10]);
+ assert!(r.seek(SeekFrom::End(-2)).is_err());
+
+ let mut buf = [0];
+ let mut r = Cursor::new(&mut buf[..]);
+ assert!(r.seek(SeekFrom::End(-2)).is_err());
+
+ let mut r = Cursor::new(vec![10].into_boxed_slice());
+ assert!(r.seek(SeekFrom::End(-2)).is_err());
+ }
+
+ #[test]
+ fn test_seekable_mem_writer() {
+ let mut writer = Cursor::new(Vec::<u8>::new());
+ assert_eq!(writer.position(), 0);
+ assert_eq!(writer.write(&[0]).unwrap(), 1);
+ assert_eq!(writer.position(), 1);
+ assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
+ assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
+ assert_eq!(writer.position(), 8);
+ let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
+ assert_eq!(&writer.get_ref()[..], b);
+
+ assert_eq!(writer.seek(SeekFrom::Start(0)).unwrap(), 0);
+ assert_eq!(writer.position(), 0);
+ assert_eq!(writer.write(&[3, 4]).unwrap(), 2);
+ let b: &[_] = &[3, 4, 2, 3, 4, 5, 6, 7];
+ assert_eq!(&writer.get_ref()[..], b);
+
+ assert_eq!(writer.seek(SeekFrom::Current(1)).unwrap(), 3);
+ assert_eq!(writer.write(&[0, 1]).unwrap(), 2);
+ let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 7];
+ assert_eq!(&writer.get_ref()[..], b);
+
+ assert_eq!(writer.seek(SeekFrom::End(-1)).unwrap(), 7);
+ assert_eq!(writer.write(&[1, 2]).unwrap(), 2);
+ let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 1, 2];
+ assert_eq!(&writer.get_ref()[..], b);
+
+ assert_eq!(writer.seek(SeekFrom::End(1)).unwrap(), 10);
+ assert_eq!(writer.write(&[1]).unwrap(), 1);
+ let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 1, 2, 0, 1];
+ assert_eq!(&writer.get_ref()[..], b);
+ }
+
+ #[test]
+ fn vec_seek_past_end() {
+ let mut r = Cursor::new(Vec::new());
+ assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
+ assert_eq!(r.write(&[3]).unwrap(), 1);
+ }
+
+ #[test]
+ fn vec_seek_before_0() {
+ let mut r = Cursor::new(Vec::new());
+ assert!(r.seek(SeekFrom::End(-2)).is_err());
+ }
+
+ #[test]
+ #[cfg(target_pointer_width = "32")]
+ fn vec_seek_and_write_past_usize_max() {
+ let mut c = Cursor::new(Vec::new());
+ c.set_position(<usize>::max_value() as u64 + 1);
+ assert!(c.write_all(&[1, 2, 3]).is_err());
+ }
+}
diff --git a/ctr-std/src/io/error.rs b/ctr-std/src/io/error.rs
new file mode 100644
index 0000000..795c89c
--- /dev/null
+++ b/ctr-std/src/io/error.rs
@@ -0,0 +1,594 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use error;
+use fmt;
+use result;
+use sys;
+use convert::From;
+
+/// A specialized [`Result`](../result/enum.Result.html) type for I/O
+/// operations.
+///
+/// This type is broadly used across `std::io` for any operation which may
+/// produce an error.
+///
+/// This typedef is generally used to avoid writing out `io::Error` directly and
+/// is otherwise a direct mapping to `Result`.
+///
+/// While usual Rust style is to import types directly, aliases of `Result`
+/// often are not, to make it easier to distinguish between them. `Result` is
+/// generally assumed to be `std::result::Result`, and so users of this alias
+/// will generally use `io::Result` instead of shadowing the prelude's import
+/// of `std::result::Result`.
+///
+/// # Examples
+///
+/// A convenience function that bubbles an `io::Result` to its caller:
+///
+/// ```
+/// use std::io;
+///
+/// fn get_string() -> io::Result<String> {
+/// let mut buffer = String::new();
+///
+/// try!(io::stdin().read_line(&mut buffer));
+///
+/// Ok(buffer)
+/// }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub type Result<T> = result::Result<T, Error>;
+
+/// The error type for I/O operations of the `Read`, `Write`, `Seek`, and
+/// associated traits.
+///
+/// Errors mostly originate from the underlying OS, but custom instances of
+/// `Error` can be created with crafted error messages and a particular value of
+/// [`ErrorKind`].
+///
+/// [`ErrorKind`]: enum.ErrorKind.html
+#[derive(Debug)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Error {
+ repr: Repr,
+}
+
+enum Repr {
+ Os(i32),
+ Simple(ErrorKind),
+ Custom(Box<Custom>),
+}
+
+#[derive(Debug)]
+struct Custom {
+ kind: ErrorKind,
+ error: Box<error::Error+Send+Sync>,
+}
+
+/// A list specifying general categories of I/O error.
+///
+/// This list is intended to grow over time and it is not recommended to
+/// exhaustively match against it.
+///
+/// It is used with the [`io::Error`] type.
+///
+/// [`io::Error`]: struct.Error.html
+#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
+#[stable(feature = "rust1", since = "1.0.0")]
+#[allow(deprecated)]
+pub enum ErrorKind {
+ /// An entity was not found, often a file.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ NotFound,
+ /// The operation lacked the necessary privileges to complete.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ PermissionDenied,
+ /// The connection was refused by the remote server.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ ConnectionRefused,
+ /// The connection was reset by the remote server.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ ConnectionReset,
+ /// The connection was aborted (terminated) by the remote server.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ ConnectionAborted,
+ /// The network operation failed because it was not connected yet.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ NotConnected,
+ /// A socket address could not be bound because the address is already in
+ /// use elsewhere.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ AddrInUse,
+ /// A nonexistent interface was requested or the requested address was not
+ /// local.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ AddrNotAvailable,
+ /// The operation failed because a pipe was closed.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ BrokenPipe,
+ /// An entity already exists, often a file.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ AlreadyExists,
+ /// The operation needs to block to complete, but the blocking operation was
+ /// requested to not occur.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ WouldBlock,
+ /// A parameter was incorrect.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ InvalidInput,
+ /// Data not valid for the operation were encountered.
+ ///
+ /// Unlike [`InvalidInput`], this typically means that the operation
+ /// parameters were valid, however the error was caused by malformed
+ /// input data.
+ ///
+ /// For example, a function that reads a file into a string will error with
+ /// `InvalidData` if the file's contents are not valid UTF-8.
+ ///
+ /// [`InvalidInput`]: #variant.InvalidInput
+ #[stable(feature = "io_invalid_data", since = "1.2.0")]
+ InvalidData,
+ /// The I/O operation's timeout expired, causing it to be canceled.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ TimedOut,
+ /// An error returned when an operation could not be completed because a
+ /// call to [`write()`] returned [`Ok(0)`].
+ ///
+ /// This typically means that an operation could only succeed if it wrote a
+ /// particular number of bytes but only a smaller number of bytes could be
+ /// written.
+ ///
+ /// [`write()`]: ../../std/io/trait.Write.html#tymethod.write
+ /// [`Ok(0)`]: ../../std/io/type.Result.html
+ #[stable(feature = "rust1", since = "1.0.0")]
+ WriteZero,
+ /// This operation was interrupted.
+ ///
+ /// Interrupted operations can typically be retried.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Interrupted,
+ /// Any I/O error not part of this list.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Other,
+
+ /// An error returned when an operation could not be completed because an
+ /// "end of file" was reached prematurely.
+ ///
+ /// This typically means that an operation could only succeed if it read a
+ /// particular number of bytes but only a smaller number of bytes could be
+ /// read.
+ #[stable(feature = "read_exact", since = "1.6.0")]
+ UnexpectedEof,
+
+ /// A marker variant that tells the compiler that users of this enum cannot
+ /// match it exhaustively.
+ #[unstable(feature = "io_error_internals",
+ reason = "better expressed through extensible enums that this \
+ enum cannot be exhaustively matched against",
+ issue = "0")]
+ #[doc(hidden)]
+ __Nonexhaustive,
+}
+
+impl ErrorKind {
+ fn as_str(&self) -> &'static str {
+ match *self {
+ ErrorKind::NotFound => "entity not found",
+ ErrorKind::PermissionDenied => "permission denied",
+ ErrorKind::ConnectionRefused => "connection refused",
+ ErrorKind::ConnectionReset => "connection reset",
+ ErrorKind::ConnectionAborted => "connection aborted",
+ ErrorKind::NotConnected => "not connected",
+ ErrorKind::AddrInUse => "address in use",
+ ErrorKind::AddrNotAvailable => "address not available",
+ ErrorKind::BrokenPipe => "broken pipe",
+ ErrorKind::AlreadyExists => "entity already exists",
+ ErrorKind::WouldBlock => "operation would block",
+ ErrorKind::InvalidInput => "invalid input parameter",
+ ErrorKind::InvalidData => "invalid data",
+ ErrorKind::TimedOut => "timed out",
+ ErrorKind::WriteZero => "write zero",
+ ErrorKind::Interrupted => "operation interrupted",
+ ErrorKind::Other => "other os error",
+ ErrorKind::UnexpectedEof => "unexpected end of file",
+ ErrorKind::__Nonexhaustive => unreachable!()
+ }
+ }
+}
+
+/// Intended for use for errors not exposed to the user, where allocating onto
+/// the heap (for normal construction via Error::new) is too costly.
+#[stable(feature = "io_error_from_errorkind", since = "1.14.0")]
+impl From<ErrorKind> for Error {
+ fn from(kind: ErrorKind) -> Error {
+ Error {
+ repr: Repr::Simple(kind)
+ }
+ }
+}
+
+impl Error {
+ /// Creates a new I/O error from a known kind of error as well as an
+ /// arbitrary error payload.
+ ///
+ /// This function is used to generically create I/O errors which do not
+ /// originate from the OS itself. The `error` argument is an arbitrary
+ /// payload which will be contained in this `Error`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// // errors can be created from strings
+ /// let custom_error = Error::new(ErrorKind::Other, "oh no!");
+ ///
+ /// // errors can also be created from other errors
+ /// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new<E>(kind: ErrorKind, error: E) -> Error
+ where E: Into<Box<error::Error+Send+Sync>>
+ {
+ Self::_new(kind, error.into())
+ }
+
+ fn _new(kind: ErrorKind, error: Box<error::Error+Send+Sync>) -> Error {
+ Error {
+ repr: Repr::Custom(Box::new(Custom {
+ kind: kind,
+ error: error,
+ }))
+ }
+ }
+
+ /// Returns an error representing the last OS error which occurred.
+ ///
+ /// This function reads the value of `errno` for the target platform (e.g.
+ /// `GetLastError` on Windows) and will return a corresponding instance of
+ /// `Error` for the error code.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Error;
+ ///
+ /// println!("last OS error: {:?}", Error::last_os_error());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn last_os_error() -> Error {
+ Error::from_raw_os_error(sys::os::errno() as i32)
+ }
+
+ /// Creates a new instance of an `Error` from a particular OS error code.
+ ///
+ /// # Examples
+ ///
+ /// On Linux:
+ ///
+ /// ```
+ /// # if cfg!(target_os = "linux") {
+ /// use std::io;
+ ///
+ /// let error = io::Error::from_raw_os_error(98);
+ /// assert_eq!(error.kind(), io::ErrorKind::AddrInUse);
+ /// # }
+ /// ```
+ ///
+ /// On Windows:
+ ///
+ /// ```
+ /// # if cfg!(windows) {
+ /// use std::io;
+ ///
+ /// let error = io::Error::from_raw_os_error(10048);
+ /// assert_eq!(error.kind(), io::ErrorKind::AddrInUse);
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn from_raw_os_error(code: i32) -> Error {
+ Error { repr: Repr::Os(code) }
+ }
+
+ /// Returns the OS error that this error represents (if any).
+ ///
+ /// If this `Error` was constructed via `last_os_error` or
+ /// `from_raw_os_error`, then this function will return `Some`, otherwise
+ /// it will return `None`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// fn print_os_error(err: &Error) {
+ /// if let Some(raw_os_err) = err.raw_os_error() {
+ /// println!("raw OS error: {:?}", raw_os_err);
+ /// } else {
+ /// println!("Not an OS error");
+ /// }
+ /// }
+ ///
+ /// fn main() {
+ /// // Will print "raw OS error: ...".
+ /// print_os_error(&Error::last_os_error());
+ /// // Will print "Not an OS error".
+ /// print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn raw_os_error(&self) -> Option<i32> {
+ match self.repr {
+ Repr::Os(i) => Some(i),
+ Repr::Custom(..) => None,
+ Repr::Simple(..) => None,
+ }
+ }
+
+ /// Returns a reference to the inner error wrapped by this error (if any).
+ ///
+ /// If this `Error` was constructed via `new` then this function will
+ /// return `Some`, otherwise it will return `None`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// fn print_error(err: &Error) {
+ /// if let Some(inner_err) = err.get_ref() {
+ /// println!("Inner error: {:?}", inner_err);
+ /// } else {
+ /// println!("No inner error");
+ /// }
+ /// }
+ ///
+ /// fn main() {
+ /// // Will print "No inner error".
+ /// print_error(&Error::last_os_error());
+ /// // Will print "Inner error: ...".
+ /// print_error(&Error::new(ErrorKind::Other, "oh no!"));
+ /// }
+ /// ```
+ #[stable(feature = "io_error_inner", since = "1.3.0")]
+ pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> {
+ match self.repr {
+ Repr::Os(..) => None,
+ Repr::Simple(..) => None,
+ Repr::Custom(ref c) => Some(&*c.error),
+ }
+ }
+
+ /// Returns a mutable reference to the inner error wrapped by this error
+ /// (if any).
+ ///
+ /// If this `Error` was constructed via `new` then this function will
+ /// return `Some`, otherwise it will return `None`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ /// use std::{error, fmt};
+ /// use std::fmt::Display;
+ ///
+ /// #[derive(Debug)]
+ /// struct MyError {
+ /// v: String,
+ /// }
+ ///
+ /// impl MyError {
+ /// fn new() -> MyError {
+ /// MyError {
+ /// v: "oh no!".to_owned()
+ /// }
+ /// }
+ ///
+ /// fn change_message(&mut self, new_message: &str) {
+ /// self.v = new_message.to_owned();
+ /// }
+ /// }
+ ///
+ /// impl error::Error for MyError {
+ /// fn description(&self) -> &str { &self.v }
+ /// }
+ ///
+ /// impl Display for MyError {
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// write!(f, "MyError: {}", &self.v)
+ /// }
+ /// }
+ ///
+ /// fn change_error(mut err: Error) -> Error {
+ /// if let Some(inner_err) = err.get_mut() {
+ /// inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
+ /// }
+ /// err
+ /// }
+ ///
+ /// fn print_error(err: &Error) {
+ /// if let Some(inner_err) = err.get_ref() {
+ /// println!("Inner error: {}", inner_err);
+ /// } else {
+ /// println!("No inner error");
+ /// }
+ /// }
+ ///
+ /// fn main() {
+ /// // Will print "No inner error".
+ /// print_error(&change_error(Error::last_os_error()));
+ /// // Will print "Inner error: ...".
+ /// print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
+ /// }
+ /// ```
+ #[stable(feature = "io_error_inner", since = "1.3.0")]
+ pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> {
+ match self.repr {
+ Repr::Os(..) => None,
+ Repr::Simple(..) => None,
+ Repr::Custom(ref mut c) => Some(&mut *c.error),
+ }
+ }
+
+ /// Consumes the `Error`, returning its inner error (if any).
+ ///
+ /// If this `Error` was constructed via `new` then this function will
+ /// return `Some`, otherwise it will return `None`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// fn print_error(err: Error) {
+ /// if let Some(inner_err) = err.into_inner() {
+ /// println!("Inner error: {}", inner_err);
+ /// } else {
+ /// println!("No inner error");
+ /// }
+ /// }
+ ///
+ /// fn main() {
+ /// // Will print "No inner error".
+ /// print_error(Error::last_os_error());
+ /// // Will print "Inner error: ...".
+ /// print_error(Error::new(ErrorKind::Other, "oh no!"));
+ /// }
+ /// ```
+ #[stable(feature = "io_error_inner", since = "1.3.0")]
+ pub fn into_inner(self) -> Option<Box<error::Error+Send+Sync>> {
+ match self.repr {
+ Repr::Os(..) => None,
+ Repr::Simple(..) => None,
+ Repr::Custom(c) => Some(c.error)
+ }
+ }
+
+ /// Returns the corresponding `ErrorKind` for this error.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::{Error, ErrorKind};
+ ///
+ /// fn print_error(err: Error) {
+ /// println!("{:?}", err.kind());
+ /// }
+ ///
+ /// fn main() {
+ /// // Will print "No inner error".
+ /// print_error(Error::last_os_error());
+ /// // Will print "Inner error: ...".
+ /// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn kind(&self) -> ErrorKind {
+ match self.repr {
+ Repr::Os(code) => sys::decode_error_kind(code),
+ Repr::Custom(ref c) => c.kind,
+ Repr::Simple(kind) => kind,
+ }
+ }
+}
+
+impl fmt::Debug for Repr {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ match *self {
+ Repr::Os(ref code) =>
+ fmt.debug_struct("Os").field("code", code)
+ .field("message", &sys::os::error_string(*code)).finish(),
+ Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(),
+ Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl fmt::Display for Error {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ match self.repr {
+ Repr::Os(code) => {
+ let detail = sys::os::error_string(code);
+ write!(fmt, "{} (os error {})", detail, code)
+ }
+ Repr::Custom(ref c) => c.error.fmt(fmt),
+ Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl error::Error for Error {
+ fn description(&self) -> &str {
+ match self.repr {
+ Repr::Os(..) | Repr::Simple(..) => self.kind().as_str(),
+ Repr::Custom(ref c) => c.error.description(),
+ }
+ }
+
+ fn cause(&self) -> Option<&error::Error> {
+ match self.repr {
+ Repr::Os(..) => None,
+ Repr::Simple(..) => None,
+ Repr::Custom(ref c) => c.error.cause(),
+ }
+ }
+}
+
+fn _assert_error_is_sync_send() {
+ fn _is_sync_send<T: Sync+Send>() {}
+ _is_sync_send::<Error>();
+}
+
+#[cfg(test)]
+mod test {
+ use super::{Error, ErrorKind};
+ use error;
+ use fmt;
+ use sys::os::error_string;
+
+ #[test]
+ fn test_debug_error() {
+ let code = 6;
+ let msg = error_string(code);
+ let err = Error { repr: super::Repr::Os(code) };
+ let expected = format!("Error {{ repr: Os {{ code: {:?}, message: {:?} }} }}", code, msg);
+ assert_eq!(format!("{:?}", err), expected);
+ }
+
+ #[test]
+ fn test_downcasting() {
+ #[derive(Debug)]
+ struct TestError;
+
+ impl fmt::Display for TestError {
+ fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
+ Ok(())
+ }
+ }
+
+ impl error::Error for TestError {
+ fn description(&self) -> &str {
+ "asdf"
+ }
+ }
+
+ // we have to call all of these UFCS style right now since method
+ // resolution won't implicitly drop the Send+Sync bounds
+ let mut err = Error::new(ErrorKind::Other, TestError);
+ assert!(err.get_ref().unwrap().is::<TestError>());
+ assert_eq!("asdf", err.get_ref().unwrap().description());
+ assert!(err.get_mut().unwrap().is::<TestError>());
+ let extracted = err.into_inner().unwrap();
+ extracted.downcast::<TestError>().unwrap();
+ }
+}
diff --git a/ctr-std/src/io/impls.rs b/ctr-std/src/io/impls.rs
new file mode 100644
index 0000000..6b26c01
--- /dev/null
+++ b/ctr-std/src/io/impls.rs
@@ -0,0 +1,296 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use cmp;
+use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind};
+use fmt;
+use mem;
+
+// =============================================================================
+// Forwarding implementations
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, R: Read + ?Sized> Read for &'a mut R {
+ #[inline]
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ (**self).read(buf)
+ }
+
+ #[inline]
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ (**self).read_to_end(buf)
+ }
+
+ #[inline]
+ fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+ (**self).read_to_string(buf)
+ }
+
+ #[inline]
+ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+ (**self).read_exact(buf)
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, W: Write + ?Sized> Write for &'a mut W {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> { (**self).flush() }
+
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ (**self).write_all(buf)
+ }
+
+ #[inline]
+ fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
+ (**self).write_fmt(fmt)
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, S: Seek + ?Sized> Seek for &'a mut S {
+ #[inline]
+ fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B {
+ #[inline]
+ fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() }
+
+ #[inline]
+ fn consume(&mut self, amt: usize) { (**self).consume(amt) }
+
+ #[inline]
+ fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
+ (**self).read_until(byte, buf)
+ }
+
+ #[inline]
+ fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
+ (**self).read_line(buf)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Read + ?Sized> Read for Box<R> {
+ #[inline]
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ (**self).read(buf)
+ }
+
+ #[inline]
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ (**self).read_to_end(buf)
+ }
+
+ #[inline]
+ fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+ (**self).read_to_string(buf)
+ }
+
+ #[inline]
+ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+ (**self).read_exact(buf)
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<W: Write + ?Sized> Write for Box<W> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> { (**self).flush() }
+
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ (**self).write_all(buf)
+ }
+
+ #[inline]
+ fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
+ (**self).write_fmt(fmt)
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<S: Seek + ?Sized> Seek for Box<S> {
+ #[inline]
+ fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<B: BufRead + ?Sized> BufRead for Box<B> {
+ #[inline]
+ fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() }
+
+ #[inline]
+ fn consume(&mut self, amt: usize) { (**self).consume(amt) }
+
+ #[inline]
+ fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
+ (**self).read_until(byte, buf)
+ }
+
+ #[inline]
+ fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
+ (**self).read_line(buf)
+ }
+}
+
+// =============================================================================
+// In-memory buffer implementations
+
+/// Read is implemented for `&[u8]` by copying from the slice.
+///
+/// Note that reading updates the slice to point to the yet unread part.
+/// The slice will be empty when EOF is reached.
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> Read for &'a [u8] {
+ #[inline]
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ let amt = cmp::min(buf.len(), self.len());
+ let (a, b) = self.split_at(amt);
+ buf[..amt].copy_from_slice(a);
+ *self = b;
+ Ok(amt)
+ }
+
+ #[inline]
+ fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+ if buf.len() > self.len() {
+ return Err(Error::new(ErrorKind::UnexpectedEof,
+ "failed to fill whole buffer"));
+ }
+ let (a, b) = self.split_at(buf.len());
+ buf.copy_from_slice(a);
+ *self = b;
+ Ok(())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> BufRead for &'a [u8] {
+ #[inline]
+ fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) }
+
+ #[inline]
+ fn consume(&mut self, amt: usize) { *self = &self[amt..]; }
+}
+
+/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
+/// its data.
+///
+/// Note that writing updates the slice to point to the yet unwritten part.
+/// The slice will be empty when it has been completely overwritten.
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> Write for &'a mut [u8] {
+ #[inline]
+ fn write(&mut self, data: &[u8]) -> io::Result<usize> {
+ let amt = cmp::min(data.len(), self.len());
+ let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
+ a.copy_from_slice(&data[..amt]);
+ *self = b;
+ Ok(amt)
+ }
+
+ #[inline]
+ fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
+ if self.write(data)? == data.len() {
+ Ok(())
+ } else {
+ Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"))
+ }
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> { Ok(()) }
+}
+
+/// Write is implemented for `Vec<u8>` by appending to the vector.
+/// The vector will grow as needed.
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Write for Vec<u8> {
+ #[inline]
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ self.extend_from_slice(buf);
+ Ok(buf.len())
+ }
+
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+ self.extend_from_slice(buf);
+ Ok(())
+ }
+
+ #[inline]
+ fn flush(&mut self) -> io::Result<()> { Ok(()) }
+}
+
+#[cfg(test)]
+mod tests {
+ use io::prelude::*;
+ use test;
+
+ #[bench]
+ fn bench_read_slice(b: &mut test::Bencher) {
+ let buf = [5; 1024];
+ let mut dst = [0; 128];
+
+ b.iter(|| {
+ let mut rd = &buf[..];
+ for _ in 0..8 {
+ let _ = rd.read(&mut dst);
+ test::black_box(&dst);
+ }
+ })
+ }
+
+ #[bench]
+ fn bench_write_slice(b: &mut test::Bencher) {
+ let mut buf = [0; 1024];
+ let src = [5; 128];
+
+ b.iter(|| {
+ let mut wr = &mut buf[..];
+ for _ in 0..8 {
+ let _ = wr.write_all(&src);
+ test::black_box(&wr);
+ }
+ })
+ }
+
+ #[bench]
+ fn bench_read_vec(b: &mut test::Bencher) {
+ let buf = vec![5; 1024];
+ let mut dst = [0; 128];
+
+ b.iter(|| {
+ let mut rd = &buf[..];
+ for _ in 0..8 {
+ let _ = rd.read(&mut dst);
+ test::black_box(&dst);
+ }
+ })
+ }
+
+ #[bench]
+ fn bench_write_vec(b: &mut test::Bencher) {
+ let mut buf = Vec::with_capacity(1024);
+ let src = [5; 128];
+
+ b.iter(|| {
+ let mut wr = &mut buf[..];
+ for _ in 0..8 {
+ let _ = wr.write_all(&src);
+ test::black_box(&wr);
+ }
+ })
+ }
+}
diff --git a/ctr-std/src/io/mod.rs b/ctr-std/src/io/mod.rs
new file mode 100644
index 0000000..1653790
--- /dev/null
+++ b/ctr-std/src/io/mod.rs
@@ -0,0 +1,1990 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Traits, helpers, and type definitions for core I/O functionality.
+//!
+//! The `std::io` module contains a number of common things you'll need
+//! when doing input and output. The most core part of this module is
+//! the [`Read`] and [`Write`] traits, which provide the
+//! most general interface for reading and writing input and output.
+//!
+//! # Read and Write
+//!
+//! Because they are traits, [`Read`] and [`Write`] are implemented by a number
+//! of other types, and you can implement them for your types too. As such,
+//! you'll see a few different types of I/O throughout the documentation in
+//! this module: [`File`]s, [`TcpStream`]s, and sometimes even [`Vec<T>`]s. For
+//! example, `Read` adds a `read()` method, which we can use on `File`s:
+//!
+//! ```
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::fs::File;
+//!
+//! # fn foo() -> io::Result<()> {
+//! let mut f = try!(File::open("foo.txt"));
+//! let mut buffer = [0; 10];
+//!
+//! // read up to 10 bytes
+//! try!(f.read(&mut buffer));
+//!
+//! println!("The bytes: {:?}", buffer);
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! [`Read`] and [`Write`] are so important, implementors of the two traits have a
+//! nickname: readers and writers. So you'll sometimes see 'a reader' instead
+//! of 'a type that implements the [`Read`] trait'. Much easier!
+//!
+//! ## Seek and BufRead
+//!
+//! Beyond that, there are two important traits that are provided: [`Seek`]
+//! and [`BufRead`]. Both of these build on top of a reader to control
+//! how the reading happens. [`Seek`] lets you control where the next byte is
+//! coming from:
+//!
+//! ```
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::SeekFrom;
+//! use std::fs::File;
+//!
+//! # fn foo() -> io::Result<()> {
+//! let mut f = try!(File::open("foo.txt"));
+//! let mut buffer = [0; 10];
+//!
+//! // skip to the last 10 bytes of the file
+//! try!(f.seek(SeekFrom::End(-10)));
+//!
+//! // read up to 10 bytes
+//! try!(f.read(&mut buffer));
+//!
+//! println!("The bytes: {:?}", buffer);
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! [`BufRead`] uses an internal buffer to provide a number of other ways to read, but
+//! to show it off, we'll need to talk about buffers in general. Keep reading!
+//!
+//! ## BufReader and BufWriter
+//!
+//! Byte-based interfaces are unwieldy and can be inefficient, as we'd need to be
+//! making near-constant calls to the operating system. To help with this,
+//! `std::io` comes with two structs, [`BufReader`] and [`BufWriter`], which wrap
+//! readers and writers. The wrapper uses a buffer, reducing the number of
+//! calls and providing nicer methods for accessing exactly what you want.
+//!
+//! For example, [`BufReader`] works with the [`BufRead`] trait to add extra
+//! methods to any reader:
+//!
+//! ```
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::BufReader;
+//! use std::fs::File;
+//!
+//! # fn foo() -> io::Result<()> {
+//! let f = try!(File::open("foo.txt"));
+//! let mut reader = BufReader::new(f);
+//! let mut buffer = String::new();
+//!
+//! // read a line into buffer
+//! try!(reader.read_line(&mut buffer));
+//!
+//! println!("{}", buffer);
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! [`BufWriter`] doesn't add any new ways of writing; it just buffers every call
+//! to [`write()`]:
+//!
+//! ```
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::BufWriter;
+//! use std::fs::File;
+//!
+//! # fn foo() -> io::Result<()> {
+//! let f = try!(File::create("foo.txt"));
+//! {
+//! let mut writer = BufWriter::new(f);
+//!
+//! // write a byte to the buffer
+//! try!(writer.write(&[42]));
+//!
+//! } // the buffer is flushed once writer goes out of scope
+//!
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! ## Standard input and output
+//!
+//! A very common source of input is standard input:
+//!
+//! ```
+//! use std::io;
+//!
+//! # fn foo() -> io::Result<()> {
+//! let mut input = String::new();
+//!
+//! try!(io::stdin().read_line(&mut input));
+//!
+//! println!("You typed: {}", input.trim());
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! And a very common source of output is standard output:
+//!
+//! ```
+//! use std::io;
+//! use std::io::prelude::*;
+//!
+//! # fn foo() -> io::Result<()> {
+//! try!(io::stdout().write(&[42]));
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! Of course, using [`io::stdout()`] directly is less common than something like
+//! [`println!`].
+//!
+//! ## Iterator types
+//!
+//! A large number of the structures provided by `std::io` are for various
+//! ways of iterating over I/O. For example, [`Lines`] is used to split over
+//! lines:
+//!
+//! ```
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::BufReader;
+//! use std::fs::File;
+//!
+//! # fn foo() -> io::Result<()> {
+//! let f = try!(File::open("foo.txt"));
+//! let reader = BufReader::new(f);
+//!
+//! for line in reader.lines() {
+//! println!("{}", try!(line));
+//! }
+//!
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! ## Functions
+//!
+//! There are a number of [functions][functions-list] that offer access to various
+//! features. For example, we can use three of these functions to copy everything
+//! from standard input to standard output:
+//!
+//! ```
+//! use std::io;
+//!
+//! # fn foo() -> io::Result<()> {
+//! try!(io::copy(&mut io::stdin(), &mut io::stdout()));
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! [functions-list]: #functions-1
+//!
+//! ## io::Result
+//!
+//! Last, but certainly not least, is [`io::Result`]. This type is used
+//! as the return type of many `std::io` functions that can cause an error, and
+//! can be returned from your own functions as well. Many of the examples in this
+//! module use the [`try!`] macro:
+//!
+//! ```
+//! use std::io;
+//!
+//! fn read_input() -> io::Result<()> {
+//! let mut input = String::new();
+//!
+//! try!(io::stdin().read_line(&mut input));
+//!
+//! println!("You typed: {}", input.trim());
+//!
+//! Ok(())
+//! }
+//! ```
+//!
+//! The return type of `read_input()`, [`io::Result<()>`][`io::Result`], is a very
+//! common type for functions which don't have a 'real' return value, but do want to
+//! return errors if they happen. In this case, the only purpose of this function is
+//! to read the line and print it, so we use `()`.
+//!
+//! ## Platform-specific behavior
+//!
+//! Many I/O functions throughout the standard library are documented to indicate
+//! what various library or syscalls they are delegated to. This is done to help
+//! applications both understand what's happening under the hood as well as investigate
+//! any possibly unclear semantics. Note, however, that this is informative, not a binding
+//! contract. The implementation of many of these functions are subject to change over
+//! time and may call fewer or more syscalls/library functions.
+//!
+//! [`Read`]: trait.Read.html
+//! [`Write`]: trait.Write.html
+//! [`Seek`]: trait.Seek.html
+//! [`BufRead`]: trait.BufRead.html
+//! [`File`]: ../fs/struct.File.html
+//! [`TcpStream`]: ../net/struct.TcpStream.html
+//! [`Vec<T>`]: ../vec/struct.Vec.html
+//! [`BufReader`]: struct.BufReader.html
+//! [`BufWriter`]: struct.BufWriter.html
+//! [`write()`]: trait.Write.html#tymethod.write
+//! [`io::stdout()`]: fn.stdout.html
+//! [`println!`]: ../macro.println.html
+//! [`Lines`]: struct.Lines.html
+//! [`io::Result`]: type.Result.html
+//! [`try!`]: ../macro.try.html
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+use cmp;
+use std_unicode::str as core_str;
+use error as std_error;
+use fmt;
+use result;
+use str;
+use memchr;
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use self::buffered::{BufReader, BufWriter, LineWriter};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use self::buffered::IntoInnerError;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use self::cursor::Cursor;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use self::error::{Result, Error, ErrorKind};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use self::print::{STDOUT, _print};
+
+
+pub mod prelude;
+mod buffered;
+mod cursor;
+mod error;
+mod impls;
+mod util;
+mod print;
+
+const DEFAULT_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
+
+// A few methods below (read_to_string, read_line) will append data into a
+// `String` buffer, but we need to be pretty careful when doing this. The
+// implementation will just call `.as_mut_vec()` and then delegate to a
+// byte-oriented reading method, but we must ensure that when returning we never
+// leave `buf` in a state such that it contains invalid UTF-8 in its bounds.
+//
+// To this end, we use an RAII guard (to protect against panics) which updates
+// the length of the string when it is dropped. This guard initially truncates
+// the string to the prior length and only after we've validated that the
+// new contents are valid UTF-8 do we allow it to set a longer length.
+//
+// The unsafety in this function is twofold:
+//
+// 1. We're looking at the raw bytes of `buf`, so we take on the burden of UTF-8
+// checks.
+// 2. We're passing a raw buffer to the function `f`, and it is expected that
+// the function only *appends* bytes to the buffer. We'll get undefined
+// behavior if existing bytes are overwritten to have non-UTF-8 data.
+fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize>
+ where F: FnOnce(&mut Vec<u8>) -> Result<usize>
+{
+ struct Guard<'a> { s: &'a mut Vec<u8>, len: usize }
+ impl<'a> Drop for Guard<'a> {
+ fn drop(&mut self) {
+ unsafe { self.s.set_len(self.len); }
+ }
+ }
+
+ unsafe {
+ let mut g = Guard { len: buf.len(), s: buf.as_mut_vec() };
+ let ret = f(g.s);
+ if str::from_utf8(&g.s[g.len..]).is_err() {
+ ret.and_then(|_| {
+ Err(Error::new(ErrorKind::InvalidData,
+ "stream did not contain valid UTF-8"))
+ })
+ } else {
+ g.len = g.s.len();
+ ret
+ }
+ }
+}
+
+// This uses an adaptive system to extend the vector when it fills. We want to
+// avoid paying to allocate and zero a huge chunk of memory if the reader only
+// has 4 bytes while still making large reads if the reader does have a ton
+// of data to return. Simply tacking on an extra DEFAULT_BUF_SIZE space every
+// time is 4,500 times (!) slower than this if the reader has a very small
+// amount of data to return.
+fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize> {
+ let start_len = buf.len();
+ let mut len = start_len;
+ let mut new_write_size = 16;
+ let ret;
+ loop {
+ if len == buf.len() {
+ if new_write_size < DEFAULT_BUF_SIZE {
+ new_write_size *= 2;
+ }
+ buf.resize(len + new_write_size, 0);
+ }
+
+ match r.read(&mut buf[len..]) {
+ Ok(0) => {
+ ret = Ok(len - start_len);
+ break;
+ }
+ Ok(n) => len += n,
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
+ Err(e) => {
+ ret = Err(e);
+ break;
+ }
+ }
+ }
+
+ buf.truncate(len);
+ ret
+}
+
+/// The `Read` trait allows for reading bytes from a source.
+///
+/// Implementors of the `Read` trait are sometimes called 'readers'.
+///
+/// Readers are defined by one required method, `read()`. Each call to `read`
+/// will attempt to pull bytes from this source into a provided buffer. A
+/// number of other methods are implemented in terms of `read()`, giving
+/// implementors a number of ways to read bytes while only needing to implement
+/// a single method.
+///
+/// Readers are intended to be composable with one another. Many implementors
+/// throughout `std::io` take and provide types which implement the `Read`
+/// trait.
+///
+/// Please note that each call to `read` may involve a system call, and
+/// therefore, using something that implements [`BufRead`][bufread], such as
+/// [`BufReader`][bufreader], will be more efficient.
+///
+/// [bufread]: trait.BufRead.html
+/// [bufreader]: struct.BufReader.html
+///
+/// # Examples
+///
+/// [`File`][file]s implement `Read`:
+///
+/// [file]: ../fs/struct.File.html
+///
+/// ```
+/// use std::io;
+/// use std::io::prelude::*;
+/// use std::fs::File;
+///
+/// # fn foo() -> io::Result<()> {
+/// let mut f = try!(File::open("foo.txt"));
+/// let mut buffer = [0; 10];
+///
+/// // read up to 10 bytes
+/// try!(f.read(&mut buffer));
+///
+/// let mut buffer = vec![0; 10];
+/// // read the whole file
+/// try!(f.read_to_end(&mut buffer));
+///
+/// // read into a String, so that you don't need to do the conversion.
+/// let mut buffer = String::new();
+/// try!(f.read_to_string(&mut buffer));
+///
+/// // and more! See the other methods for more details.
+/// # Ok(())
+/// # }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Read {
+ /// Pull some bytes from this source into the specified buffer, returning
+ /// how many bytes were read.
+ ///
+ /// This function does not provide any guarantees about whether it blocks
+ /// waiting for data, but if an object needs to block for a read but cannot
+ /// it will typically signal this via an `Err` return value.
+ ///
+ /// If the return value of this method is `Ok(n)`, then it must be
+ /// guaranteed that `0 <= n <= buf.len()`. A nonzero `n` value indicates
+ /// that the buffer `buf` has been filled in with `n` bytes of data from this
+ /// source. If `n` is `0`, then it can indicate one of two scenarios:
+ ///
+ /// 1. This reader has reached its "end of file" and will likely no longer
+ /// be able to produce bytes. Note that this does not mean that the
+ /// reader will *always* no longer be able to produce bytes.
+ /// 2. The buffer specified was 0 bytes in length.
+ ///
+ /// No guarantees are provided about the contents of `buf` when this
+ /// function is called, implementations cannot rely on any property of the
+ /// contents of `buf` being true. It is recommended that implementations
+ /// only write data to `buf` instead of reading its contents.
+ ///
+ /// # Errors
+ ///
+ /// If this function encounters any form of I/O or other error, an error
+ /// variant will be returned. If an error is returned then it must be
+ /// guaranteed that no bytes were read.
+ ///
+ /// # Examples
+ ///
+ /// [`File`][file]s implement `Read`:
+ ///
+ /// [file]: ../fs/struct.File.html
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let mut f = try!(File::open("foo.txt"));
+ /// let mut buffer = [0; 10];
+ ///
+ /// // read 10 bytes
+ /// try!(f.read(&mut buffer[..]));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
+
+ /// Read all bytes until EOF in this source, placing them into `buf`.
+ ///
+ /// All bytes read from this source will be appended to the specified buffer
+ /// `buf`. This function will continuously call `read` to append more data to
+ /// `buf` until `read` returns either `Ok(0)` or an error of
+ /// non-`ErrorKind::Interrupted` kind.
+ ///
+ /// If successful, this function will return the total number of bytes read.
+ ///
+ /// # Errors
+ ///
+ /// If this function encounters an error of the kind
+ /// `ErrorKind::Interrupted` then the error is ignored and the operation
+ /// will continue.
+ ///
+ /// If any other read error is encountered then this function immediately
+ /// returns. Any bytes which have already been read will be appended to
+ /// `buf`.
+ ///
+ /// # Examples
+ ///
+ /// [`File`][file]s implement `Read`:
+ ///
+ /// [file]: ../fs/struct.File.html
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let mut f = try!(File::open("foo.txt"));
+ /// let mut buffer = Vec::new();
+ ///
+ /// // read the whole file
+ /// try!(f.read_to_end(&mut buffer));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
+ read_to_end(self, buf)
+ }
+
+ /// Read all bytes until EOF in this source, placing them into `buf`.
+ ///
+ /// If successful, this function returns the number of bytes which were read
+ /// and appended to `buf`.
+ ///
+ /// # Errors
+ ///
+ /// If the data in this stream is *not* valid UTF-8 then an error is
+ /// returned and `buf` is unchanged.
+ ///
+ /// See [`read_to_end()`][readtoend] for other error semantics.
+ ///
+ /// [readtoend]: #method.read_to_end
+ ///
+ /// # Examples
+ ///
+ /// [`File`][file]s implement `Read`:
+ ///
+ /// [file]: ../fs/struct.File.html
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let mut f = try!(File::open("foo.txt"));
+ /// let mut buffer = String::new();
+ ///
+ /// try!(f.read_to_string(&mut buffer));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
+ // Note that we do *not* call `.read_to_end()` here. We are passing
+ // `&mut Vec<u8>` (the raw contents of `buf`) into the `read_to_end`
+ // method to fill it up. An arbitrary implementation could overwrite the
+ // entire contents of the vector, not just append to it (which is what
+ // we are expecting).
+ //
+ // To prevent extraneously checking the UTF-8-ness of the entire buffer
+ // we pass it to our hardcoded `read_to_end` implementation which we
+ // know is guaranteed to only read data into the end of the buffer.
+ append_to_string(buf, |b| read_to_end(self, b))
+ }
+
+ /// Read the exact number of bytes required to fill `buf`.
+ ///
+ /// This function reads as many bytes as necessary to completely fill the
+ /// specified buffer `buf`.
+ ///
+ /// No guarantees are provided about the contents of `buf` when this
+ /// function is called, implementations cannot rely on any property of the
+ /// contents of `buf` being true. It is recommended that implementations
+ /// only write data to `buf` instead of reading its contents.
+ ///
+ /// # Errors
+ ///
+ /// If this function encounters an error of the kind
+ /// `ErrorKind::Interrupted` then the error is ignored and the operation
+ /// will continue.
+ ///
+ /// If this function encounters an "end of file" before completely filling
+ /// the buffer, it returns an error of the kind `ErrorKind::UnexpectedEof`.
+ /// The contents of `buf` are unspecified in this case.
+ ///
+ /// If any other read error is encountered then this function immediately
+ /// returns. The contents of `buf` are unspecified in this case.
+ ///
+ /// If this function returns an error, it is unspecified how many bytes it
+ /// has read, but it will never read more than would be necessary to
+ /// completely fill the buffer.
+ ///
+ /// # Examples
+ ///
+ /// [`File`][file]s implement `Read`:
+ ///
+ /// [file]: ../fs/struct.File.html
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let mut f = try!(File::open("foo.txt"));
+ /// let mut buffer = [0; 10];
+ ///
+ /// // read exactly 10 bytes
+ /// try!(f.read_exact(&mut buffer));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "read_exact", since = "1.6.0")]
+ fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
+ while !buf.is_empty() {
+ match self.read(buf) {
+ Ok(0) => break,
+ Ok(n) => { let tmp = buf; buf = &mut tmp[n..]; }
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
+ Err(e) => return Err(e),
+ }
+ }
+ if !buf.is_empty() {
+ Err(Error::new(ErrorKind::UnexpectedEof,
+ "failed to fill whole buffer"))
+ } else {
+ Ok(())
+ }
+ }
+
+ /// Creates a "by reference" adaptor for this instance of `Read`.
+ ///
+ /// The returned adaptor also implements `Read` and will simply borrow this
+ /// current reader.
+ ///
+ /// # Examples
+ ///
+ /// [`File`][file]s implement `Read`:
+ ///
+ /// [file]: ../fs/struct.File.html
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::Read;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let mut f = try!(File::open("foo.txt"));
+ /// let mut buffer = Vec::new();
+ /// let mut other_buffer = Vec::new();
+ ///
+ /// {
+ /// let reference = f.by_ref();
+ ///
+ /// // read at most 5 bytes
+ /// try!(reference.take(5).read_to_end(&mut buffer));
+ ///
+ /// } // drop our &mut reference so we can use f again
+ ///
+ /// // original file still usable, read the rest
+ /// try!(f.read_to_end(&mut other_buffer));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
+
+ /// Transforms this `Read` instance to an `Iterator` over its bytes.
+ ///
+ /// The returned type implements `Iterator` where the `Item` is `Result<u8,
+ /// R::Err>`. The yielded item is `Ok` if a byte was successfully read and
+ /// `Err` otherwise for I/O errors. EOF is mapped to returning `None` from
+ /// this iterator.
+ ///
+ /// # Examples
+ ///
+ /// [`File`][file]s implement `Read`:
+ ///
+ /// [file]: ../fs/struct.File.html
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let mut f = try!(File::open("foo.txt"));
+ ///
+ /// for byte in f.bytes() {
+ /// println!("{}", byte.unwrap());
+ /// }
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn bytes(self) -> Bytes<Self> where Self: Sized {
+ Bytes { inner: self }
+ }
+
+ /// Transforms this `Read` instance to an `Iterator` over `char`s.
+ ///
+ /// This adaptor will attempt to interpret this reader as a UTF-8 encoded
+ /// sequence of characters. The returned iterator will return `None` once
+ /// EOF is reached for this reader. Otherwise each element yielded will be a
+ /// `Result<char, E>` where `E` may contain information about what I/O error
+ /// occurred or where decoding failed.
+ ///
+ /// Currently this adaptor will discard intermediate data read, and should
+ /// be avoided if this is not desired.
+ ///
+ /// # Examples
+ ///
+ /// [`File`][file]s implement `Read`:
+ ///
+ /// [file]: ../fs/struct.File.html
+ ///
+ /// ```
+ /// #![feature(io)]
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let mut f = try!(File::open("foo.txt"));
+ ///
+ /// for c in f.chars() {
+ /// println!("{}", c.unwrap());
+ /// }
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[unstable(feature = "io", reason = "the semantics of a partial read/write \
+ of where errors happen is currently \
+ unclear and may change",
+ issue = "27802")]
+ fn chars(self) -> Chars<Self> where Self: Sized {
+ Chars { inner: self }
+ }
+
+ /// Creates an adaptor which will chain this stream with another.
+ ///
+ /// The returned `Read` instance will first read all bytes from this object
+ /// until EOF is encountered. Afterwards the output is equivalent to the
+ /// output of `next`.
+ ///
+ /// # Examples
+ ///
+ /// [`File`][file]s implement `Read`:
+ ///
+ /// [file]: ../fs/struct.File.html
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let mut f1 = try!(File::open("foo.txt"));
+ /// let mut f2 = try!(File::open("bar.txt"));
+ ///
+ /// let mut handle = f1.chain(f2);
+ /// let mut buffer = String::new();
+ ///
+ /// // read the value into a String. We could use any Read method here,
+ /// // this is just one example.
+ /// try!(handle.read_to_string(&mut buffer));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn chain<R: Read>(self, next: R) -> Chain<Self, R> where Self: Sized {
+ Chain { first: self, second: next, done_first: false }
+ }
+
+ /// Creates an adaptor which will read at most `limit` bytes from it.
+ ///
+ /// This function returns a new instance of `Read` which will read at most
+ /// `limit` bytes, after which it will always return EOF (`Ok(0)`). Any
+ /// read errors will not count towards the number of bytes read and future
+ /// calls to `read` may succeed.
+ ///
+ /// # Examples
+ ///
+ /// [`File`][file]s implement `Read`:
+ ///
+ /// [file]: ../fs/struct.File.html
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let mut f = try!(File::open("foo.txt"));
+ /// let mut buffer = [0; 5];
+ ///
+ /// // read at most five bytes
+ /// let mut handle = f.take(5);
+ ///
+ /// try!(handle.read(&mut buffer));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn take(self, limit: u64) -> Take<Self> where Self: Sized {
+ Take { inner: self, limit: limit }
+ }
+}
+
+/// A trait for objects which are byte-oriented sinks.
+///
+/// Implementors of the `Write` trait are sometimes called 'writers'.
+///
+/// Writers are defined by two required methods, `write()` and `flush()`:
+///
+/// * The `write()` method will attempt to write some data into the object,
+/// returning how many bytes were successfully written.
+///
+/// * The `flush()` method is useful for adaptors and explicit buffers
+/// themselves for ensuring that all buffered data has been pushed out to the
+/// 'true sink'.
+///
+/// Writers are intended to be composable with one another. Many implementors
+/// throughout `std::io` take and provide types which implement the `Write`
+/// trait.
+///
+/// # Examples
+///
+/// ```
+/// use std::io::prelude::*;
+/// use std::fs::File;
+///
+/// # fn foo() -> std::io::Result<()> {
+/// let mut buffer = try!(File::create("foo.txt"));
+///
+/// try!(buffer.write(b"some bytes"));
+/// # Ok(())
+/// # }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Write {
+ /// Write a buffer into this object, returning how many bytes were written.
+ ///
+ /// This function will attempt to write the entire contents of `buf`, but
+ /// the entire write may not succeed, or the write may also generate an
+ /// error. A call to `write` represents *at most one* attempt to write to
+ /// any wrapped object.
+ ///
+ /// Calls to `write` are not guaranteed to block waiting for data to be
+ /// written, and a write which would otherwise block can be indicated through
+ /// an `Err` variant.
+ ///
+ /// If the return value is `Ok(n)` then it must be guaranteed that
+ /// `0 <= n <= buf.len()`. A return value of `0` typically means that the
+ /// underlying object is no longer able to accept bytes and will likely not
+ /// be able to in the future as well, or that the buffer provided is empty.
+ ///
+ /// # Errors
+ ///
+ /// Each call to `write` may generate an I/O error indicating that the
+ /// operation could not be completed. If an error is returned then no bytes
+ /// in the buffer were written to this writer.
+ ///
+ /// It is **not** considered an error if the entire buffer could not be
+ /// written to this writer.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let mut buffer = try!(File::create("foo.txt"));
+ ///
+ /// try!(buffer.write(b"some bytes"));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn write(&mut self, buf: &[u8]) -> Result<usize>;
+
+ /// Flush this output stream, ensuring that all intermediately buffered
+ /// contents reach their destination.
+ ///
+ /// # Errors
+ ///
+ /// It is considered an error if not all bytes could be written due to
+ /// I/O errors or EOF being reached.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::prelude::*;
+ /// use std::io::BufWriter;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let mut buffer = BufWriter::new(try!(File::create("foo.txt")));
+ ///
+ /// try!(buffer.write(b"some bytes"));
+ /// try!(buffer.flush());
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn flush(&mut self) -> Result<()>;
+
+ /// Attempts to write an entire buffer into this write.
+ ///
+ /// This method will continuously call `write` while there is more data to
+ /// write. This method will not return until the entire buffer has been
+ /// successfully written or an error occurs. The first error generated from
+ /// this method will be returned.
+ ///
+ /// # Errors
+ ///
+ /// This function will return the first error that `write` returns.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let mut buffer = try!(File::create("foo.txt"));
+ ///
+ /// try!(buffer.write_all(b"some bytes"));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
+ while !buf.is_empty() {
+ match self.write(buf) {
+ Ok(0) => return Err(Error::new(ErrorKind::WriteZero,
+ "failed to write whole buffer")),
+ Ok(n) => buf = &buf[n..],
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
+ Err(e) => return Err(e),
+ }
+ }
+ Ok(())
+ }
+
+ /// Writes a formatted string into this writer, returning any error
+ /// encountered.
+ ///
+ /// This method is primarily used to interface with the
+ /// [`format_args!`][formatargs] macro, but it is rare that this should
+ /// explicitly be called. The [`write!`][write] macro should be favored to
+ /// invoke this method instead.
+ ///
+ /// [formatargs]: ../macro.format_args.html
+ /// [write]: ../macro.write.html
+ ///
+ /// This function internally uses the [`write_all`][writeall] method on
+ /// this trait and hence will continuously write data so long as no errors
+ /// are received. This also means that partial writes are not indicated in
+ /// this signature.
+ ///
+ /// [writeall]: #method.write_all
+ ///
+ /// # Errors
+ ///
+ /// This function will return any I/O error reported while formatting.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let mut buffer = try!(File::create("foo.txt"));
+ ///
+ /// // this call
+ /// try!(write!(buffer, "{:.*}", 2, 1.234567));
+ /// // turns into this:
+ /// try!(buffer.write_fmt(format_args!("{:.*}", 2, 1.234567)));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<()> {
+ // Create a shim which translates a Write to a fmt::Write and saves
+ // off I/O errors. instead of discarding them
+ struct Adaptor<'a, T: ?Sized + 'a> {
+ inner: &'a mut T,
+ error: Result<()>,
+ }
+
+ impl<'a, T: Write + ?Sized> fmt::Write for Adaptor<'a, T> {
+ fn write_str(&mut self, s: &str) -> fmt::Result {
+ match self.inner.write_all(s.as_bytes()) {
+ Ok(()) => Ok(()),
+ Err(e) => {
+ self.error = Err(e);
+ Err(fmt::Error)
+ }
+ }
+ }
+ }
+
+ let mut output = Adaptor { inner: self, error: Ok(()) };
+ match fmt::write(&mut output, fmt) {
+ Ok(()) => Ok(()),
+ Err(..) => {
+ // check if the error came from the underlying `Write` or not
+ if output.error.is_err() {
+ output.error
+ } else {
+ Err(Error::new(ErrorKind::Other, "formatter error"))
+ }
+ }
+ }
+ }
+
+ /// Creates a "by reference" adaptor for this instance of `Write`.
+ ///
+ /// The returned adaptor also implements `Write` and will simply borrow this
+ /// current writer.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io::Write;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> std::io::Result<()> {
+ /// let mut buffer = try!(File::create("foo.txt"));
+ ///
+ /// let reference = buffer.by_ref();
+ ///
+ /// // we can use reference just like our original buffer
+ /// try!(reference.write_all(b"some bytes"));
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
+}
+
+/// The `Seek` trait provides a cursor which can be moved within a stream of
+/// bytes.
+///
+/// The stream typically has a fixed size, allowing seeking relative to either
+/// end or the current offset.
+///
+/// # Examples
+///
+/// [`File`][file]s implement `Seek`:
+///
+/// [file]: ../fs/struct.File.html
+///
+/// ```
+/// use std::io;
+/// use std::io::prelude::*;
+/// use std::fs::File;
+/// use std::io::SeekFrom;
+///
+/// # fn foo() -> io::Result<()> {
+/// let mut f = try!(File::open("foo.txt"));
+///
+/// // move the cursor 42 bytes from the start of the file
+/// try!(f.seek(SeekFrom::Start(42)));
+/// # Ok(())
+/// # }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Seek {
+ /// Seek to an offset, in bytes, in a stream.
+ ///
+ /// A seek beyond the end of a stream is allowed, but implementation
+ /// defined.
+ ///
+ /// If the seek operation completed successfully,
+ /// this method returns the new position from the start of the stream.
+ /// That position can be used later with [`SeekFrom::Start`].
+ ///
+ /// # Errors
+ ///
+ /// Seeking to a negative offset is considered an error.
+ ///
+ /// [`SeekFrom::Start`]: enum.SeekFrom.html#variant.Start
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
+}
+
+/// Enumeration of possible methods to seek within an I/O object.
+///
+/// It is used by the [`Seek`] trait.
+///
+/// [`Seek`]: trait.Seek.html
+#[derive(Copy, PartialEq, Eq, Clone, Debug)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub enum SeekFrom {
+ /// Set the offset to the provided number of bytes.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Start(#[stable(feature = "rust1", since = "1.0.0")] u64),
+
+ /// Set the offset to the size of this object plus the specified number of
+ /// bytes.
+ ///
+ /// It is possible to seek beyond the end of an object, but it's an error to
+ /// seek before byte 0.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ End(#[stable(feature = "rust1", since = "1.0.0")] i64),
+
+ /// Set the offset to the current position plus the specified number of
+ /// bytes.
+ ///
+ /// It is possible to seek beyond the end of an object, but it's an error to
+ /// seek before byte 0.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Current(#[stable(feature = "rust1", since = "1.0.0")] i64),
+}
+
+fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>)
+ -> Result<usize> {
+ let mut read = 0;
+ loop {
+ let (done, used) = {
+ let available = match r.fill_buf() {
+ Ok(n) => n,
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => return Err(e)
+ };
+ match memchr::memchr(delim, available) {
+ Some(i) => {
+ buf.extend_from_slice(&available[..i + 1]);
+ (true, i + 1)
+ }
+ None => {
+ buf.extend_from_slice(available);
+ (false, available.len())
+ }
+ }
+ };
+ r.consume(used);
+ read += used;
+ if done || used == 0 {
+ return Ok(read);
+ }
+ }
+}
+
+/// A `BufRead` is a type of `Read`er which has an internal buffer, allowing it
+/// to perform extra ways of reading.
+///
+/// For example, reading line-by-line is inefficient without using a buffer, so
+/// if you want to read by line, you'll need `BufRead`, which includes a
+/// [`read_line()`] method as well as a [`lines()`] iterator.
+///
+/// # Examples
+///
+/// A locked standard input implements `BufRead`:
+///
+/// ```
+/// use std::io;
+/// use std::io::prelude::*;
+///
+/// let stdin = io::stdin();
+/// for line in stdin.lock().lines() {
+/// println!("{}", line.unwrap());
+/// }
+/// ```
+///
+/// If you have something that implements [`Read`], you can use the [`BufReader`
+/// type][`BufReader`] to turn it into a `BufRead`.
+///
+/// For example, [`File`] implements [`Read`], but not `BufRead`.
+/// [`BufReader`] to the rescue!
+///
+/// [`BufReader`]: struct.BufReader.html
+/// [`File`]: ../fs/struct.File.html
+/// [`read_line()`]: #method.read_line
+/// [`lines()`]: #method.lines
+/// [`Read`]: trait.Read.html
+///
+/// ```
+/// use std::io::{self, BufReader};
+/// use std::io::prelude::*;
+/// use std::fs::File;
+///
+/// # fn foo() -> io::Result<()> {
+/// let f = try!(File::open("foo.txt"));
+/// let f = BufReader::new(f);
+///
+/// for line in f.lines() {
+/// println!("{}", line.unwrap());
+/// }
+///
+/// # Ok(())
+/// # }
+/// ```
+///
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait BufRead: Read {
+ /// Fills the internal buffer of this object, returning the buffer contents.
+ ///
+ /// This function is a lower-level call. It needs to be paired with the
+ /// [`consume()`] method to function properly. When calling this
+ /// method, none of the contents will be "read" in the sense that later
+ /// calling `read` may return the same contents. As such, [`consume()`] must
+ /// be called with the number of bytes that are consumed from this buffer to
+ /// ensure that the bytes are never returned twice.
+ ///
+ /// [`consume()`]: #tymethod.consume
+ ///
+ /// An empty buffer returned indicates that the stream has reached EOF.
+ ///
+ /// # Errors
+ ///
+ /// This function will return an I/O error if the underlying reader was
+ /// read, but returned an error.
+ ///
+ /// # Examples
+ ///
+ /// A locked standard input implements `BufRead`:
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ ///
+ /// let stdin = io::stdin();
+ /// let mut stdin = stdin.lock();
+ ///
+ /// // we can't have two `&mut` references to `stdin`, so use a block
+ /// // to end the borrow early.
+ /// let length = {
+ /// let buffer = stdin.fill_buf().unwrap();
+ ///
+ /// // work with buffer
+ /// println!("{:?}", buffer);
+ ///
+ /// buffer.len()
+ /// };
+ ///
+ /// // ensure the bytes we worked with aren't returned again later
+ /// stdin.consume(length);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fill_buf(&mut self) -> Result<&[u8]>;
+
+ /// Tells this buffer that `amt` bytes have been consumed from the buffer,
+ /// so they should no longer be returned in calls to `read`.
+ ///
+ /// This function is a lower-level call. It needs to be paired with the
+ /// [`fill_buf()`] method to function properly. This function does
+ /// not perform any I/O, it simply informs this object that some amount of
+ /// its buffer, returned from [`fill_buf()`], has been consumed and should
+ /// no longer be returned. As such, this function may do odd things if
+ /// [`fill_buf()`] isn't called before calling it.
+ ///
+ /// The `amt` must be `<=` the number of bytes in the buffer returned by
+ /// [`fill_buf()`].
+ ///
+ /// # Examples
+ ///
+ /// Since `consume()` is meant to be used with [`fill_buf()`],
+ /// that method's example includes an example of `consume()`.
+ ///
+ /// [`fill_buf()`]: #tymethod.fill_buf
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn consume(&mut self, amt: usize);
+
+ /// Read all bytes into `buf` until the delimiter `byte` or EOF is reached.
+ ///
+ /// This function will read bytes from the underlying stream until the
+ /// delimiter or EOF is found. Once found, all bytes up to, and including,
+ /// the delimiter (if found) will be appended to `buf`.
+ ///
+ /// If successful, this function will return the total number of bytes read.
+ ///
+ /// # Errors
+ ///
+ /// This function will ignore all instances of [`ErrorKind::Interrupted`] and
+ /// will otherwise return any errors returned by [`fill_buf()`].
+ ///
+ /// If an I/O error is encountered then all bytes read so far will be
+ /// present in `buf` and its length will have been adjusted appropriately.
+ ///
+ /// # Examples
+ ///
+ /// A locked standard input implements `BufRead`. In this example, we'll
+ /// read from standard input until we see an `a` byte.
+ ///
+ /// [`fill_buf()`]: #tymethod.fill_buf
+ /// [`ErrorKind::Interrupted`]: enum.ErrorKind.html#variant.Interrupted
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ ///
+ /// fn foo() -> io::Result<()> {
+ /// let stdin = io::stdin();
+ /// let mut stdin = stdin.lock();
+ /// let mut buffer = Vec::new();
+ ///
+ /// try!(stdin.read_until(b'a', &mut buffer));
+ ///
+ /// println!("{:?}", buffer);
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
+ read_until(self, byte, buf)
+ }
+
+ /// Read all bytes until a newline (the 0xA byte) is reached, and append
+ /// them to the provided buffer.
+ ///
+ /// This function will read bytes from the underlying stream until the
+ /// newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes
+ /// up to, and including, the delimiter (if found) will be appended to
+ /// `buf`.
+ ///
+ /// If successful, this function will return the total number of bytes read.
+ ///
+ /// # Errors
+ ///
+ /// This function has the same error semantics as [`read_until()`] and will
+ /// also return an error if the read bytes are not valid UTF-8. If an I/O
+ /// error is encountered then `buf` may contain some bytes already read in
+ /// the event that all data read so far was valid UTF-8.
+ ///
+ /// # Examples
+ ///
+ /// A locked standard input implements `BufRead`. In this example, we'll
+ /// read all of the lines from standard input. If we were to do this in
+ /// an actual project, the [`lines()`] method would be easier, of
+ /// course.
+ ///
+ /// [`lines()`]: #method.lines
+ /// [`read_until()`]: #method.read_until
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ ///
+ /// let stdin = io::stdin();
+ /// let mut stdin = stdin.lock();
+ /// let mut buffer = String::new();
+ ///
+ /// while stdin.read_line(&mut buffer).unwrap() > 0 {
+ /// // work with buffer
+ /// println!("{:?}", buffer);
+ ///
+ /// buffer.clear();
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn read_line(&mut self, buf: &mut String) -> Result<usize> {
+ // Note that we are not calling the `.read_until` method here, but
+ // rather our hardcoded implementation. For more details as to why, see
+ // the comments in `read_to_end`.
+ append_to_string(buf, |b| read_until(self, b'\n', b))
+ }
+
+ /// Returns an iterator over the contents of this reader split on the byte
+ /// `byte`.
+ ///
+ /// The iterator returned from this function will return instances of
+ /// [`io::Result`]`<`[`Vec<u8>`]`>`. Each vector returned will *not* have
+ /// the delimiter byte at the end.
+ ///
+ /// This function will yield errors whenever [`read_until()`] would have
+ /// also yielded an error.
+ ///
+ /// # Examples
+ ///
+ /// A locked standard input implements `BufRead`. In this example, we'll
+ /// read some input from standard input, splitting on commas.
+ ///
+ /// [`io::Result`]: type.Result.html
+ /// [`Vec<u8>`]: ../vec/struct.Vec.html
+ /// [`read_until()`]: #method.read_until
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ ///
+ /// let stdin = io::stdin();
+ ///
+ /// for content in stdin.lock().split(b',') {
+ /// println!("{:?}", content.unwrap());
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn split(self, byte: u8) -> Split<Self> where Self: Sized {
+ Split { buf: self, delim: byte }
+ }
+
+ /// Returns an iterator over the lines of this reader.
+ ///
+ /// The iterator returned from this function will yield instances of
+ /// [`io::Result`]`<`[`String`]`>`. Each string returned will *not* have a newline
+ /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.
+ ///
+ /// [`io::Result`]: type.Result.html
+ /// [`String`]: ../string/struct.String.html
+ ///
+ /// # Examples
+ ///
+ /// A locked standard input implements `BufRead`:
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ ///
+ /// let stdin = io::stdin();
+ ///
+ /// for line in stdin.lock().lines() {
+ /// println!("{}", line.unwrap());
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn lines(self) -> Lines<Self> where Self: Sized {
+ Lines { buf: self }
+ }
+}
+
+/// Adaptor to chain together two readers.
+///
+/// This struct is generally created by calling [`chain()`] on a reader.
+/// Please see the documentation of [`chain()`] for more details.
+///
+/// [`chain()`]: trait.Read.html#method.chain
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Chain<T, U> {
+ first: T,
+ second: U,
+ done_first: bool,
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: Read, U: Read> Read for Chain<T, U> {
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
+ if !self.done_first {
+ match self.first.read(buf)? {
+ 0 if buf.len() != 0 => { self.done_first = true; }
+ n => return Ok(n),
+ }
+ }
+ self.second.read(buf)
+ }
+}
+
+#[stable(feature = "chain_bufread", since = "1.9.0")]
+impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
+ fn fill_buf(&mut self) -> Result<&[u8]> {
+ if !self.done_first {
+ match self.first.fill_buf()? {
+ buf if buf.len() == 0 => { self.done_first = true; }
+ buf => return Ok(buf),
+ }
+ }
+ self.second.fill_buf()
+ }
+
+ fn consume(&mut self, amt: usize) {
+ if !self.done_first {
+ self.first.consume(amt)
+ } else {
+ self.second.consume(amt)
+ }
+ }
+}
+
+/// Reader adaptor which limits the bytes read from an underlying reader.
+///
+/// This struct is generally created by calling [`take()`][take] on a reader.
+/// Please see the documentation of `take()` for more details.
+///
+/// [take]: trait.Read.html#method.take
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Take<T> {
+ inner: T,
+ limit: u64,
+}
+
+impl<T> Take<T> {
+ /// Returns the number of bytes that can be read before this instance will
+ /// return EOF.
+ ///
+ /// # Note
+ ///
+ /// This instance may reach EOF after reading fewer bytes than indicated by
+ /// this method if the underlying `Read` instance reaches EOF.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let f = try!(File::open("foo.txt"));
+ ///
+ /// // read at most five bytes
+ /// let handle = f.take(5);
+ ///
+ /// println!("limit: {}", handle.limit());
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn limit(&self) -> u64 { self.limit }
+
+ /// Consumes the `Take`, returning the wrapped reader.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(io_take_into_inner)]
+ ///
+ /// use std::io;
+ /// use std::io::prelude::*;
+ /// use std::fs::File;
+ ///
+ /// # fn foo() -> io::Result<()> {
+ /// let mut file = try!(File::open("foo.txt"));
+ ///
+ /// let mut buffer = [0; 5];
+ /// let mut handle = file.take(5);
+ /// try!(handle.read(&mut buffer));
+ ///
+ /// let file = handle.into_inner();
+ /// # Ok(())
+ /// # }
+ /// ```
+ #[unstable(feature = "io_take_into_inner", issue = "23755")]
+ pub fn into_inner(self) -> T {
+ self.inner
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: Read> Read for Take<T> {
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
+ // Don't call into inner reader at all at EOF because it may still block
+ if self.limit == 0 {
+ return Ok(0);
+ }
+
+ let max = cmp::min(buf.len() as u64, self.limit) as usize;
+ let n = self.inner.read(&mut buf[..max])?;
+ self.limit -= n as u64;
+ Ok(n)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: BufRead> BufRead for Take<T> {
+ fn fill_buf(&mut self) -> Result<&[u8]> {
+ // Don't call into inner reader at all at EOF because it may still block
+ if self.limit == 0 {
+ return Ok(&[]);
+ }
+
+ let buf = self.inner.fill_buf()?;
+ let cap = cmp::min(buf.len() as u64, self.limit) as usize;
+ Ok(&buf[..cap])
+ }
+
+ fn consume(&mut self, amt: usize) {
+ // Don't let callers reset the limit by passing an overlarge value
+ let amt = cmp::min(amt as u64, self.limit) as usize;
+ self.limit -= amt as u64;
+ self.inner.consume(amt);
+ }
+}
+
+fn read_one_byte(reader: &mut Read) -> Option<Result<u8>> {
+ let mut buf = [0];
+ loop {
+ return match reader.read(&mut buf) {
+ Ok(0) => None,
+ Ok(..) => Some(Ok(buf[0])),
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => Some(Err(e)),
+ };
+ }
+}
+
+/// An iterator over `u8` values of a reader.
+///
+/// This struct is generally created by calling [`bytes()`] on a reader.
+/// Please see the documentation of [`bytes()`] for more details.
+///
+/// [`bytes()`]: trait.Read.html#method.bytes
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Bytes<R> {
+ inner: R,
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<R: Read> Iterator for Bytes<R> {
+ type Item = Result<u8>;
+
+ fn next(&mut self) -> Option<Result<u8>> {
+ read_one_byte(&mut self.inner)
+ }
+}
+
+/// An iterator over the `char`s of a reader.
+///
+/// This struct is generally created by calling [`chars()`][chars] on a reader.
+/// Please see the documentation of `chars()` for more details.
+///
+/// [chars]: trait.Read.html#method.chars
+#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
+ issue = "27802")]
+pub struct Chars<R> {
+ inner: R,
+}
+
+/// An enumeration of possible errors that can be generated from the `Chars`
+/// adapter.
+#[derive(Debug)]
+#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
+ issue = "27802")]
+pub enum CharsError {
+ /// Variant representing that the underlying stream was read successfully
+ /// but it did not contain valid utf8 data.
+ NotUtf8,
+
+ /// Variant representing that an I/O error occurred.
+ Other(Error),
+}
+
+#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
+ issue = "27802")]
+impl<R: Read> Iterator for Chars<R> {
+ type Item = result::Result<char, CharsError>;
+
+ fn next(&mut self) -> Option<result::Result<char, CharsError>> {
+ let first_byte = match read_one_byte(&mut self.inner) {
+ None => return None,
+ Some(Ok(b)) => b,
+ Some(Err(e)) => return Some(Err(CharsError::Other(e))),
+ };
+ let width = core_str::utf8_char_width(first_byte);
+ if width == 1 { return Some(Ok(first_byte as char)) }
+ if width == 0 { return Some(Err(CharsError::NotUtf8)) }
+ let mut buf = [first_byte, 0, 0, 0];
+ {
+ let mut start = 1;
+ while start < width {
+ match self.inner.read(&mut buf[start..width]) {
+ Ok(0) => return Some(Err(CharsError::NotUtf8)),
+ Ok(n) => start += n,
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => return Some(Err(CharsError::Other(e))),
+ }
+ }
+ }
+ Some(match str::from_utf8(&buf[..width]).ok() {
+ Some(s) => Ok(s.chars().next().unwrap()),
+ None => Err(CharsError::NotUtf8),
+ })
+ }
+}
+
+#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
+ issue = "27802")]
+impl std_error::Error for CharsError {
+ fn description(&self) -> &str {
+ match *self {
+ CharsError::NotUtf8 => "invalid utf8 encoding",
+ CharsError::Other(ref e) => std_error::Error::description(e),
+ }
+ }
+ fn cause(&self) -> Option<&std_error::Error> {
+ match *self {
+ CharsError::NotUtf8 => None,
+ CharsError::Other(ref e) => e.cause(),
+ }
+ }
+}
+
+#[unstable(feature = "io", reason = "awaiting stability of Read::chars",
+ issue = "27802")]
+impl fmt::Display for CharsError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match *self {
+ CharsError::NotUtf8 => {
+ "byte stream did not contain valid utf8".fmt(f)
+ }
+ CharsError::Other(ref e) => e.fmt(f),
+ }
+ }
+}
+
+/// An iterator over the contents of an instance of `BufRead` split on a
+/// particular byte.
+///
+/// This struct is generally created by calling [`split()`][split] on a
+/// `BufRead`. Please see the documentation of `split()` for more details.
+///
+/// [split]: trait.BufRead.html#method.split
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Split<B> {
+ buf: B,
+ delim: u8,
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<B: BufRead> Iterator for Split<B> {
+ type Item = Result<Vec<u8>>;
+
+ fn next(&mut self) -> Option<Result<Vec<u8>>> {
+ let mut buf = Vec::new();
+ match self.buf.read_until(self.delim, &mut buf) {
+ Ok(0) => None,
+ Ok(_n) => {
+ if buf[buf.len() - 1] == self.delim {
+ buf.pop();
+ }
+ Some(Ok(buf))
+ }
+ Err(e) => Some(Err(e))
+ }
+ }
+}
+
+/// An iterator over the lines of an instance of `BufRead`.
+///
+/// This struct is generally created by calling [`lines()`][lines] on a
+/// `BufRead`. Please see the documentation of `lines()` for more details.
+///
+/// [lines]: trait.BufRead.html#method.lines
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Lines<B> {
+ buf: B,
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<B: BufRead> Iterator for Lines<B> {
+ type Item = Result<String>;
+
+ fn next(&mut self) -> Option<Result<String>> {
+ let mut buf = String::new();
+ match self.buf.read_line(&mut buf) {
+ Ok(0) => None,
+ Ok(_n) => {
+ if buf.ends_with("\n") {
+ buf.pop();
+ if buf.ends_with("\r") {
+ buf.pop();
+ }
+ }
+ Some(Ok(buf))
+ }
+ Err(e) => Some(Err(e))
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use io::prelude::*;
+ use io;
+ use super::Cursor;
+ use test;
+ use super::repeat;
+
+ #[test]
+ #[cfg_attr(target_os = "emscripten", ignore)]
+ fn read_until() {
+ let mut buf = Cursor::new(&b"12"[..]);
+ let mut v = Vec::new();
+ assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 2);
+ assert_eq!(v, b"12");
+
+ let mut buf = Cursor::new(&b"1233"[..]);
+ let mut v = Vec::new();
+ assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 3);
+ assert_eq!(v, b"123");
+ v.truncate(0);
+ assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 1);
+ assert_eq!(v, b"3");
+ v.truncate(0);
+ assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 0);
+ assert_eq!(v, []);
+ }
+
+ #[test]
+ fn split() {
+ let buf = Cursor::new(&b"12"[..]);
+ let mut s = buf.split(b'3');
+ assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
+ assert!(s.next().is_none());
+
+ let buf = Cursor::new(&b"1233"[..]);
+ let mut s = buf.split(b'3');
+ assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
+ assert_eq!(s.next().unwrap().unwrap(), vec![]);
+ assert!(s.next().is_none());
+ }
+
+ #[test]
+ fn read_line() {
+ let mut buf = Cursor::new(&b"12"[..]);
+ let mut v = String::new();
+ assert_eq!(buf.read_line(&mut v).unwrap(), 2);
+ assert_eq!(v, "12");
+
+ let mut buf = Cursor::new(&b"12\n\n"[..]);
+ let mut v = String::new();
+ assert_eq!(buf.read_line(&mut v).unwrap(), 3);
+ assert_eq!(v, "12\n");
+ v.truncate(0);
+ assert_eq!(buf.read_line(&mut v).unwrap(), 1);
+ assert_eq!(v, "\n");
+ v.truncate(0);
+ assert_eq!(buf.read_line(&mut v).unwrap(), 0);
+ assert_eq!(v, "");
+ }
+
+ #[test]
+ fn lines() {
+ let buf = Cursor::new(&b"12\r"[..]);
+ let mut s = buf.lines();
+ assert_eq!(s.next().unwrap().unwrap(), "12\r".to_string());
+ assert!(s.next().is_none());
+
+ let buf = Cursor::new(&b"12\r\n\n"[..]);
+ let mut s = buf.lines();
+ assert_eq!(s.next().unwrap().unwrap(), "12".to_string());
+ assert_eq!(s.next().unwrap().unwrap(), "".to_string());
+ assert!(s.next().is_none());
+ }
+
+ #[test]
+ fn read_to_end() {
+ let mut c = Cursor::new(&b""[..]);
+ let mut v = Vec::new();
+ assert_eq!(c.read_to_end(&mut v).unwrap(), 0);
+ assert_eq!(v, []);
+
+ let mut c = Cursor::new(&b"1"[..]);
+ let mut v = Vec::new();
+ assert_eq!(c.read_to_end(&mut v).unwrap(), 1);
+ assert_eq!(v, b"1");
+
+ let cap = 1024 * 1024;
+ let data = (0..cap).map(|i| (i / 3) as u8).collect::<Vec<_>>();
+ let mut v = Vec::new();
+ let (a, b) = data.split_at(data.len() / 2);
+ assert_eq!(Cursor::new(a).read_to_end(&mut v).unwrap(), a.len());
+ assert_eq!(Cursor::new(b).read_to_end(&mut v).unwrap(), b.len());
+ assert_eq!(v, data);
+ }
+
+ #[test]
+ fn read_to_string() {
+ let mut c = Cursor::new(&b""[..]);
+ let mut v = String::new();
+ assert_eq!(c.read_to_string(&mut v).unwrap(), 0);
+ assert_eq!(v, "");
+
+ let mut c = Cursor::new(&b"1"[..]);
+ let mut v = String::new();
+ assert_eq!(c.read_to_string(&mut v).unwrap(), 1);
+ assert_eq!(v, "1");
+
+ let mut c = Cursor::new(&b"\xff"[..]);
+ let mut v = String::new();
+ assert!(c.read_to_string(&mut v).is_err());
+ }
+
+ #[test]
+ fn read_exact() {
+ let mut buf = [0; 4];
+
+ let mut c = Cursor::new(&b""[..]);
+ assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
+ io::ErrorKind::UnexpectedEof);
+
+ let mut c = Cursor::new(&b"123"[..]).chain(Cursor::new(&b"456789"[..]));
+ c.read_exact(&mut buf).unwrap();
+ assert_eq!(&buf, b"1234");
+ c.read_exact(&mut buf).unwrap();
+ assert_eq!(&buf, b"5678");
+ assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
+ io::ErrorKind::UnexpectedEof);
+ }
+
+ #[test]
+ fn read_exact_slice() {
+ let mut buf = [0; 4];
+
+ let mut c = &b""[..];
+ assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
+ io::ErrorKind::UnexpectedEof);
+
+ let mut c = &b"123"[..];
+ assert_eq!(c.read_exact(&mut buf).unwrap_err().kind(),
+ io::ErrorKind::UnexpectedEof);
+ // make sure the optimized (early returning) method is being used
+ assert_eq!(&buf, &[0; 4]);
+
+ let mut c = &b"1234"[..];
+ c.read_exact(&mut buf).unwrap();
+ assert_eq!(&buf, b"1234");
+
+ let mut c = &b"56789"[..];
+ c.read_exact(&mut buf).unwrap();
+ assert_eq!(&buf, b"5678");
+ assert_eq!(c, b"9");
+ }
+
+ #[test]
+ fn take_eof() {
+ struct R;
+
+ impl Read for R {
+ fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
+ Err(io::Error::new(io::ErrorKind::Other, ""))
+ }
+ }
+ impl BufRead for R {
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
+ Err(io::Error::new(io::ErrorKind::Other, ""))
+ }
+ fn consume(&mut self, _amt: usize) { }
+ }
+
+ let mut buf = [0; 1];
+ assert_eq!(0, R.take(0).read(&mut buf).unwrap());
+ assert_eq!(b"", R.take(0).fill_buf().unwrap());
+ }
+
+ fn cmp_bufread<Br1: BufRead, Br2: BufRead>(mut br1: Br1, mut br2: Br2, exp: &[u8]) {
+ let mut cat = Vec::new();
+ loop {
+ let consume = {
+ let buf1 = br1.fill_buf().unwrap();
+ let buf2 = br2.fill_buf().unwrap();
+ let minlen = if buf1.len() < buf2.len() { buf1.len() } else { buf2.len() };
+ assert_eq!(buf1[..minlen], buf2[..minlen]);
+ cat.extend_from_slice(&buf1[..minlen]);
+ minlen
+ };
+ if consume == 0 {
+ break;
+ }
+ br1.consume(consume);
+ br2.consume(consume);
+ }
+ assert_eq!(br1.fill_buf().unwrap().len(), 0);
+ assert_eq!(br2.fill_buf().unwrap().len(), 0);
+ assert_eq!(&cat[..], &exp[..])
+ }
+
+ #[test]
+ fn chain_bufread() {
+ let testdata = b"ABCDEFGHIJKL";
+ let chain1 = (&testdata[..3]).chain(&testdata[3..6])
+ .chain(&testdata[6..9])
+ .chain(&testdata[9..]);
+ let chain2 = (&testdata[..4]).chain(&testdata[4..8])
+ .chain(&testdata[8..]);
+ cmp_bufread(chain1, chain2, &testdata[..]);
+ }
+
+ #[test]
+ fn chain_zero_length_read_is_not_eof() {
+ let a = b"A";
+ let b = b"B";
+ let mut s = String::new();
+ let mut chain = (&a[..]).chain(&b[..]);
+ chain.read(&mut []).unwrap();
+ chain.read_to_string(&mut s).unwrap();
+ assert_eq!("AB", s);
+ }
+
+ #[bench]
+ #[cfg_attr(target_os = "emscripten", ignore)]
+ fn bench_read_to_end(b: &mut test::Bencher) {
+ b.iter(|| {
+ let mut lr = repeat(1).take(10000000);
+ let mut vec = Vec::with_capacity(1024);
+ super::read_to_end(&mut lr, &mut vec)
+ });
+ }
+}
diff --git a/ctr-std/src/io/prelude.rs b/ctr-std/src/io/prelude.rs
new file mode 100644
index 0000000..8772d0f
--- /dev/null
+++ b/ctr-std/src/io/prelude.rs
@@ -0,0 +1,24 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! The I/O Prelude
+//!
+//! The purpose of this module is to alleviate imports of many common I/O traits
+//! by adding a glob import to the top of I/O heavy modules:
+//!
+//! ```
+//! # #![allow(unused_imports)]
+//! use std::io::prelude::*;
+//! ```
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use super::{Read, Write, BufRead, Seek};
diff --git a/ctr-std/src/io/print.rs b/ctr-std/src/io/print.rs
new file mode 100644
index 0000000..8a5851b
--- /dev/null
+++ b/ctr-std/src/io/print.rs
@@ -0,0 +1,33 @@
+use fmt;
+use io::{self, Write};
+
+// NOTE: We're just gonna use the spin mutex until we figure out how to properly
+// implement mutexes with ctrulib functions
+use spin::Mutex;
+use libc;
+
+pub static STDOUT: Mutex<StdoutRaw> = Mutex::new(StdoutRaw(()));
+
+pub struct StdoutRaw(());
+
+#[stable(feature = "3ds", since = "1.0.0")]
+impl Write for StdoutRaw {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ unsafe {
+ // devkitPro's version of write(2) fails if zero bytes are written,
+ // so let's just exit if the buffer size is zero
+ if buf.is_empty() {
+ return Ok(buf.len())
+ }
+ libc::write(libc::STDOUT_FILENO, buf.as_ptr() as *const _, buf.len());
+ Ok(buf.len())
+ }
+ }
+
+ fn flush(&mut self) -> io::Result<()> { Ok(()) }
+}
+
+#[doc(hidden)]
+pub fn _print(args: fmt::Arguments) {
+ STDOUT.lock().write_fmt(args).unwrap();
+}
diff --git a/ctr-std/src/io/util.rs b/ctr-std/src/io/util.rs
new file mode 100644
index 0000000..2c68802
--- /dev/null
+++ b/ctr-std/src/io/util.rs
@@ -0,0 +1,215 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![allow(missing_copy_implementations)]
+
+use io::{self, Read, Write, ErrorKind, BufRead};
+
+/// Copies the entire contents of a reader into a writer.
+///
+/// This function will continuously read data from `reader` and then
+/// write it into `writer` in a streaming fashion until `reader`
+/// returns EOF.
+///
+/// On success, the total number of bytes that were copied from
+/// `reader` to `writer` is returned.
+///
+/// # Errors
+///
+/// This function will return an error immediately if any call to `read` or
+/// `write` returns an error. All instances of `ErrorKind::Interrupted` are
+/// handled by this function and the underlying operation is retried.
+///
+/// # Examples
+///
+/// ```
+/// use std::io;
+///
+/// # fn foo() -> io::Result<()> {
+/// let mut reader: &[u8] = b"hello";
+/// let mut writer: Vec<u8> = vec![];
+///
+/// try!(io::copy(&mut reader, &mut writer));
+///
+/// assert_eq!(reader, &writer[..]);
+/// # Ok(())
+/// # }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
+ where R: Read, W: Write
+{
+ let mut buf = [0; super::DEFAULT_BUF_SIZE];
+ let mut written = 0;
+ loop {
+ let len = match reader.read(&mut buf) {
+ Ok(0) => return Ok(written),
+ Ok(len) => len,
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => return Err(e),
+ };
+ writer.write_all(&buf[..len])?;
+ written += len as u64;
+ }
+}
+
+/// A reader which is always at EOF.
+///
+/// This struct is generally created by calling [`empty()`][empty]. Please see
+/// the documentation of `empty()` for more details.
+///
+/// [empty]: fn.empty.html
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Empty { _priv: () }
+
+/// Constructs a new handle to an empty reader.
+///
+/// All reads from the returned reader will return `Ok(0)`.
+///
+/// # Examples
+///
+/// A slightly sad example of not reading anything into a buffer:
+///
+/// ```
+/// use std::io::{self, Read};
+///
+/// let mut buffer = String::new();
+/// io::empty().read_to_string(&mut buffer).unwrap();
+/// assert!(buffer.is_empty());
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn empty() -> Empty { Empty { _priv: () } }
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Read for Empty {
+ fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> { Ok(0) }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl BufRead for Empty {
+ fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) }
+ fn consume(&mut self, _n: usize) {}
+}
+
+/// A reader which yields one byte over and over and over and over and over and...
+///
+/// This struct is generally created by calling [`repeat()`][repeat]. Please
+/// see the documentation of `repeat()` for more details.
+///
+/// [repeat]: fn.repeat.html
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Repeat { byte: u8 }
+
+/// Creates an instance of a reader that infinitely repeats one byte.
+///
+/// All reads from this reader will succeed by filling the specified buffer with
+/// the given byte.
+///
+/// # Examples
+///
+/// ```
+/// use std::io::{self, Read};
+///
+/// let mut buffer = [0; 3];
+/// io::repeat(0b101).read_exact(&mut buffer).unwrap();
+/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } }
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Read for Repeat {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ for slot in &mut *buf {
+ *slot = self.byte;
+ }
+ Ok(buf.len())
+ }
+}
+
+/// A writer which will move data into the void.
+///
+/// This struct is generally created by calling [`sink()`][sink]. Please
+/// see the documentation of `sink()` for more details.
+///
+/// [sink]: fn.sink.html
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Sink { _priv: () }
+
+/// Creates an instance of a writer which will successfully consume all data.
+///
+/// All calls to `write` on the returned instance will return `Ok(buf.len())`
+/// and the contents of the buffer will not be inspected.
+///
+/// # Examples
+///
+/// ```rust
+/// use std::io::{self, Write};
+///
+/// let buffer = vec![1, 2, 3, 5, 8];
+/// let num_bytes = io::sink().write(&buffer).unwrap();
+/// assert_eq!(num_bytes, 5);
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn sink() -> Sink { Sink { _priv: () } }
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Write for Sink {
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> { Ok(buf.len()) }
+ fn flush(&mut self) -> io::Result<()> { Ok(()) }
+}
+
+#[cfg(test)]
+mod tests {
+ use io::prelude::*;
+ use io::{copy, sink, empty, repeat};
+
+ #[test]
+ fn copy_copies() {
+ let mut r = repeat(0).take(4);
+ let mut w = sink();
+ assert_eq!(copy(&mut r, &mut w).unwrap(), 4);
+
+ let mut r = repeat(0).take(1 << 17);
+ assert_eq!(copy(&mut r as &mut Read, &mut w as &mut Write).unwrap(), 1 << 17);
+ }
+
+ #[test]
+ fn sink_sinks() {
+ let mut s = sink();
+ assert_eq!(s.write(&[]).unwrap(), 0);
+ assert_eq!(s.write(&[0]).unwrap(), 1);
+ assert_eq!(s.write(&[0; 1024]).unwrap(), 1024);
+ assert_eq!(s.by_ref().write(&[0; 1024]).unwrap(), 1024);
+ }
+
+ #[test]
+ fn empty_reads() {
+ let mut e = empty();
+ assert_eq!(e.read(&mut []).unwrap(), 0);
+ assert_eq!(e.read(&mut [0]).unwrap(), 0);
+ assert_eq!(e.read(&mut [0; 1024]).unwrap(), 0);
+ assert_eq!(e.by_ref().read(&mut [0; 1024]).unwrap(), 0);
+ }
+
+ #[test]
+ fn repeat_repeats() {
+ let mut r = repeat(4);
+ let mut b = [0; 1024];
+ assert_eq!(r.read(&mut b).unwrap(), 1024);
+ assert!(b.iter().all(|b| *b == 4));
+ }
+
+ #[test]
+ fn take_some_bytes() {
+ assert_eq!(repeat(4).take(100).bytes().count(), 100);
+ assert_eq!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
+ assert_eq!(repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(), 20);
+ }
+}
diff --git a/ctr-std/src/lib.rs b/ctr-std/src/lib.rs
new file mode 100644
index 0000000..b43e108
--- /dev/null
+++ b/ctr-std/src/lib.rs
@@ -0,0 +1,174 @@
+#![feature(alloc)]
+#![feature(allow_internal_unstable)]
+#![feature(box_syntax)]
+#![feature(collections)]
+#![feature(const_fn)]
+#![feature(compiler_builtins_lib)]
+#![feature(core_intrinsics)]
+#![feature(char_escape_debug)]
+#![feature(float_extras)]
+#![feature(fused)]
+#![feature(int_error_internals)]
+#![feature(lang_items)]
+#![feature(macro_reexport)]
+#![feature(optin_builtin_traits)]
+#![feature(prelude_import)]
+#![feature(raw)]
+#![feature(slice_concat_ext)]
+#![feature(slice_patterns)]
+#![feature(staged_api)]
+#![feature(str_internals)]
+#![feature(thread_local)]
+#![feature(try_from)]
+#![feature(unicode)]
+#![feature(zero_one)]
+#![allow(non_camel_case_types, dead_code, unused_features)]
+#![no_std]
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+#[prelude_import]
+#[allow(unused)]
+use prelude::v1::*;
+
+#[macro_reexport(assert, assert_eq, debug_assert, debug_assert_eq,
+ unreachable, unimplemented, write, writeln, try)]
+extern crate core as __core;
+
+#[macro_use]
+#[macro_reexport(vec, format)]
+extern crate collections as core_collections;
+
+extern crate alloc;
+extern crate std_unicode;
+extern crate alloc_system;
+
+// compiler-rt intrinsics
+extern crate compiler_builtins;
+
+// 3ds-specific dependencies
+extern crate ctr_libc as libc;
+
+// stealing spin's mutex implementation for now
+extern crate spin;
+
+// The standard macros that are not built-in to the compiler.
+#[macro_use]
+mod macros;
+
+// The Rust prelude
+pub mod prelude;
+
+// Public module declarations and reexports
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::any;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::cell;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::clone;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::cmp;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::convert;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::default;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::hash;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::intrinsics;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::iter;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::marker;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::mem;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::ops;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::ptr;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::raw;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::result;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::option;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::isize;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::i8;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::i16;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::i32;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::i64;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::usize;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::u8;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::u16;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::u32;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::u64;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use alloc::boxed;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use alloc::rc;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core_collections::borrow;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core_collections::fmt;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core_collections::slice;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core_collections::str;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core_collections::string;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core_collections::vec;
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use std_unicode::char;
+
+pub mod f32;
+pub mod f64;
+
+pub mod ascii;
+pub mod error;
+pub mod ffi;
+pub mod io;
+pub mod num;
+pub mod os;
+pub mod path;
+pub mod sync;
+
+// Platform-abstraction modules
+#[macro_use]
+mod sys_common;
+mod sys;
+
+// Private support modules
+mod panicking;
+mod memchr;
+
+// The runtime entry point and a few unstable public functions used by the
+// compiler
+pub mod rt;
+
+// NOTE: These two are "undefined" symbols that LLVM emits but that
+// we never actually use
+#[doc(hidden)]
+
+#[stable(feature = "3ds", since = "1.0.0")]
+#[no_mangle]
+pub unsafe extern "C" fn __aeabi_unwind_cpp_pr0() {
+ intrinsics::unreachable()
+}
+
+#[stable(feature = "3ds", since = "1.0.0")]
+#[doc(hidden)]
+#[no_mangle]
+pub unsafe extern "C" fn __aeabi_unwind_cpp_pr1() {
+ intrinsics::unreachable()
+}
diff --git a/ctr-std/src/macros.rs b/ctr-std/src/macros.rs
new file mode 100644
index 0000000..0ce6b0a
--- /dev/null
+++ b/ctr-std/src/macros.rs
@@ -0,0 +1,481 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Standard library macros
+//!
+//! This modules contains a set of macros which are exported from the standard
+//! library. Each macro is available for use when linking against the standard
+//! library.
+
+/// The entry point for panic of Rust threads.
+///
+/// This macro is used to inject panic into a Rust thread, causing the thread to
+/// panic entirely. Each thread's panic can be reaped as the `Box<Any>` type,
+/// and the single-argument form of the `panic!` macro will be the value which
+/// is transmitted.
+///
+/// The multi-argument form of this macro panics with a string and has the
+/// `format!` syntax for building a string.
+///
+/// # Examples
+///
+/// ```should_panic
+/// # #![allow(unreachable_code)]
+/// panic!();
+/// panic!("this is a terrible mistake!");
+/// panic!(4); // panic with the value of 4 to be collected elsewhere
+/// panic!("this is a {} {message}", "fancy", message = "message");
+/// ```
+#[macro_export]
+#[stable(feature = "rust1", since = "1.0.0")]
+#[allow_internal_unstable]
+macro_rules! panic {
+ () => ({
+ panic!("explicit panic")
+ });
+ ($msg:expr) => ({
+ $crate::rt::begin_panic($msg, {
+ // static requires less code at runtime, more constant data
+ static _FILE_LINE: (&'static str, u32) = (file!(), line!());
+ &_FILE_LINE
+ })
+ });
+ ($fmt:expr, $($arg:tt)+) => ({
+ $crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+), {
+ // The leading _'s are to avoid dead code warnings if this is
+ // used inside a dead function. Just `#[allow(dead_code)]` is
+ // insufficient, since the user may have
+ // `#[forbid(dead_code)]` and which cannot be overridden.
+ static _FILE_LINE: (&'static str, u32) = (file!(), line!());
+ &_FILE_LINE
+ })
+ });
+}
+
+/// Macro for printing to the standard output.
+///
+/// Equivalent to the `println!` macro except that a newline is not printed at
+/// the end of the message.
+///
+/// Note that stdout is frequently line-buffered by default so it may be
+/// necessary to use `io::stdout().flush()` to ensure the output is emitted
+/// immediately.
+///
+/// # Panics
+///
+/// Panics if writing to `io::stdout()` fails.
+///
+/// # Examples
+///
+/// ```
+/// use std::io::{self, Write};
+///
+/// print!("this ");
+/// print!("will ");
+/// print!("be ");
+/// print!("on ");
+/// print!("the ");
+/// print!("same ");
+/// print!("line ");
+///
+/// io::stdout().flush().unwrap();
+///
+/// print!("this string has a newline, why not choose println! instead?\n");
+///
+/// io::stdout().flush().unwrap();
+/// ```
+#[macro_export]
+#[stable(feature = "rust1", since = "1.0.0")]
+#[allow_internal_unstable]
+macro_rules! print {
+ ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
+}
+
+/// Macro for printing to the standard output, with a newline. On all
+/// platforms, the newline is the LINE FEED character (`\n`/`U+000A`) alone
+/// (no additional CARRIAGE RETURN (`\r`/`U+000D`).
+///
+/// Use the `format!` syntax to write data to the standard output.
+/// See `std::fmt` for more information.
+///
+/// # Panics
+///
+/// Panics if writing to `io::stdout()` fails.
+///
+/// # Examples
+///
+/// ```
+/// println!();
+/// println!("hello there!");
+/// println!("format {} arguments", "some");
+/// ```
+#[macro_export]
+#[stable(feature = "rust1", since = "1.0.0")]
+macro_rules! println {
+ () => (print!("\n"));
+ ($fmt:expr) => (print!(concat!($fmt, "\n")));
+ ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
+}
+
+/// A macro to select an event from a number of receivers.
+///
+/// This macro is used to wait for the first event to occur on a number of
+/// receivers. It places no restrictions on the types of receivers given to
+/// this macro, this can be viewed as a heterogeneous select.
+///
+/// # Examples
+///
+/// ```
+/// #![feature(mpsc_select)]
+///
+/// use std::thread;
+/// use std::sync::mpsc;
+///
+/// // two placeholder functions for now
+/// fn long_running_thread() {}
+/// fn calculate_the_answer() -> u32 { 42 }
+///
+/// let (tx1, rx1) = mpsc::channel();
+/// let (tx2, rx2) = mpsc::channel();
+///
+/// thread::spawn(move|| { long_running_thread(); tx1.send(()).unwrap(); });
+/// thread::spawn(move|| { tx2.send(calculate_the_answer()).unwrap(); });
+///
+/// select! {
+/// _ = rx1.recv() => println!("the long running thread finished first"),
+/// answer = rx2.recv() => {
+/// println!("the answer was: {}", answer.unwrap());
+/// }
+/// }
+/// # drop(rx1.recv());
+/// # drop(rx2.recv());
+/// ```
+///
+/// For more information about select, see the `std::sync::mpsc::Select` structure.
+#[macro_export]
+#[unstable(feature = "mpsc_select", issue = "27800")]
+macro_rules! select {
+ (
+ $($name:pat = $rx:ident.$meth:ident() => $code:expr),+
+ ) => ({
+ use $crate::sync::mpsc::Select;
+ let sel = Select::new();
+ $( let mut $rx = sel.handle(&$rx); )+
+ unsafe {
+ $( $rx.add(); )+
+ }
+ let ret = sel.wait();
+ $( if ret == $rx.id() { let $name = $rx.$meth(); $code } else )+
+ { unreachable!() }
+ })
+}
+
+#[cfg(test)]
+macro_rules! assert_approx_eq {
+ ($a:expr, $b:expr) => ({
+ let (a, b) = (&$a, &$b);
+ assert!((*a - *b).abs() < 1.0e-6,
+ "{} is not approximately equal to {}", *a, *b);
+ })
+}
+
+/// Built-in macros to the compiler itself.
+///
+/// These macros do not have any corresponding definition with a `macro_rules!`
+/// macro, but are documented here. Their implementations can be found hardcoded
+/// into libsyntax itself.
+#[cfg(dox)]
+pub mod builtin {
+ /// The core macro for formatted string creation & output.
+ ///
+ /// This macro produces a value of type [`fmt::Arguments`]. This value can be
+ /// passed to the functions in [`std::fmt`] for performing useful functions.
+ /// All other formatting macros ([`format!`], [`write!`], [`println!`], etc) are
+ /// proxied through this one.
+ ///
+ /// For more information, see the documentation in [`std::fmt`].
+ ///
+ /// [`fmt::Arguments`]: ../std/fmt/struct.Arguments.html
+ /// [`std::fmt`]: ../std/fmt/index.html
+ /// [`format!`]: ../std/macro.format.html
+ /// [`write!`]: ../std/macro.write.html
+ /// [`println!`]: ../std/macro.println.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::fmt;
+ ///
+ /// let s = fmt::format(format_args!("hello {}", "world"));
+ /// assert_eq!(s, format!("hello {}", "world"));
+ ///
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! format_args { ($fmt:expr, $($args:tt)*) => ({
+ /* compiler built-in */
+ }) }
+
+ /// Inspect an environment variable at compile time.
+ ///
+ /// This macro will expand to the value of the named environment variable at
+ /// compile time, yielding an expression of type `&'static str`.
+ ///
+ /// If the environment variable is not defined, then a compilation error
+ /// will be emitted. To not emit a compile error, use the `option_env!`
+ /// macro instead.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let path: &'static str = env!("PATH");
+ /// println!("the $PATH variable at the time of compiling was: {}", path);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! env { ($name:expr) => ({ /* compiler built-in */ }) }
+
+ /// Optionally inspect an environment variable at compile time.
+ ///
+ /// If the named environment variable is present at compile time, this will
+ /// expand into an expression of type `Option<&'static str>` whose value is
+ /// `Some` of the value of the environment variable. If the environment
+ /// variable is not present, then this will expand to `None`.
+ ///
+ /// A compile time error is never emitted when using this macro regardless
+ /// of whether the environment variable is present or not.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let key: Option<&'static str> = option_env!("SECRET_KEY");
+ /// println!("the secret key might be: {:?}", key);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! option_env { ($name:expr) => ({ /* compiler built-in */ }) }
+
+ /// Concatenate identifiers into one identifier.
+ ///
+ /// This macro takes any number of comma-separated identifiers, and
+ /// concatenates them all into one, yielding an expression which is a new
+ /// identifier. Note that hygiene makes it such that this macro cannot
+ /// capture local variables. Also, as a general rule, macros are only
+ /// allowed in item, statement or expression position. That means while
+ /// you may use this macro for referring to existing variables, functions or
+ /// modules etc, you cannot define a new one with it.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(concat_idents)]
+ ///
+ /// # fn main() {
+ /// fn foobar() -> u32 { 23 }
+ ///
+ /// let f = concat_idents!(foo, bar);
+ /// println!("{}", f());
+ ///
+ /// // fn concat_idents!(new, fun, name) { } // not usable in this way!
+ /// # }
+ /// ```
+ #[unstable(feature = "concat_idents_macro", issue = "29599")]
+ #[macro_export]
+ macro_rules! concat_idents {
+ ($($e:ident),*) => ({ /* compiler built-in */ })
+ }
+
+ /// Concatenates literals into a static string slice.
+ ///
+ /// This macro takes any number of comma-separated literals, yielding an
+ /// expression of type `&'static str` which represents all of the literals
+ /// concatenated left-to-right.
+ ///
+ /// Integer and floating point literals are stringified in order to be
+ /// concatenated.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let s = concat!("test", 10, 'b', true);
+ /// assert_eq!(s, "test10btrue");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! concat { ($($e:expr),*) => ({ /* compiler built-in */ }) }
+
+ /// A macro which expands to the line number on which it was invoked.
+ ///
+ /// The expanded expression has type `u32`, and the returned line is not
+ /// the invocation of the `line!()` macro itself, but rather the first macro
+ /// invocation leading up to the invocation of the `line!()` macro.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let current_line = line!();
+ /// println!("defined on line: {}", current_line);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! line { () => ({ /* compiler built-in */ }) }
+
+ /// A macro which expands to the column number on which it was invoked.
+ ///
+ /// The expanded expression has type `u32`, and the returned column is not
+ /// the invocation of the `column!()` macro itself, but rather the first macro
+ /// invocation leading up to the invocation of the `column!()` macro.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let current_col = column!();
+ /// println!("defined on column: {}", current_col);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! column { () => ({ /* compiler built-in */ }) }
+
+ /// A macro which expands to the file name from which it was invoked.
+ ///
+ /// The expanded expression has type `&'static str`, and the returned file
+ /// is not the invocation of the `file!()` macro itself, but rather the
+ /// first macro invocation leading up to the invocation of the `file!()`
+ /// macro.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let this_file = file!();
+ /// println!("defined in file: {}", this_file);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! file { () => ({ /* compiler built-in */ }) }
+
+ /// A macro which stringifies its argument.
+ ///
+ /// This macro will yield an expression of type `&'static str` which is the
+ /// stringification of all the tokens passed to the macro. No restrictions
+ /// are placed on the syntax of the macro invocation itself.
+ ///
+ /// Note that the expanded results of the input tokens may change in the
+ /// future. You should be careful if you rely on the output.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let one_plus_one = stringify!(1 + 1);
+ /// assert_eq!(one_plus_one, "1 + 1");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! stringify { ($t:tt) => ({ /* compiler built-in */ }) }
+
+ /// Includes a utf8-encoded file as a string.
+ ///
+ /// The file is located relative to the current file. (similarly to how
+ /// modules are found)
+ ///
+ /// This macro will yield an expression of type `&'static str` which is the
+ /// contents of the file.
+ ///
+ /// # Examples
+ ///
+ /// ```rust,ignore
+ /// let secret_key = include_str!("secret-key.ascii");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! include_str { ($file:expr) => ({ /* compiler built-in */ }) }
+
+ /// Includes a file as a reference to a byte array.
+ ///
+ /// The file is located relative to the current file. (similarly to how
+ /// modules are found)
+ ///
+ /// This macro will yield an expression of type `&'static [u8; N]` which is
+ /// the contents of the file.
+ ///
+ /// # Examples
+ ///
+ /// ```rust,ignore
+ /// let secret_key = include_bytes!("secret-key.bin");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! include_bytes { ($file:expr) => ({ /* compiler built-in */ }) }
+
+ /// Expands to a string that represents the current module path.
+ ///
+ /// The current module path can be thought of as the hierarchy of modules
+ /// leading back up to the crate root. The first component of the path
+ /// returned is the name of the crate currently being compiled.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// mod test {
+ /// pub fn foo() {
+ /// assert!(module_path!().ends_with("test"));
+ /// }
+ /// }
+ ///
+ /// test::foo();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! module_path { () => ({ /* compiler built-in */ }) }
+
+ /// Boolean evaluation of configuration flags.
+ ///
+ /// In addition to the `#[cfg]` attribute, this macro is provided to allow
+ /// boolean expression evaluation of configuration flags. This frequently
+ /// leads to less duplicated code.
+ ///
+ /// The syntax given to this macro is the same syntax as [the `cfg`
+ /// attribute](../reference.html#conditional-compilation).
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let my_directory = if cfg!(windows) {
+ /// "windows-specific-directory"
+ /// } else {
+ /// "unix-directory"
+ /// };
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! cfg { ($($cfg:tt)*) => ({ /* compiler built-in */ }) }
+
+ /// Parse a file as an expression or an item according to the context.
+ ///
+ /// The file is located relative to the current file. (similarly to how
+ /// modules are found)
+ ///
+ /// Using this macro is often a bad idea, because if the file is
+ /// parsed as an expression, it is going to be placed in the
+ /// surrounding code unhygenically. This could result in variables
+ /// or functions being different from what the file expected if
+ /// there are variables or functions that have the same name in
+ /// the current file.
+ ///
+ /// # Examples
+ ///
+ /// ```ignore
+ /// fn foo() {
+ /// include!("/path/to/a/file")
+ /// }
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[macro_export]
+ macro_rules! include { ($file:expr) => ({ /* compiler built-in */ }) }
+}
diff --git a/ctr-std/src/memchr.rs b/ctr-std/src/memchr.rs
new file mode 100644
index 0000000..7c8c97a
--- /dev/null
+++ b/ctr-std/src/memchr.rs
@@ -0,0 +1,143 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+//
+// Original implementation taken from rust-memchr
+// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
+
+/// A safe interface to `memchr`.
+///
+/// Returns the index corresponding to the first occurrence of `needle` in
+/// `haystack`, or `None` if one is not found.
+///
+/// memchr reduces to super-optimized machine code at around an order of
+/// magnitude faster than `haystack.iter().position(|&b| b == needle)`.
+/// (See benchmarks.)
+///
+/// # Example
+///
+/// This shows how to find the first position of a byte in a byte string.
+///
+/// ```rust,ignore
+/// use memchr::memchr;
+///
+/// let haystack = b"the quick brown fox";
+/// assert_eq!(memchr(b'k', haystack), Some(8));
+/// ```
+#[inline]
+pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
+ ::sys::memchr::memchr(needle, haystack)
+}
+
+/// A safe interface to `memrchr`.
+///
+/// Returns the index corresponding to the last occurrence of `needle` in
+/// `haystack`, or `None` if one is not found.
+///
+/// # Example
+///
+/// This shows how to find the last position of a byte in a byte string.
+///
+/// ```rust,ignore
+/// use memchr::memrchr;
+///
+/// let haystack = b"the quick brown fox";
+/// assert_eq!(memrchr(b'o', haystack), Some(17));
+/// ```
+#[inline]
+pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
+ ::sys::memchr::memrchr(needle, haystack)
+}
+
+#[cfg(test)]
+mod tests {
+ // test the implementations for the current plattform
+ use super::{memchr, memrchr};
+
+ #[test]
+ fn matches_one() {
+ assert_eq!(Some(0), memchr(b'a', b"a"));
+ }
+
+ #[test]
+ fn matches_begin() {
+ assert_eq!(Some(0), memchr(b'a', b"aaaa"));
+ }
+
+ #[test]
+ fn matches_end() {
+ assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
+ }
+
+ #[test]
+ fn matches_nul() {
+ assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
+ }
+
+ #[test]
+ fn matches_past_nul() {
+ assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
+ }
+
+ #[test]
+ fn no_match_empty() {
+ assert_eq!(None, memchr(b'a', b""));
+ }
+
+ #[test]
+ fn no_match() {
+ assert_eq!(None, memchr(b'a', b"xyz"));
+ }
+
+ #[test]
+ fn matches_one_reversed() {
+ assert_eq!(Some(0), memrchr(b'a', b"a"));
+ }
+
+ #[test]
+ fn matches_begin_reversed() {
+ assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
+ }
+
+ #[test]
+ fn matches_end_reversed() {
+ assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
+ }
+
+ #[test]
+ fn matches_nul_reversed() {
+ assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
+ }
+
+ #[test]
+ fn matches_past_nul_reversed() {
+ assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
+ }
+
+ #[test]
+ fn no_match_empty_reversed() {
+ assert_eq!(None, memrchr(b'a', b""));
+ }
+
+ #[test]
+ fn no_match_reversed() {
+ assert_eq!(None, memrchr(b'a', b"xyz"));
+ }
+
+ #[test]
+ fn each_alignment() {
+ let mut data = [1u8; 64];
+ let needle = 2;
+ let pos = 40;
+ data[pos] = needle;
+ for start in 0..16 {
+ assert_eq!(Some(pos - start), memchr(needle, &data[start..]));
+ }
+ }
+}
diff --git a/ctr-std/src/num.rs b/ctr-std/src/num.rs
new file mode 100644
index 0000000..d1c2fc3
--- /dev/null
+++ b/ctr-std/src/num.rs
@@ -0,0 +1,293 @@
+// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Additional functionality for numerics.
+//!
+//! This module provides some extra types that are useful when doing numerical
+//! work. See the individual documentation for each piece for more information.
+
+#![stable(feature = "rust1", since = "1.0.0")]
+#![allow(missing_docs)]
+
+#[stable(feature = "rust1", since = "1.0.0")]
+#[allow(deprecated)]
+pub use core::num::{Zero, One};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::num::{FpCategory, ParseIntError, ParseFloatError, TryFromIntError};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::num::Wrapping;
+
+#[cfg(test)] use fmt;
+#[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem};
+
+/// Helper function for testing numeric operations
+#[cfg(test)]
+pub fn test_num<T>(ten: T, two: T) where
+ T: PartialEq
+ + Add<Output=T> + Sub<Output=T>
+ + Mul<Output=T> + Div<Output=T>
+ + Rem<Output=T> + fmt::Debug
+ + Copy
+{
+ assert_eq!(ten.add(two), ten + two);
+ assert_eq!(ten.sub(two), ten - two);
+ assert_eq!(ten.mul(two), ten * two);
+ assert_eq!(ten.div(two), ten / two);
+ assert_eq!(ten.rem(two), ten % two);
+}
+
+#[cfg(test)]
+mod tests {
+ use u8;
+ use u16;
+ use u32;
+ use u64;
+ use usize;
+ use ops::Mul;
+
+ #[test]
+ fn test_saturating_add_uint() {
+ use usize::MAX;
+ assert_eq!(3_usize.saturating_add(5_usize), 8_usize);
+ assert_eq!(3_usize.saturating_add(MAX-1), MAX);
+ assert_eq!(MAX.saturating_add(MAX), MAX);
+ assert_eq!((MAX-2).saturating_add(1), MAX-1);
+ }
+
+ #[test]
+ fn test_saturating_sub_uint() {
+ use usize::MAX;
+ assert_eq!(5_usize.saturating_sub(3_usize), 2_usize);
+ assert_eq!(3_usize.saturating_sub(5_usize), 0_usize);
+ assert_eq!(0_usize.saturating_sub(1_usize), 0_usize);
+ assert_eq!((MAX-1).saturating_sub(MAX), 0);
+ }
+
+ #[test]
+ fn test_saturating_add_int() {
+ use isize::{MIN,MAX};
+ assert_eq!(3i32.saturating_add(5), 8);
+ assert_eq!(3isize.saturating_add(MAX-1), MAX);
+ assert_eq!(MAX.saturating_add(MAX), MAX);
+ assert_eq!((MAX-2).saturating_add(1), MAX-1);
+ assert_eq!(3i32.saturating_add(-5), -2);
+ assert_eq!(MIN.saturating_add(-1), MIN);
+ assert_eq!((-2isize).saturating_add(-MAX), MIN);
+ }
+
+ #[test]
+ fn test_saturating_sub_int() {
+ use isize::{MIN,MAX};
+ assert_eq!(3i32.saturating_sub(5), -2);
+ assert_eq!(MIN.saturating_sub(1), MIN);
+ assert_eq!((-2isize).saturating_sub(MAX), MIN);
+ assert_eq!(3i32.saturating_sub(-5), 8);
+ assert_eq!(3isize.saturating_sub(-(MAX-1)), MAX);
+ assert_eq!(MAX.saturating_sub(-MAX), MAX);
+ assert_eq!((MAX-2).saturating_sub(-1), MAX-1);
+ }
+
+ #[test]
+ fn test_checked_add() {
+ let five_less = usize::MAX - 5;
+ assert_eq!(five_less.checked_add(0), Some(usize::MAX - 5));
+ assert_eq!(five_less.checked_add(1), Some(usize::MAX - 4));
+ assert_eq!(five_less.checked_add(2), Some(usize::MAX - 3));
+ assert_eq!(five_less.checked_add(3), Some(usize::MAX - 2));
+ assert_eq!(five_less.checked_add(4), Some(usize::MAX - 1));
+ assert_eq!(five_less.checked_add(5), Some(usize::MAX));
+ assert_eq!(five_less.checked_add(6), None);
+ assert_eq!(five_less.checked_add(7), None);
+ }
+
+ #[test]
+ fn test_checked_sub() {
+ assert_eq!(5_usize.checked_sub(0), Some(5));
+ assert_eq!(5_usize.checked_sub(1), Some(4));
+ assert_eq!(5_usize.checked_sub(2), Some(3));
+ assert_eq!(5_usize.checked_sub(3), Some(2));
+ assert_eq!(5_usize.checked_sub(4), Some(1));
+ assert_eq!(5_usize.checked_sub(5), Some(0));
+ assert_eq!(5_usize.checked_sub(6), None);
+ assert_eq!(5_usize.checked_sub(7), None);
+ }
+
+ #[test]
+ fn test_checked_mul() {
+ let third = usize::MAX / 3;
+ assert_eq!(third.checked_mul(0), Some(0));
+ assert_eq!(third.checked_mul(1), Some(third));
+ assert_eq!(third.checked_mul(2), Some(third * 2));
+ assert_eq!(third.checked_mul(3), Some(third * 3));
+ assert_eq!(third.checked_mul(4), None);
+ }
+
+ macro_rules! test_is_power_of_two {
+ ($test_name:ident, $T:ident) => (
+ fn $test_name() {
+ #![test]
+ assert_eq!((0 as $T).is_power_of_two(), false);
+ assert_eq!((1 as $T).is_power_of_two(), true);
+ assert_eq!((2 as $T).is_power_of_two(), true);
+ assert_eq!((3 as $T).is_power_of_two(), false);
+ assert_eq!((4 as $T).is_power_of_two(), true);
+ assert_eq!((5 as $T).is_power_of_two(), false);
+ assert_eq!(($T::MAX / 2 + 1).is_power_of_two(), true);
+ }
+ )
+ }
+
+ test_is_power_of_two!{ test_is_power_of_two_u8, u8 }
+ test_is_power_of_two!{ test_is_power_of_two_u16, u16 }
+ test_is_power_of_two!{ test_is_power_of_two_u32, u32 }
+ test_is_power_of_two!{ test_is_power_of_two_u64, u64 }
+ test_is_power_of_two!{ test_is_power_of_two_uint, usize }
+
+ macro_rules! test_next_power_of_two {
+ ($test_name:ident, $T:ident) => (
+ fn $test_name() {
+ #![test]
+ assert_eq!((0 as $T).next_power_of_two(), 1);
+ let mut next_power = 1;
+ for i in 1 as $T..40 {
+ assert_eq!(i.next_power_of_two(), next_power);
+ if i == next_power { next_power *= 2 }
+ }
+ }
+ )
+ }
+
+ test_next_power_of_two! { test_next_power_of_two_u8, u8 }
+ test_next_power_of_two! { test_next_power_of_two_u16, u16 }
+ test_next_power_of_two! { test_next_power_of_two_u32, u32 }
+ test_next_power_of_two! { test_next_power_of_two_u64, u64 }
+ test_next_power_of_two! { test_next_power_of_two_uint, usize }
+
+ macro_rules! test_checked_next_power_of_two {
+ ($test_name:ident, $T:ident) => (
+ fn $test_name() {
+ #![test]
+ assert_eq!((0 as $T).checked_next_power_of_two(), Some(1));
+ assert!(($T::MAX / 2).checked_next_power_of_two().is_some());
+ assert_eq!(($T::MAX - 1).checked_next_power_of_two(), None);
+ assert_eq!($T::MAX.checked_next_power_of_two(), None);
+ let mut next_power = 1;
+ for i in 1 as $T..40 {
+ assert_eq!(i.checked_next_power_of_two(), Some(next_power));
+ if i == next_power { next_power *= 2 }
+ }
+ }
+ )
+ }
+
+ test_checked_next_power_of_two! { test_checked_next_power_of_two_u8, u8 }
+ test_checked_next_power_of_two! { test_checked_next_power_of_two_u16, u16 }
+ test_checked_next_power_of_two! { test_checked_next_power_of_two_u32, u32 }
+ test_checked_next_power_of_two! { test_checked_next_power_of_two_u64, u64 }
+ test_checked_next_power_of_two! { test_checked_next_power_of_two_uint, usize }
+
+ #[test]
+ fn test_pow() {
+ fn naive_pow<T: Mul<Output=T> + Copy>(one: T, base: T, exp: usize) -> T {
+ (0..exp).fold(one, |acc, _| acc * base)
+ }
+ macro_rules! assert_pow {
+ (($num:expr, $exp:expr) => $expected:expr) => {{
+ let result = $num.pow($exp);
+ assert_eq!(result, $expected);
+ assert_eq!(result, naive_pow(1, $num, $exp));
+ }}
+ }
+ assert_pow!((3u32, 0 ) => 1);
+ assert_pow!((5u32, 1 ) => 5);
+ assert_pow!((-4i32, 2 ) => 16);
+ assert_pow!((8u32, 3 ) => 512);
+ assert_pow!((2u64, 50) => 1125899906842624);
+ }
+
+ #[test]
+ fn test_uint_to_str_overflow() {
+ let mut u8_val: u8 = 255;
+ assert_eq!(u8_val.to_string(), "255");
+
+ u8_val = u8_val.wrapping_add(1);
+ assert_eq!(u8_val.to_string(), "0");
+
+ let mut u16_val: u16 = 65_535;
+ assert_eq!(u16_val.to_string(), "65535");
+
+ u16_val = u16_val.wrapping_add(1);
+ assert_eq!(u16_val.to_string(), "0");
+
+ let mut u32_val: u32 = 4_294_967_295;
+ assert_eq!(u32_val.to_string(), "4294967295");
+
+ u32_val = u32_val.wrapping_add(1);
+ assert_eq!(u32_val.to_string(), "0");
+
+ let mut u64_val: u64 = 18_446_744_073_709_551_615;
+ assert_eq!(u64_val.to_string(), "18446744073709551615");
+
+ u64_val = u64_val.wrapping_add(1);
+ assert_eq!(u64_val.to_string(), "0");
+ }
+
+ fn from_str<T: ::str::FromStr>(t: &str) -> Option<T> {
+ ::str::FromStr::from_str(t).ok()
+ }
+
+ #[test]
+ fn test_uint_from_str_overflow() {
+ let mut u8_val: u8 = 255;
+ assert_eq!(from_str::<u8>("255"), Some(u8_val));
+ assert_eq!(from_str::<u8>("256"), None);
+
+ u8_val = u8_val.wrapping_add(1);
+ assert_eq!(from_str::<u8>("0"), Some(u8_val));
+ assert_eq!(from_str::<u8>("-1"), None);
+
+ let mut u16_val: u16 = 65_535;
+ assert_eq!(from_str::<u16>("65535"), Some(u16_val));
+ assert_eq!(from_str::<u16>("65536"), None);
+
+ u16_val = u16_val.wrapping_add(1);
+ assert_eq!(from_str::<u16>("0"), Some(u16_val));
+ assert_eq!(from_str::<u16>("-1"), None);
+
+ let mut u32_val: u32 = 4_294_967_295;
+ assert_eq!(from_str::<u32>("4294967295"), Some(u32_val));
+ assert_eq!(from_str::<u32>("4294967296"), None);
+
+ u32_val = u32_val.wrapping_add(1);
+ assert_eq!(from_str::<u32>("0"), Some(u32_val));
+ assert_eq!(from_str::<u32>("-1"), None);
+
+ let mut u64_val: u64 = 18_446_744_073_709_551_615;
+ assert_eq!(from_str::<u64>("18446744073709551615"), Some(u64_val));
+ assert_eq!(from_str::<u64>("18446744073709551616"), None);
+
+ u64_val = u64_val.wrapping_add(1);
+ assert_eq!(from_str::<u64>("0"), Some(u64_val));
+ assert_eq!(from_str::<u64>("-1"), None);
+ }
+}
+
+
+#[cfg(test)]
+mod bench {
+ extern crate test;
+ use self::test::Bencher;
+
+ #[bench]
+ fn bench_pow_function(b: &mut Bencher) {
+ let v = (0..1024).collect::<Vec<u32>>();
+ b.iter(|| {v.iter().fold(0u32, |old, new| old.pow(*new as u32));});
+ }
+}
diff --git a/ctr-std/src/os/mod.rs b/ctr-std/src/os/mod.rs
new file mode 100644
index 0000000..bd05ac3
--- /dev/null
+++ b/ctr-std/src/os/mod.rs
@@ -0,0 +1,17 @@
+// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! OS-specific functionality.
+
+#![stable(feature = "os", since = "1.0.0")]
+#![allow(missing_docs, bad_style)]
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use sys::ext as unix;
diff --git a/ctr-std/src/panicking.rs b/ctr-std/src/panicking.rs
new file mode 100644
index 0000000..d6b922d
--- /dev/null
+++ b/ctr-std/src/panicking.rs
@@ -0,0 +1,59 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Implementation of various bits and pieces of the `panic!` macro and
+//! associated runtime pieces.
+
+use any::Any;
+use fmt;
+use __core::fmt::Display;
+
+///The compiler wants this to be here. Otherwise it won't be happy. And we like happy compilers.
+#[lang = "eh_personality"]
+extern fn eh_personality() {}
+
+/// Entry point of panic from the libcore crate.
+#[lang = "panic_fmt"]
+extern fn rust_begin_panic(msg: fmt::Arguments, file: &'static str, line: u32) -> ! {
+ begin_panic_fmt(&msg, &(file, line))
+}
+
+/// The entry point for panicking with a formatted message.
+///
+/// This is designed to reduce the amount of code required at the call
+/// site as much as possible (so that `panic!()` has as low an impact
+/// on (e.g.) the inlining of other functions as possible), by moving
+/// the actual formatting into this shared place.
+#[unstable(feature = "libstd_sys_internals",
+ reason = "used by the panic! macro",
+ issue = "0")]
+#[inline(never)] #[cold]
+pub fn begin_panic_fmt(msg: &fmt::Arguments, file_line: &(&'static str, u32)) -> ! {
+ use fmt::Write;
+
+ let mut s = String::new();
+ let _ = s.write_fmt(*msg);
+ begin_panic(s, file_line);
+}
+
+/// This is where the main panic logic happens.
+#[inline(never)]
+#[cold]
+pub fn begin_panic<M: Any + Send + Display>(msg: M, file_line: &(&'static str, u32)) -> ! {
+ let msg = Box::new(msg);
+ let (file, line) = *file_line;
+
+ print!("--------------------------------------------------");
+ println!("PANIC in {} at line {}:", file, line);
+ println!(" {}", msg);
+ print!("\x1b[29;00H--------------------------------------------------");
+
+ loop {}
+}
diff --git a/ctr-std/src/path.rs b/ctr-std/src/path.rs
new file mode 100644
index 0000000..92c561f
--- /dev/null
+++ b/ctr-std/src/path.rs
@@ -0,0 +1,3561 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Cross-platform path manipulation.
+//!
+//! This module provides two types, `PathBuf` and `Path` (akin to `String` and
+//! `str`), for working with paths abstractly. These types are thin wrappers
+//! around `OsString` and `OsStr` respectively, meaning that they work directly
+//! on strings according to the local platform's path syntax.
+//!
+//! ## Simple usage
+//!
+//! Path manipulation includes both parsing components from slices and building
+//! new owned paths.
+//!
+//! To parse a path, you can create a `Path` slice from a `str`
+//! slice and start asking questions:
+//!
+//! ```rust
+//! use std::path::Path;
+//!
+//! let path = Path::new("/tmp/foo/bar.txt");
+//! let file = path.file_name();
+//! let extension = path.extension();
+//! let parent_dir = path.parent();
+//! ```
+//!
+//! To build or modify paths, use `PathBuf`:
+//!
+//! ```rust
+//! use std::path::PathBuf;
+//!
+//! let mut path = PathBuf::from("c:\\");
+//! path.push("windows");
+//! path.push("system32");
+//! path.set_extension("dll");
+//! ```
+//!
+//! ## Path components and normalization
+//!
+//! The path APIs are built around the notion of "components", which roughly
+//! correspond to the substrings between path separators (`/` and, on Windows,
+//! `\`). The APIs for path parsing are largely specified in terms of the path's
+//! components, so it's important to clearly understand how those are
+//! determined.
+//!
+//! A path can always be reconstructed into an *equivalent* path by
+//! putting together its components via `push`. Syntactically, the
+//! paths may differ by the normalization described below.
+//!
+//! ### Component types
+//!
+//! Components come in several types:
+//!
+//! * Normal components are the default: standard references to files or
+//! directories. The path `a/b` has two normal components, `a` and `b`.
+//!
+//! * Current directory components represent the `.` character. For example,
+//! `./a` has a current directory component and a normal component `a`.
+//!
+//! * The root directory component represents a separator that designates
+//! starting from root. For example, `/a/b` has a root directory component
+//! followed by normal components `a` and `b`.
+//!
+//! On Windows, an additional component type comes into play:
+//!
+//! * Prefix components, of which there is a large variety. For example, `C:`
+//! and `\\server\share` are prefixes. The path `C:windows` has a prefix
+//! component `C:` and a normal component `windows`; the path `C:\windows` has a
+//! prefix component `C:`, a root directory component, and a normal component
+//! `windows`.
+//!
+//! ### Normalization
+//!
+//! Aside from splitting on the separator(s), there is a small amount of
+//! "normalization":
+//!
+//! * Repeated separators are ignored: `a/b` and `a//b` both have components `a`
+//! and `b`.
+//!
+//! * Occurrences of `.` are normalized away, *except* if they are at
+//! the beginning of the path (in which case they are often meaningful
+//! in terms of path searching). So, for example, `a/./b`, `a/b/`,
+//! `/a/b/.` and `a/b` all have components `a` and `b`, but `./a/b`
+//! has a leading current directory component.
+//!
+//! No other normalization takes place by default. In particular,
+//! `a/c` and `a/b/../c` are distinct, to account for the possibility
+//! that `b` is a symbolic link (so its parent isn't `a`). Further
+//! normalization is possible to build on top of the components APIs,
+//! and will be included in this library in the near future.
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+use ascii::*;
+use borrow::{Borrow, Cow};
+use cmp;
+use error::Error;
+use fmt;
+//use fs;
+use hash::{Hash, Hasher};
+//use io;
+use iter::{self, FusedIterator};
+use mem;
+use ops::{self, Deref};
+
+use ffi::{OsStr, OsString};
+
+use sys::path::{is_sep_byte, is_verbatim_sep, MAIN_SEP_STR, parse_prefix};
+
+////////////////////////////////////////////////////////////////////////////////
+// GENERAL NOTES
+////////////////////////////////////////////////////////////////////////////////
+//
+// Parsing in this module is done by directly transmuting OsStr to [u8] slices,
+// taking advantage of the fact that OsStr always encodes ASCII characters
+// as-is. Eventually, this transmutation should be replaced by direct uses of
+// OsStr APIs for parsing, but it will take a while for those to become
+// available.
+
+////////////////////////////////////////////////////////////////////////////////
+// Windows Prefixes
+////////////////////////////////////////////////////////////////////////////////
+
+/// Path prefixes (Windows only).
+///
+/// Windows uses a variety of path styles, including references to drive
+/// volumes (like `C:`), network shared folders (like `\\server\share`) and
+/// others. In addition, some path prefixes are "verbatim", in which case
+/// `/` is *not* treated as a separator and essentially no normalization is
+/// performed.
+#[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub enum Prefix<'a> {
+ /// Prefix `\\?\`, together with the given component immediately following it.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Verbatim(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
+
+ /// Prefix `\\?\UNC\`, with the "server" and "share" components following it.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ VerbatimUNC(
+ #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
+ #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
+ ),
+
+ /// Prefix like `\\?\C:\`, for the given drive letter
+ #[stable(feature = "rust1", since = "1.0.0")]
+ VerbatimDisk(#[stable(feature = "rust1", since = "1.0.0")] u8),
+
+ /// Prefix `\\.\`, together with the given component immediately following it.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ DeviceNS(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
+
+ /// Prefix `\\server\share`, with the given "server" and "share" components.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ UNC(
+ #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
+ #[stable(feature = "rust1", since = "1.0.0")] &'a OsStr,
+ ),
+
+ /// Prefix `C:` for the given disk drive.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Disk(#[stable(feature = "rust1", since = "1.0.0")] u8),
+}
+
+impl<'a> Prefix<'a> {
+ #[inline]
+ fn len(&self) -> usize {
+ use self::Prefix::*;
+ fn os_str_len(s: &OsStr) -> usize {
+ os_str_as_u8_slice(s).len()
+ }
+ match *self {
+ Verbatim(x) => 4 + os_str_len(x),
+ VerbatimUNC(x, y) => {
+ 8 + os_str_len(x) +
+ if os_str_len(y) > 0 {
+ 1 + os_str_len(y)
+ } else {
+ 0
+ }
+ },
+ VerbatimDisk(_) => 6,
+ UNC(x, y) => {
+ 2 + os_str_len(x) +
+ if os_str_len(y) > 0 {
+ 1 + os_str_len(y)
+ } else {
+ 0
+ }
+ },
+ DeviceNS(x) => 4 + os_str_len(x),
+ Disk(_) => 2,
+ }
+
+ }
+
+ /// Determines if the prefix is verbatim, i.e. begins with `\\?\`.
+ #[inline]
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn is_verbatim(&self) -> bool {
+ use self::Prefix::*;
+ match *self {
+ Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..) => true,
+ _ => false,
+ }
+ }
+
+ #[inline]
+ fn is_drive(&self) -> bool {
+ match *self {
+ Prefix::Disk(_) => true,
+ _ => false,
+ }
+ }
+
+ #[inline]
+ fn has_implicit_root(&self) -> bool {
+ !self.is_drive()
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// Exposed parsing helpers
+////////////////////////////////////////////////////////////////////////////////
+
+/// Determines whether the character is one of the permitted path
+/// separators for the current platform.
+///
+/// # Examples
+///
+/// ```
+/// use std::path;
+///
+/// assert!(path::is_separator('/'));
+/// assert!(!path::is_separator('❤'));
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn is_separator(c: char) -> bool {
+ c.is_ascii() && is_sep_byte(c as u8)
+}
+
+/// The primary separator for the current platform
+#[stable(feature = "rust1", since = "1.0.0")]
+pub const MAIN_SEPARATOR: char = ::sys::path::MAIN_SEP;
+
+////////////////////////////////////////////////////////////////////////////////
+// Misc helpers
+////////////////////////////////////////////////////////////////////////////////
+
+// Iterate through `iter` while it matches `prefix`; return `None` if `prefix`
+// is not a prefix of `iter`, otherwise return `Some(iter_after_prefix)` giving
+// `iter` after having exhausted `prefix`.
+fn iter_after<A, I, J>(mut iter: I, mut prefix: J) -> Option<I>
+ where I: Iterator<Item = A> + Clone,
+ J: Iterator<Item = A>,
+ A: PartialEq
+{
+ loop {
+ let mut iter_next = iter.clone();
+ match (iter_next.next(), prefix.next()) {
+ (Some(ref x), Some(ref y)) if x == y => (),
+ (Some(_), Some(_)) => return None,
+ (Some(_), None) => return Some(iter),
+ (None, None) => return Some(iter),
+ (None, Some(_)) => return None,
+ }
+ iter = iter_next;
+ }
+}
+
+// See note at the top of this module to understand why these are used:
+fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
+ unsafe { mem::transmute(s) }
+}
+unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
+ mem::transmute(s)
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// Cross-platform, iterator-independent parsing
+////////////////////////////////////////////////////////////////////////////////
+
+/// Says whether the first byte after the prefix is a separator.
+fn has_physical_root(s: &[u8], prefix: Option<Prefix>) -> bool {
+ let path = if let Some(p) = prefix {
+ &s[p.len()..]
+ } else {
+ s
+ };
+ !path.is_empty() && is_sep_byte(path[0])
+}
+
+// basic workhorse for splitting stem and extension
+fn split_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
+ unsafe {
+ if os_str_as_u8_slice(file) == b".." {
+ return (Some(file), None);
+ }
+
+ // The unsafety here stems from converting between &OsStr and &[u8]
+ // and back. This is safe to do because (1) we only look at ASCII
+ // contents of the encoding and (2) new &OsStr values are produced
+ // only from ASCII-bounded slices of existing &OsStr values.
+
+ let mut iter = os_str_as_u8_slice(file).rsplitn(2, |b| *b == b'.');
+ let after = iter.next();
+ let before = iter.next();
+ if before == Some(b"") {
+ (Some(file), None)
+ } else {
+ (before.map(|s| u8_slice_as_os_str(s)),
+ after.map(|s| u8_slice_as_os_str(s)))
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// The core iterators
+////////////////////////////////////////////////////////////////////////////////
+
+/// Component parsing works by a double-ended state machine; the cursors at the
+/// front and back of the path each keep track of what parts of the path have
+/// been consumed so far.
+///
+/// Going front to back, a path is made up of a prefix, a starting
+/// directory component, and a body (of normal components)
+#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
+enum State {
+ Prefix = 0, // c:
+ StartDir = 1, // / or . or nothing
+ Body = 2, // foo/bar/baz
+ Done = 3,
+}
+
+/// A Windows path prefix, e.g. `C:` or `\\server\share`.
+///
+/// Does not occur on Unix.
+#[stable(feature = "rust1", since = "1.0.0")]
+#[derive(Copy, Clone, Eq, Debug)]
+pub struct PrefixComponent<'a> {
+ /// The prefix as an unparsed `OsStr` slice.
+ raw: &'a OsStr,
+
+ /// The parsed prefix data.
+ parsed: Prefix<'a>,
+}
+
+impl<'a> PrefixComponent<'a> {
+ /// The parsed prefix data.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn kind(&self) -> Prefix<'a> {
+ self.parsed
+ }
+
+ /// The raw `OsStr` slice for this prefix.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn as_os_str(&self) -> &'a OsStr {
+ self.raw
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> cmp::PartialEq for PrefixComponent<'a> {
+ fn eq(&self, other: &PrefixComponent<'a>) -> bool {
+ cmp::PartialEq::eq(&self.parsed, &other.parsed)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> cmp::PartialOrd for PrefixComponent<'a> {
+ fn partial_cmp(&self, other: &PrefixComponent<'a>) -> Option<cmp::Ordering> {
+ cmp::PartialOrd::partial_cmp(&self.parsed, &other.parsed)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> cmp::Ord for PrefixComponent<'a> {
+ fn cmp(&self, other: &PrefixComponent<'a>) -> cmp::Ordering {
+ cmp::Ord::cmp(&self.parsed, &other.parsed)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> Hash for PrefixComponent<'a> {
+ fn hash<H: Hasher>(&self, h: &mut H) {
+ self.parsed.hash(h);
+ }
+}
+
+/// A single component of a path.
+///
+/// See the module documentation for an in-depth explanation of components and
+/// their role in the API.
+///
+/// This `enum` is created from iterating over the [`path::Components`]
+/// `struct`.
+///
+/// # Examples
+///
+/// ```rust
+/// use std::path::{Component, Path};
+///
+/// let path = Path::new("/tmp/foo/bar.txt");
+/// let components = path.components().collect::<Vec<_>>();
+/// assert_eq!(&components, &[
+/// Component::RootDir,
+/// Component::Normal("tmp".as_ref()),
+/// Component::Normal("foo".as_ref()),
+/// Component::Normal("bar.txt".as_ref()),
+/// ]);
+/// ```
+///
+/// [`path::Components`]: struct.Components.html
+#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub enum Component<'a> {
+ /// A Windows path prefix, e.g. `C:` or `\\server\share`.
+ ///
+ /// Does not occur on Unix.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Prefix(
+ #[stable(feature = "rust1", since = "1.0.0")] PrefixComponent<'a>
+ ),
+
+ /// The root directory component, appears after any prefix and before anything else
+ #[stable(feature = "rust1", since = "1.0.0")]
+ RootDir,
+
+ /// A reference to the current directory, i.e. `.`
+ #[stable(feature = "rust1", since = "1.0.0")]
+ CurDir,
+
+ /// A reference to the parent directory, i.e. `..`
+ #[stable(feature = "rust1", since = "1.0.0")]
+ ParentDir,
+
+ /// A normal component, i.e. `a` and `b` in `a/b`
+ #[stable(feature = "rust1", since = "1.0.0")]
+ Normal(#[stable(feature = "rust1", since = "1.0.0")] &'a OsStr),
+}
+
+impl<'a> Component<'a> {
+ /// Extracts the underlying `OsStr` slice
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn as_os_str(self) -> &'a OsStr {
+ match self {
+ Component::Prefix(p) => p.as_os_str(),
+ Component::RootDir => OsStr::new(MAIN_SEP_STR),
+ Component::CurDir => OsStr::new("."),
+ Component::ParentDir => OsStr::new(".."),
+ Component::Normal(path) => path,
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> AsRef<OsStr> for Component<'a> {
+ fn as_ref(&self) -> &OsStr {
+ self.as_os_str()
+ }
+}
+
+/// The core iterator giving the components of a path.
+///
+/// See the module documentation for an in-depth explanation of components and
+/// their role in the API.
+///
+/// This `struct` is created by the [`path::Path::components`] method.
+///
+/// # Examples
+///
+/// ```
+/// use std::path::Path;
+///
+/// let path = Path::new("/tmp/foo/bar.txt");
+///
+/// for component in path.components() {
+/// println!("{:?}", component);
+/// }
+/// ```
+///
+/// [`path::Path::components`]: struct.Path.html#method.components
+#[derive(Clone)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Components<'a> {
+ // The path left to parse components from
+ path: &'a [u8],
+
+ // The prefix as it was originally parsed, if any
+ prefix: Option<Prefix<'a>>,
+
+ // true if path *physically* has a root separator; for most Windows
+ // prefixes, it may have a "logical" rootseparator for the purposes of
+ // normalization, e.g. \\server\share == \\server\share\.
+ has_physical_root: bool,
+
+ // The iterator is double-ended, and these two states keep track of what has
+ // been produced from either end
+ front: State,
+ back: State,
+}
+
+/// An iterator over the components of a path, as `OsStr` slices.
+#[derive(Clone)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Iter<'a> {
+ inner: Components<'a>,
+}
+
+#[stable(feature = "path_components_debug", since = "1.13.0")]
+impl<'a> fmt::Debug for Components<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ struct DebugHelper<'a>(&'a Path);
+
+ impl<'a> fmt::Debug for DebugHelper<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.debug_list()
+ .entries(self.0.components())
+ .finish()
+ }
+ }
+
+ f.debug_tuple("Components")
+ .field(&DebugHelper(self.as_path()))
+ .finish()
+ }
+}
+
+impl<'a> Components<'a> {
+ // how long is the prefix, if any?
+ #[inline]
+ fn prefix_len(&self) -> usize {
+ self.prefix.as_ref().map(Prefix::len).unwrap_or(0)
+ }
+
+ #[inline]
+ fn prefix_verbatim(&self) -> bool {
+ self.prefix.as_ref().map(Prefix::is_verbatim).unwrap_or(false)
+ }
+
+ /// how much of the prefix is left from the point of view of iteration?
+ #[inline]
+ fn prefix_remaining(&self) -> usize {
+ if self.front == State::Prefix {
+ self.prefix_len()
+ } else {
+ 0
+ }
+ }
+
+ // Given the iteration so far, how much of the pre-State::Body path is left?
+ #[inline]
+ fn len_before_body(&self) -> usize {
+ let root = if self.front <= State::StartDir && self.has_physical_root {
+ 1
+ } else {
+ 0
+ };
+ let cur_dir = if self.front <= State::StartDir && self.include_cur_dir() {
+ 1
+ } else {
+ 0
+ };
+ self.prefix_remaining() + root + cur_dir
+ }
+
+ // is the iteration complete?
+ #[inline]
+ fn finished(&self) -> bool {
+ self.front == State::Done || self.back == State::Done || self.front > self.back
+ }
+
+ #[inline]
+ fn is_sep_byte(&self, b: u8) -> bool {
+ if self.prefix_verbatim() {
+ is_verbatim_sep(b)
+ } else {
+ is_sep_byte(b)
+ }
+ }
+
+ /// Extracts a slice corresponding to the portion of the path remaining for iteration.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let mut components = Path::new("/tmp/foo/bar.txt").components();
+ /// components.next();
+ /// components.next();
+ ///
+ /// assert_eq!(Path::new("foo/bar.txt"), components.as_path());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn as_path(&self) -> &'a Path {
+ let mut comps = self.clone();
+ if comps.front == State::Body {
+ comps.trim_left();
+ }
+ if comps.back == State::Body {
+ comps.trim_right();
+ }
+ unsafe { Path::from_u8_slice(comps.path) }
+ }
+
+ /// Is the *original* path rooted?
+ fn has_root(&self) -> bool {
+ if self.has_physical_root {
+ return true;
+ }
+ if let Some(p) = self.prefix {
+ if p.has_implicit_root() {
+ return true;
+ }
+ }
+ false
+ }
+
+ /// Should the normalized path include a leading . ?
+ fn include_cur_dir(&self) -> bool {
+ if self.has_root() {
+ return false;
+ }
+ let mut iter = self.path[self.prefix_len()..].iter();
+ match (iter.next(), iter.next()) {
+ (Some(&b'.'), None) => true,
+ (Some(&b'.'), Some(&b)) => self.is_sep_byte(b),
+ _ => false,
+ }
+ }
+
+ // parse a given byte sequence into the corresponding path component
+ fn parse_single_component<'b>(&self, comp: &'b [u8]) -> Option<Component<'b>> {
+ match comp {
+ b"." if self.prefix_verbatim() => Some(Component::CurDir),
+ b"." => None, // . components are normalized away, except at
+ // the beginning of a path, which is treated
+ // separately via `include_cur_dir`
+ b".." => Some(Component::ParentDir),
+ b"" => None,
+ _ => Some(Component::Normal(unsafe { u8_slice_as_os_str(comp) })),
+ }
+ }
+
+ // parse a component from the left, saying how many bytes to consume to
+ // remove the component
+ fn parse_next_component(&self) -> (usize, Option<Component<'a>>) {
+ debug_assert!(self.front == State::Body);
+ let (extra, comp) = match self.path.iter().position(|b| self.is_sep_byte(*b)) {
+ None => (0, self.path),
+ Some(i) => (1, &self.path[..i]),
+ };
+ (comp.len() + extra, self.parse_single_component(comp))
+ }
+
+ // parse a component from the right, saying how many bytes to consume to
+ // remove the component
+ fn parse_next_component_back(&self) -> (usize, Option<Component<'a>>) {
+ debug_assert!(self.back == State::Body);
+ let start = self.len_before_body();
+ let (extra, comp) = match self.path[start..].iter().rposition(|b| self.is_sep_byte(*b)) {
+ None => (0, &self.path[start..]),
+ Some(i) => (1, &self.path[start + i + 1..]),
+ };
+ (comp.len() + extra, self.parse_single_component(comp))
+ }
+
+ // trim away repeated separators (i.e. empty components) on the left
+ fn trim_left(&mut self) {
+ while !self.path.is_empty() {
+ let (size, comp) = self.parse_next_component();
+ if comp.is_some() {
+ return;
+ } else {
+ self.path = &self.path[size..];
+ }
+ }
+ }
+
+ // trim away repeated separators (i.e. empty components) on the right
+ fn trim_right(&mut self) {
+ while self.path.len() > self.len_before_body() {
+ let (size, comp) = self.parse_next_component_back();
+ if comp.is_some() {
+ return;
+ } else {
+ self.path = &self.path[..self.path.len() - size];
+ }
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> AsRef<Path> for Components<'a> {
+ fn as_ref(&self) -> &Path {
+ self.as_path()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> AsRef<OsStr> for Components<'a> {
+ fn as_ref(&self) -> &OsStr {
+ self.as_path().as_os_str()
+ }
+}
+
+#[stable(feature = "path_iter_debug", since = "1.13.0")]
+impl<'a> fmt::Debug for Iter<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ struct DebugHelper<'a>(&'a Path);
+
+ impl<'a> fmt::Debug for DebugHelper<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.debug_list()
+ .entries(self.0.iter())
+ .finish()
+ }
+ }
+
+ f.debug_tuple("Iter")
+ .field(&DebugHelper(self.as_path()))
+ .finish()
+ }
+}
+
+impl<'a> Iter<'a> {
+ /// Extracts a slice corresponding to the portion of the path remaining for iteration.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn as_path(&self) -> &'a Path {
+ self.inner.as_path()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> AsRef<Path> for Iter<'a> {
+ fn as_ref(&self) -> &Path {
+ self.as_path()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> AsRef<OsStr> for Iter<'a> {
+ fn as_ref(&self) -> &OsStr {
+ self.as_path().as_os_str()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> Iterator for Iter<'a> {
+ type Item = &'a OsStr;
+
+ fn next(&mut self) -> Option<&'a OsStr> {
+ self.inner.next().map(Component::as_os_str)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> DoubleEndedIterator for Iter<'a> {
+ fn next_back(&mut self) -> Option<&'a OsStr> {
+ self.inner.next_back().map(Component::as_os_str)
+ }
+}
+
+#[unstable(feature = "fused", issue = "35602")]
+impl<'a> FusedIterator for Iter<'a> {}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> Iterator for Components<'a> {
+ type Item = Component<'a>;
+
+ fn next(&mut self) -> Option<Component<'a>> {
+ while !self.finished() {
+ match self.front {
+ State::Prefix if self.prefix_len() > 0 => {
+ self.front = State::StartDir;
+ debug_assert!(self.prefix_len() <= self.path.len());
+ let raw = &self.path[..self.prefix_len()];
+ self.path = &self.path[self.prefix_len()..];
+ return Some(Component::Prefix(PrefixComponent {
+ raw: unsafe { u8_slice_as_os_str(raw) },
+ parsed: self.prefix.unwrap(),
+ }));
+ }
+ State::Prefix => {
+ self.front = State::StartDir;
+ }
+ State::StartDir => {
+ self.front = State::Body;
+ if self.has_physical_root {
+ debug_assert!(!self.path.is_empty());
+ self.path = &self.path[1..];
+ return Some(Component::RootDir);
+ } else if let Some(p) = self.prefix {
+ if p.has_implicit_root() && !p.is_verbatim() {
+ return Some(Component::RootDir);
+ }
+ } else if self.include_cur_dir() {
+ debug_assert!(!self.path.is_empty());
+ self.path = &self.path[1..];
+ return Some(Component::CurDir);
+ }
+ }
+ State::Body if !self.path.is_empty() => {
+ let (size, comp) = self.parse_next_component();
+ self.path = &self.path[size..];
+ if comp.is_some() {
+ return comp;
+ }
+ }
+ State::Body => {
+ self.front = State::Done;
+ }
+ State::Done => unreachable!(),
+ }
+ }
+ None
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> DoubleEndedIterator for Components<'a> {
+ fn next_back(&mut self) -> Option<Component<'a>> {
+ while !self.finished() {
+ match self.back {
+ State::Body if self.path.len() > self.len_before_body() => {
+ let (size, comp) = self.parse_next_component_back();
+ self.path = &self.path[..self.path.len() - size];
+ if comp.is_some() {
+ return comp;
+ }
+ }
+ State::Body => {
+ self.back = State::StartDir;
+ }
+ State::StartDir => {
+ self.back = State::Prefix;
+ if self.has_physical_root {
+ self.path = &self.path[..self.path.len() - 1];
+ return Some(Component::RootDir);
+ } else if let Some(p) = self.prefix {
+ if p.has_implicit_root() && !p.is_verbatim() {
+ return Some(Component::RootDir);
+ }
+ } else if self.include_cur_dir() {
+ self.path = &self.path[..self.path.len() - 1];
+ return Some(Component::CurDir);
+ }
+ }
+ State::Prefix if self.prefix_len() > 0 => {
+ self.back = State::Done;
+ return Some(Component::Prefix(PrefixComponent {
+ raw: unsafe { u8_slice_as_os_str(self.path) },
+ parsed: self.prefix.unwrap(),
+ }));
+ }
+ State::Prefix => {
+ self.back = State::Done;
+ return None;
+ }
+ State::Done => unreachable!(),
+ }
+ }
+ None
+ }
+}
+
+#[unstable(feature = "fused", issue = "35602")]
+impl<'a> FusedIterator for Components<'a> {}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> cmp::PartialEq for Components<'a> {
+ fn eq(&self, other: &Components<'a>) -> bool {
+ Iterator::eq(self.clone(), other.clone())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> cmp::Eq for Components<'a> {}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> cmp::PartialOrd for Components<'a> {
+ fn partial_cmp(&self, other: &Components<'a>) -> Option<cmp::Ordering> {
+ Iterator::partial_cmp(self.clone(), other.clone())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> cmp::Ord for Components<'a> {
+ fn cmp(&self, other: &Components<'a>) -> cmp::Ordering {
+ Iterator::cmp(self.clone(), other.clone())
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// Basic types and traits
+////////////////////////////////////////////////////////////////////////////////
+
+/// An owned, mutable path (akin to [`String`]).
+///
+/// This type provides methods like [`push`] and [`set_extension`] that mutate
+/// the path in place. It also implements [`Deref`] to [`Path`], meaning that
+/// all methods on [`Path`] slices are available on `PathBuf` values as well.
+///
+/// [`String`]: ../string/struct.String.html
+/// [`Path`]: struct.Path.html
+/// [`push`]: struct.PathBuf.html#method.push
+/// [`set_extension`]: struct.PathBuf.html#method.set_extension
+/// [`Deref`]: ../ops/trait.Deref.html
+///
+/// More details about the overall approach can be found in
+/// the module documentation.
+///
+/// # Examples
+///
+/// ```
+/// use std::path::PathBuf;
+///
+/// let mut path = PathBuf::from("c:\\");
+/// path.push("windows");
+/// path.push("system32");
+/// path.set_extension("dll");
+/// ```
+#[derive(Clone)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct PathBuf {
+ inner: OsString,
+}
+
+impl PathBuf {
+ fn as_mut_vec(&mut self) -> &mut Vec<u8> {
+ unsafe { &mut *(self as *mut PathBuf as *mut Vec<u8>) }
+ }
+
+ /// Allocates an empty `PathBuf`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::PathBuf;
+ ///
+ /// let path = PathBuf::new();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new() -> PathBuf {
+ PathBuf { inner: OsString::new() }
+ }
+
+ /// Coerces to a [`Path`] slice.
+ ///
+ /// [`Path`]: struct.Path.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::{Path, PathBuf};
+ ///
+ /// let p = PathBuf::from("/test");
+ /// assert_eq!(Path::new("/test"), p.as_path());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn as_path(&self) -> &Path {
+ self
+ }
+
+ /// Extends `self` with `path`.
+ ///
+ /// If `path` is absolute, it replaces the current path.
+ ///
+ /// On Windows:
+ ///
+ /// * if `path` has a root but no prefix (e.g. `\windows`), it
+ /// replaces everything except for the prefix (if any) of `self`.
+ /// * if `path` has a prefix but no root, it replaces `self`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::PathBuf;
+ ///
+ /// let mut path = PathBuf::new();
+ /// path.push("/tmp");
+ /// path.push("file.bk");
+ /// assert_eq!(path, PathBuf::from("/tmp/file.bk"));
+ ///
+ /// // Pushing an absolute path replaces the current path
+ /// path.push("/etc/passwd");
+ /// assert_eq!(path, PathBuf::from("/etc/passwd"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn push<P: AsRef<Path>>(&mut self, path: P) {
+ self._push(path.as_ref())
+ }
+
+ fn _push(&mut self, path: &Path) {
+ // in general, a separator is needed if the rightmost byte is not a separator
+ let mut need_sep = self.as_mut_vec().last().map(|c| !is_sep_byte(*c)).unwrap_or(false);
+
+ // in the special case of `C:` on Windows, do *not* add a separator
+ {
+ let comps = self.components();
+ if comps.prefix_len() > 0 && comps.prefix_len() == comps.path.len() &&
+ comps.prefix.unwrap().is_drive() {
+ need_sep = false
+ }
+ }
+
+ // absolute `path` replaces `self`
+ if path.is_absolute() || path.prefix().is_some() {
+ self.as_mut_vec().truncate(0);
+
+ // `path` has a root but no prefix, e.g. `\windows` (Windows only)
+ } else if path.has_root() {
+ let prefix_len = self.components().prefix_remaining();
+ self.as_mut_vec().truncate(prefix_len);
+
+ // `path` is a pure relative path
+ } else if need_sep {
+ self.inner.push(MAIN_SEP_STR);
+ }
+
+ self.inner.push(path);
+ }
+
+ /// Truncate `self` to [`self.parent()`].
+ ///
+ /// Returns false and does nothing if [`self.file_name()`] is `None`.
+ /// Otherwise, returns `true`.
+ ///
+ /// [`self.parent()`]: struct.PathBuf.html#method.parent
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::{Path, PathBuf};
+ ///
+ /// let mut p = PathBuf::from("/test/test.rs");
+ ///
+ /// p.pop();
+ /// assert_eq!(Path::new("/test"), p);
+ /// p.pop();
+ /// assert_eq!(Path::new("/"), p);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn pop(&mut self) -> bool {
+ match self.parent().map(|p| p.as_u8_slice().len()) {
+ Some(len) => {
+ self.as_mut_vec().truncate(len);
+ true
+ }
+ None => false,
+ }
+ }
+
+ /// Updates [`self.file_name()`] to `file_name`.
+ ///
+ /// If [`self.file_name()`] was `None`, this is equivalent to pushing
+ /// `file_name`.
+ ///
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::PathBuf;
+ ///
+ /// let mut buf = PathBuf::from("/");
+ /// assert!(buf.file_name() == None);
+ /// buf.set_file_name("bar");
+ /// assert!(buf == PathBuf::from("/bar"));
+ /// assert!(buf.file_name().is_some());
+ /// buf.set_file_name("baz.txt");
+ /// assert!(buf == PathBuf::from("/baz.txt"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
+ self._set_file_name(file_name.as_ref())
+ }
+
+ fn _set_file_name(&mut self, file_name: &OsStr) {
+ if self.file_name().is_some() {
+ let popped = self.pop();
+ debug_assert!(popped);
+ }
+ self.push(file_name);
+ }
+
+ /// Updates [`self.extension()`] to `extension`.
+ ///
+ /// If [`self.file_name()`] is `None`, does nothing and returns `false`.
+ ///
+ /// Otherwise, returns `true`; if [`self.extension()`] is `None`, the
+ /// extension is added; otherwise it is replaced.
+ ///
+ /// [`self.file_name()`]: struct.PathBuf.html#method.file_name
+ /// [`self.extension()`]: struct.PathBuf.html#method.extension
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::{Path, PathBuf};
+ ///
+ /// let mut p = PathBuf::from("/feel/the");
+ ///
+ /// p.set_extension("force");
+ /// assert_eq!(Path::new("/feel/the.force"), p.as_path());
+ ///
+ /// p.set_extension("dark_side");
+ /// assert_eq!(Path::new("/feel/the.dark_side"), p.as_path());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
+ self._set_extension(extension.as_ref())
+ }
+
+ fn _set_extension(&mut self, extension: &OsStr) -> bool {
+ if self.file_name().is_none() {
+ return false;
+ }
+
+ let mut stem = match self.file_stem() {
+ Some(stem) => stem.to_os_string(),
+ None => OsString::new(),
+ };
+
+ if !os_str_as_u8_slice(extension).is_empty() {
+ stem.push(".");
+ stem.push(extension);
+ }
+ self.set_file_name(&stem);
+
+ true
+ }
+
+ /// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
+ ///
+ /// [`OsString`]: ../ffi/struct.OsString.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::PathBuf;
+ ///
+ /// let p = PathBuf::from("/the/head");
+ /// let os_str = p.into_os_string();
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn into_os_string(self) -> OsString {
+ self.inner
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, T: ?Sized + AsRef<OsStr>> From<&'a T> for PathBuf {
+ fn from(s: &'a T) -> PathBuf {
+ PathBuf::from(s.as_ref().to_os_string())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl From<OsString> for PathBuf {
+ fn from(s: OsString) -> PathBuf {
+ PathBuf { inner: s }
+ }
+}
+
+#[stable(feature = "from_path_buf_for_os_string", since = "1.14.0")]
+impl From<PathBuf> for OsString {
+ fn from(path_buf : PathBuf) -> OsString {
+ path_buf.inner
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl From<String> for PathBuf {
+ fn from(s: String) -> PathBuf {
+ PathBuf::from(OsString::from(s))
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<P: AsRef<Path>> iter::FromIterator<P> for PathBuf {
+ fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf {
+ let mut buf = PathBuf::new();
+ buf.extend(iter);
+ buf
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<P: AsRef<Path>> iter::Extend<P> for PathBuf {
+ fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
+ for p in iter {
+ self.push(p.as_ref())
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl fmt::Debug for PathBuf {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ fmt::Debug::fmt(&**self, formatter)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl ops::Deref for PathBuf {
+ type Target = Path;
+
+ fn deref(&self) -> &Path {
+ Path::new(&self.inner)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Borrow<Path> for PathBuf {
+ fn borrow(&self) -> &Path {
+ self.deref()
+ }
+}
+
+#[stable(feature = "cow_from_path", since = "1.6.0")]
+impl<'a> From<&'a Path> for Cow<'a, Path> {
+ #[inline]
+ fn from(s: &'a Path) -> Cow<'a, Path> {
+ Cow::Borrowed(s)
+ }
+}
+
+#[stable(feature = "cow_from_path", since = "1.6.0")]
+impl<'a> From<PathBuf> for Cow<'a, Path> {
+ #[inline]
+ fn from(s: PathBuf) -> Cow<'a, Path> {
+ Cow::Owned(s)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl ToOwned for Path {
+ type Owned = PathBuf;
+ fn to_owned(&self) -> PathBuf {
+ self.to_path_buf()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl cmp::PartialEq for PathBuf {
+ fn eq(&self, other: &PathBuf) -> bool {
+ self.components() == other.components()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Hash for PathBuf {
+ fn hash<H: Hasher>(&self, h: &mut H) {
+ self.as_path().hash(h)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl cmp::Eq for PathBuf {}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl cmp::PartialOrd for PathBuf {
+ fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
+ self.components().partial_cmp(other.components())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl cmp::Ord for PathBuf {
+ fn cmp(&self, other: &PathBuf) -> cmp::Ordering {
+ self.components().cmp(other.components())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<OsStr> for PathBuf {
+ fn as_ref(&self) -> &OsStr {
+ &self.inner[..]
+ }
+}
+
+/// A slice of a path (akin to [`str`]).
+///
+/// This type supports a number of operations for inspecting a path, including
+/// breaking the path into its components (separated by `/` or `\`, depending on
+/// the platform), extracting the file name, determining whether the path is
+/// absolute, and so on.
+///
+/// This is an *unsized* type, meaning that it must always be used behind a
+/// pointer like `&` or [`Box`]. For an owned version of this type,
+/// see [`PathBuf`].
+///
+/// [`str`]: ../primitive.str.html
+/// [`Box`]: ../boxed/struct.Box.html
+/// [`PathBuf`]: struct.PathBuf.html
+///
+/// More details about the overall approach can be found in
+/// the module documentation.
+///
+/// # Examples
+///
+/// ```
+/// use std::path::Path;
+///
+/// let path = Path::new("/tmp/foo/bar.txt");
+/// let file = path.file_name();
+/// let extension = path.extension();
+/// let parent_dir = path.parent();
+/// ```
+///
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Path {
+ inner: OsStr,
+}
+
+/// An error returned from the `Path::strip_prefix` method indicating that the
+/// prefix was not found in `self`.
+#[derive(Debug, Clone, PartialEq, Eq)]
+#[stable(since = "1.7.0", feature = "strip_prefix")]
+pub struct StripPrefixError(());
+
+impl Path {
+ // The following (private!) function allows construction of a path from a u8
+ // slice, which is only safe when it is known to follow the OsStr encoding.
+ unsafe fn from_u8_slice(s: &[u8]) -> &Path {
+ Path::new(u8_slice_as_os_str(s))
+ }
+ // The following (private!) function reveals the byte encoding used for OsStr.
+ fn as_u8_slice(&self) -> &[u8] {
+ os_str_as_u8_slice(&self.inner)
+ }
+
+ /// Directly wrap a string slice as a `Path` slice.
+ ///
+ /// This is a cost-free conversion.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// Path::new("foo.txt");
+ /// ```
+ ///
+ /// You can create `Path`s from `String`s, or even other `Path`s:
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let string = String::from("foo.txt");
+ /// let from_string = Path::new(&string);
+ /// let from_path = Path::new(&from_string);
+ /// assert_eq!(from_string, from_path);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
+ unsafe { mem::transmute(s.as_ref()) }
+ }
+
+ /// Yields the underlying [`OsStr`] slice.
+ ///
+ /// [`OsStr`]: ../ffi/struct.OsStr.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let os_str = Path::new("foo.txt").as_os_str();
+ /// assert_eq!(os_str, std::ffi::OsStr::new("foo.txt"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn as_os_str(&self) -> &OsStr {
+ &self.inner
+ }
+
+ /// Yields a [`&str`] slice if the `Path` is valid unicode.
+ ///
+ /// This conversion may entail doing a check for UTF-8 validity.
+ ///
+ /// [`&str`]: ../primitive.str.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let path_str = Path::new("foo.txt").to_str();
+ /// assert_eq!(path_str, Some("foo.txt"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn to_str(&self) -> Option<&str> {
+ self.inner.to_str()
+ }
+
+ /// Converts a `Path` to a [`Cow<str>`].
+ ///
+ /// Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
+ ///
+ /// [`Cow<str>`]: ../borrow/enum.Cow.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let path_str = Path::new("foo.txt").to_string_lossy();
+ /// assert_eq!(path_str, "foo.txt");
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn to_string_lossy(&self) -> Cow<str> {
+ self.inner.to_string_lossy()
+ }
+
+ /// Converts a `Path` to an owned [`PathBuf`].
+ ///
+ /// [`PathBuf`]: struct.PathBuf.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let path_buf = Path::new("foo.txt").to_path_buf();
+ /// assert_eq!(path_buf, std::path::PathBuf::from("foo.txt"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn to_path_buf(&self) -> PathBuf {
+ PathBuf::from(self.inner.to_os_string())
+ }
+
+ /// A path is *absolute* if it is independent of the current directory.
+ ///
+ /// * On Unix, a path is absolute if it starts with the root, so
+ /// `is_absolute` and `has_root` are equivalent.
+ ///
+ /// * On Windows, a path is absolute if it has a prefix and starts with the
+ /// root: `c:\windows` is absolute, while `c:temp` and `\temp` are not.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// assert!(!Path::new("foo.txt").is_absolute());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[allow(deprecated)]
+ pub fn is_absolute(&self) -> bool {
+ self.has_root() && (cfg!(unix) || self.prefix().is_some())
+ }
+
+ /// A path is *relative* if it is not absolute.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// assert!(Path::new("foo.txt").is_relative());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn is_relative(&self) -> bool {
+ !self.is_absolute()
+ }
+
+ fn prefix(&self) -> Option<Prefix> {
+ self.components().prefix
+ }
+
+ /// A path has a root if the body of the path begins with the directory separator.
+ ///
+ /// * On Unix, a path has a root if it begins with `/`.
+ ///
+ /// * On Windows, a path has a root if it:
+ /// * has no prefix and begins with a separator, e.g. `\\windows`
+ /// * has a prefix followed by a separator, e.g. `c:\windows` but not `c:windows`
+ /// * has any non-disk prefix, e.g. `\\server\share`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// assert!(Path::new("/etc/passwd").has_root());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn has_root(&self) -> bool {
+ self.components().has_root()
+ }
+
+ /// The path without its final component, if any.
+ ///
+ /// Returns `None` if the path terminates in a root or prefix.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let path = Path::new("/foo/bar");
+ /// let parent = path.parent().unwrap();
+ /// assert_eq!(parent, Path::new("/foo"));
+ ///
+ /// let grand_parent = parent.parent().unwrap();
+ /// assert_eq!(grand_parent, Path::new("/"));
+ /// assert_eq!(grand_parent.parent(), None);
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn parent(&self) -> Option<&Path> {
+ let mut comps = self.components();
+ let comp = comps.next_back();
+ comp.and_then(|p| {
+ match p {
+ Component::Normal(_) |
+ Component::CurDir |
+ Component::ParentDir => Some(comps.as_path()),
+ _ => None,
+ }
+ })
+ }
+
+ /// The final component of the path, if it is a normal file.
+ ///
+ /// If the path terminates in `..`, `file_name` will return `None`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ /// use std::ffi::OsStr;
+ ///
+ /// let path = Path::new("foo.txt");
+ /// let os_str = OsStr::new("foo.txt");
+ ///
+ /// assert_eq!(Some(os_str), path.file_name());
+ /// ```
+ ///
+ /// # Other examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ /// use std::ffi::OsStr;
+ ///
+ /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.").file_name());
+ /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.//").file_name());
+ /// assert_eq!(None, Path::new("foo.txt/..").file_name());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn file_name(&self) -> Option<&OsStr> {
+ self.components().next_back().and_then(|p| {
+ match p {
+ Component::Normal(p) => Some(p.as_ref()),
+ _ => None,
+ }
+ })
+ }
+
+ /// Returns a path that, when joined onto `base`, yields `self`.
+ ///
+ /// # Errors
+ ///
+ /// If `base` is not a prefix of `self` (i.e. `starts_with`
+ /// returns `false`), returns `Err`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let path = Path::new("/test/haha/foo.txt");
+ ///
+ /// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
+ /// assert_eq!(path.strip_prefix("test").is_ok(), false);
+ /// assert_eq!(path.strip_prefix("/haha").is_ok(), false);
+ /// ```
+ #[stable(since = "1.7.0", feature = "path_strip_prefix")]
+ pub fn strip_prefix<'a, P: ?Sized>(&'a self, base: &'a P)
+ -> Result<&'a Path, StripPrefixError>
+ where P: AsRef<Path>
+ {
+ self._strip_prefix(base.as_ref())
+ }
+
+ fn _strip_prefix<'a>(&'a self, base: &'a Path)
+ -> Result<&'a Path, StripPrefixError> {
+ iter_after(self.components(), base.components())
+ .map(|c| c.as_path())
+ .ok_or(StripPrefixError(()))
+ }
+
+ /// Determines whether `base` is a prefix of `self`.
+ ///
+ /// Only considers whole path components to match.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let path = Path::new("/etc/passwd");
+ ///
+ /// assert!(path.starts_with("/etc"));
+ ///
+ /// assert!(!path.starts_with("/e"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool {
+ self._starts_with(base.as_ref())
+ }
+
+ fn _starts_with(&self, base: &Path) -> bool {
+ iter_after(self.components(), base.components()).is_some()
+ }
+
+ /// Determines whether `child` is a suffix of `self`.
+ ///
+ /// Only considers whole path components to match.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let path = Path::new("/etc/passwd");
+ ///
+ /// assert!(path.ends_with("passwd"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
+ self._ends_with(child.as_ref())
+ }
+
+ fn _ends_with(&self, child: &Path) -> bool {
+ iter_after(self.components().rev(), child.components().rev()).is_some()
+ }
+
+ /// Extracts the stem (non-extension) portion of [`self.file_name()`].
+ ///
+ /// [`self.file_name()`]: struct.Path.html#method.file_name
+ ///
+ /// The stem is:
+ ///
+ /// * None, if there is no file name;
+ /// * The entire file name if there is no embedded `.`;
+ /// * The entire file name if the file name begins with `.` and has no other `.`s within;
+ /// * Otherwise, the portion of the file name before the final `.`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let path = Path::new("foo.rs");
+ ///
+ /// assert_eq!("foo", path.file_stem().unwrap());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn file_stem(&self) -> Option<&OsStr> {
+ self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.or(after))
+ }
+
+ /// Extracts the extension of [`self.file_name()`], if possible.
+ ///
+ /// [`self.file_name()`]: struct.Path.html#method.file_name
+ ///
+ /// The extension is:
+ ///
+ /// * None, if there is no file name;
+ /// * None, if there is no embedded `.`;
+ /// * None, if the file name begins with `.` and has no other `.`s within;
+ /// * Otherwise, the portion of the file name after the final `.`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let path = Path::new("foo.rs");
+ ///
+ /// assert_eq!("rs", path.extension().unwrap());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn extension(&self) -> Option<&OsStr> {
+ self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.and(after))
+ }
+
+ /// Creates an owned [`PathBuf`] with `path` adjoined to `self`.
+ ///
+ /// See [`PathBuf::push`] for more details on what it means to adjoin a path.
+ ///
+ /// [`PathBuf`]: struct.PathBuf.html
+ /// [`PathBuf::push`]: struct.PathBuf.html#method.push
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::{Path, PathBuf};
+ ///
+ /// assert_eq!(Path::new("/etc").join("passwd"), PathBuf::from("/etc/passwd"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
+ self._join(path.as_ref())
+ }
+
+ fn _join(&self, path: &Path) -> PathBuf {
+ let mut buf = self.to_path_buf();
+ buf.push(path);
+ buf
+ }
+
+ /// Creates an owned [`PathBuf`] like `self` but with the given file name.
+ ///
+ /// See [`PathBuf::set_file_name`] for more details.
+ ///
+ /// [`PathBuf`]: struct.PathBuf.html
+ /// [`PathBuf::set_file_name`]: struct.PathBuf.html#method.set_file_name
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::{Path, PathBuf};
+ ///
+ /// let path = Path::new("/tmp/foo.txt");
+ /// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
+ self._with_file_name(file_name.as_ref())
+ }
+
+ fn _with_file_name(&self, file_name: &OsStr) -> PathBuf {
+ let mut buf = self.to_path_buf();
+ buf.set_file_name(file_name);
+ buf
+ }
+
+ /// Creates an owned [`PathBuf`] like `self` but with the given extension.
+ ///
+ /// See [`PathBuf::set_extension`] for more details.
+ ///
+ /// [`PathBuf`]: struct.PathBuf.html
+ /// [`PathBuf::set_extension`]: struct.PathBuf.html#method.set_extension
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::{Path, PathBuf};
+ ///
+ /// let path = Path::new("foo.rs");
+ /// assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt"));
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
+ self._with_extension(extension.as_ref())
+ }
+
+ fn _with_extension(&self, extension: &OsStr) -> PathBuf {
+ let mut buf = self.to_path_buf();
+ buf.set_extension(extension);
+ buf
+ }
+
+ /// Produce an iterator over the components of the path.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::{Path, Component};
+ /// use std::ffi::OsStr;
+ ///
+ /// let mut components = Path::new("/tmp/foo.txt").components();
+ ///
+ /// assert_eq!(components.next(), Some(Component::RootDir));
+ /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("tmp"))));
+ /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt"))));
+ /// assert_eq!(components.next(), None)
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn components(&self) -> Components {
+ let prefix = parse_prefix(self.as_os_str());
+ Components {
+ path: self.as_u8_slice(),
+ prefix: prefix,
+ has_physical_root: has_physical_root(self.as_u8_slice(), prefix),
+ front: State::Prefix,
+ back: State::Body,
+ }
+ }
+
+ /// Produce an iterator over the path's components viewed as [`OsStr`] slices.
+ ///
+ /// [`OsStr`]: ../ffi/struct.OsStr.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::{self, Path};
+ /// use std::ffi::OsStr;
+ ///
+ /// let mut it = Path::new("/tmp/foo.txt").iter();
+ /// assert_eq!(it.next(), Some(OsStr::new(&path::MAIN_SEPARATOR.to_string())));
+ /// assert_eq!(it.next(), Some(OsStr::new("tmp")));
+ /// assert_eq!(it.next(), Some(OsStr::new("foo.txt")));
+ /// assert_eq!(it.next(), None)
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn iter(&self) -> Iter {
+ Iter { inner: self.components() }
+ }
+
+ /// Returns an object that implements [`Display`] for safely printing paths
+ /// that may contain non-Unicode data.
+ ///
+ /// [`Display`]: ../fmt/trait.Display.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::path::Path;
+ ///
+ /// let path = Path::new("/tmp/foo.rs");
+ ///
+ /// println!("{}", path.display());
+ /// ```
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn display(&self) -> Display {
+ Display { path: self }
+ }
+
+
+ /// Query the file system to get information about a file, directory, etc.
+ ///
+ /// This function will traverse symbolic links to query information about the
+ /// destination file.
+ ///
+ /// This is an alias to [`fs::metadata`].
+ ///
+ /// [`fs::metadata`]: ../fs/fn.metadata.html
+ #[cfg(feature = "fs_not_implemented")]
+ pub fn metadata(&self) -> io::Result<fs::Metadata> {
+ fs::metadata(self)
+ }
+
+ /// Query the metadata about a file without following symlinks.
+ ///
+ /// This is an alias to [`fs::symlink_metadata`].
+ ///
+ /// [`fs::symlink_metadata`]: ../fs/fn.symlink_metadata.html
+ #[cfg(feature = "fs_not_implemented")]
+ pub fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
+ fs::symlink_metadata(self)
+ }
+
+ /// Returns the canonical form of the path with all intermediate components
+ /// normalized and symbolic links resolved.
+ ///
+ /// This is an alias to [`fs::canonicalize`].
+ ///
+ /// [`fs::canonicalize`]: ../fs/fn.canonicalize.html
+ #[cfg(feature = "fs_not_implemented")]
+ pub fn canonicalize(&self) -> io::Result<PathBuf> {
+ fs::canonicalize(self)
+ }
+
+ /// Reads a symbolic link, returning the file that the link points to.
+ ///
+ /// This is an alias to [`fs::read_link`].
+ ///
+ /// [`fs::read_link`]: ../fs/fn.read_link.html
+ #[cfg(feature = "fs_not_implemented")]
+ pub fn read_link(&self) -> io::Result<PathBuf> {
+ fs::read_link(self)
+ }
+
+ /// Returns an iterator over the entries within a directory.
+ ///
+ /// The iterator will yield instances of [`io::Result`]`<`[`DirEntry`]`>`. New
+ /// errors may be encountered after an iterator is initially constructed.
+ ///
+ /// This is an alias to [`fs::read_dir`].
+ ///
+ /// [`io::Result`]: ../io/type.Result.html
+ /// [`DirEntry`]: ../fs/struct.DirEntry.html
+ /// [`fs::read_dir`]: ../fs/fn.read_dir.html
+ #[cfg(feature = "fs_not_implemented")]
+ pub fn read_dir(&self) -> io::Result<fs::ReadDir> {
+ fs::read_dir(self)
+ }
+
+ /// Returns whether the path points at an existing entity.
+ ///
+ /// This function will traverse symbolic links to query information about the
+ /// destination file. In case of broken symbolic links this will return `false`.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::path::Path;
+ /// assert_eq!(Path::new("does_not_exist.txt").exists(), false);
+ /// ```
+ #[cfg(feature = "fs_not_implemented")]
+ pub fn exists(&self) -> bool {
+ fs::metadata(self).is_ok()
+ }
+
+ /// Returns whether the path is pointing at a regular file.
+ ///
+ /// This function will traverse symbolic links to query information about the
+ /// destination file. In case of broken symbolic links this will return `false`.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::path::Path;
+ /// assert_eq!(Path::new("./is_a_directory/").is_file(), false);
+ /// assert_eq!(Path::new("a_file.txt").is_file(), true);
+ /// ```
+ #[cfg(feature = "fs_not_implemented")]
+ pub fn is_file(&self) -> bool {
+ fs::metadata(self).map(|m| m.is_file()).unwrap_or(false)
+ }
+
+ /// Returns whether the path is pointing at a directory.
+ ///
+ /// This function will traverse symbolic links to query information about the
+ /// destination file. In case of broken symbolic links this will return `false`.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use std::path::Path;
+ /// assert_eq!(Path::new("./is_a_directory/").is_dir(), true);
+ /// assert_eq!(Path::new("a_file.txt").is_dir(), false);
+ /// ```
+ #[cfg(feature = "fs_not_implemented")]
+ pub fn is_dir(&self) -> bool {
+ fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<OsStr> for Path {
+ fn as_ref(&self) -> &OsStr {
+ &self.inner
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl fmt::Debug for Path {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ self.inner.fmt(formatter)
+ }
+}
+
+/// Helper struct for safely printing paths with `format!()` and `{}`
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Display<'a> {
+ path: &'a Path,
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> fmt::Debug for Display<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Debug::fmt(&self.path.to_string_lossy(), f)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> fmt::Display for Display<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(&self.path.to_string_lossy(), f)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl cmp::PartialEq for Path {
+ fn eq(&self, other: &Path) -> bool {
+ self.components().eq(other.components())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Hash for Path {
+ fn hash<H: Hasher>(&self, h: &mut H) {
+ for component in self.components() {
+ component.hash(h);
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl cmp::Eq for Path {}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl cmp::PartialOrd for Path {
+ fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
+ self.components().partial_cmp(other.components())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl cmp::Ord for Path {
+ fn cmp(&self, other: &Path) -> cmp::Ordering {
+ self.components().cmp(other.components())
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<Path> for Path {
+ fn as_ref(&self) -> &Path {
+ self
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<Path> for OsStr {
+ fn as_ref(&self) -> &Path {
+ Path::new(self)
+ }
+}
+
+#[stable(feature = "cow_os_str_as_ref_path", since = "1.8.0")]
+impl<'a> AsRef<Path> for Cow<'a, OsStr> {
+ fn as_ref(&self) -> &Path {
+ Path::new(self)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<Path> for OsString {
+ fn as_ref(&self) -> &Path {
+ Path::new(self)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<Path> for str {
+ fn as_ref(&self) -> &Path {
+ Path::new(self)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<Path> for String {
+ fn as_ref(&self) -> &Path {
+ Path::new(self)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl AsRef<Path> for PathBuf {
+ fn as_ref(&self) -> &Path {
+ self
+ }
+}
+
+#[stable(feature = "path_into_iter", since = "1.6.0")]
+impl<'a> IntoIterator for &'a PathBuf {
+ type Item = &'a OsStr;
+ type IntoIter = Iter<'a>;
+ fn into_iter(self) -> Iter<'a> { self.iter() }
+}
+
+#[stable(feature = "path_into_iter", since = "1.6.0")]
+impl<'a> IntoIterator for &'a Path {
+ type Item = &'a OsStr;
+ type IntoIter = Iter<'a>;
+ fn into_iter(self) -> Iter<'a> { self.iter() }
+}
+
+macro_rules! impl_cmp {
+ ($lhs:ty, $rhs: ty) => {
+ #[stable(feature = "partialeq_path", since = "1.6.0")]
+ impl<'a, 'b> PartialEq<$rhs> for $lhs {
+ #[inline]
+ fn eq(&self, other: &$rhs) -> bool { <Path as PartialEq>::eq(self, other) }
+ }
+
+ #[stable(feature = "partialeq_path", since = "1.6.0")]
+ impl<'a, 'b> PartialEq<$lhs> for $rhs {
+ #[inline]
+ fn eq(&self, other: &$lhs) -> bool { <Path as PartialEq>::eq(self, other) }
+ }
+
+ #[stable(feature = "cmp_path", since = "1.8.0")]
+ impl<'a, 'b> PartialOrd<$rhs> for $lhs {
+ #[inline]
+ fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
+ <Path as PartialOrd>::partial_cmp(self, other)
+ }
+ }
+
+ #[stable(feature = "cmp_path", since = "1.8.0")]
+ impl<'a, 'b> PartialOrd<$lhs> for $rhs {
+ #[inline]
+ fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
+ <Path as PartialOrd>::partial_cmp(self, other)
+ }
+ }
+ }
+}
+
+impl_cmp!(PathBuf, Path);
+impl_cmp!(PathBuf, &'a Path);
+impl_cmp!(Cow<'a, Path>, Path);
+impl_cmp!(Cow<'a, Path>, &'b Path);
+impl_cmp!(Cow<'a, Path>, PathBuf);
+
+macro_rules! impl_cmp_os_str {
+ ($lhs:ty, $rhs: ty) => {
+ #[stable(feature = "cmp_path", since = "1.8.0")]
+ impl<'a, 'b> PartialEq<$rhs> for $lhs {
+ #[inline]
+ fn eq(&self, other: &$rhs) -> bool { <Path as PartialEq>::eq(self, other.as_ref()) }
+ }
+
+ #[stable(feature = "cmp_path", since = "1.8.0")]
+ impl<'a, 'b> PartialEq<$lhs> for $rhs {
+ #[inline]
+ fn eq(&self, other: &$lhs) -> bool { <Path as PartialEq>::eq(self.as_ref(), other) }
+ }
+
+ #[stable(feature = "cmp_path", since = "1.8.0")]
+ impl<'a, 'b> PartialOrd<$rhs> for $lhs {
+ #[inline]
+ fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
+ <Path as PartialOrd>::partial_cmp(self, other.as_ref())
+ }
+ }
+
+ #[stable(feature = "cmp_path", since = "1.8.0")]
+ impl<'a, 'b> PartialOrd<$lhs> for $rhs {
+ #[inline]
+ fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
+ <Path as PartialOrd>::partial_cmp(self.as_ref(), other)
+ }
+ }
+ }
+}
+
+impl_cmp_os_str!(PathBuf, OsStr);
+impl_cmp_os_str!(PathBuf, &'a OsStr);
+impl_cmp_os_str!(PathBuf, Cow<'a, OsStr>);
+impl_cmp_os_str!(PathBuf, OsString);
+impl_cmp_os_str!(Path, OsStr);
+impl_cmp_os_str!(Path, &'a OsStr);
+impl_cmp_os_str!(Path, Cow<'a, OsStr>);
+impl_cmp_os_str!(Path, OsString);
+impl_cmp_os_str!(&'a Path, OsStr);
+impl_cmp_os_str!(&'a Path, Cow<'b, OsStr>);
+impl_cmp_os_str!(&'a Path, OsString);
+impl_cmp_os_str!(Cow<'a, Path>, OsStr);
+impl_cmp_os_str!(Cow<'a, Path>, &'b OsStr);
+impl_cmp_os_str!(Cow<'a, Path>, OsString);
+
+#[stable(since = "1.7.0", feature = "strip_prefix")]
+impl fmt::Display for StripPrefixError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ self.description().fmt(f)
+ }
+}
+
+#[stable(since = "1.7.0", feature = "strip_prefix")]
+impl Error for StripPrefixError {
+ fn description(&self) -> &str { "prefix not found" }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ macro_rules! t(
+ ($path:expr, iter: $iter:expr) => (
+ {
+ let path = Path::new($path);
+
+ // Forward iteration
+ let comps = path.iter()
+ .map(|p| p.to_string_lossy().into_owned())
+ .collect::<Vec<String>>();
+ let exp: &[&str] = &$iter;
+ let exps = exp.iter().map(|s| s.to_string()).collect::<Vec<String>>();
+ assert!(comps == exps, "iter: Expected {:?}, found {:?}",
+ exps, comps);
+
+ // Reverse iteration
+ let comps = Path::new($path).iter().rev()
+ .map(|p| p.to_string_lossy().into_owned())
+ .collect::<Vec<String>>();
+ let exps = exps.into_iter().rev().collect::<Vec<String>>();
+ assert!(comps == exps, "iter().rev(): Expected {:?}, found {:?}",
+ exps, comps);
+ }
+ );
+
+ ($path:expr, has_root: $has_root:expr, is_absolute: $is_absolute:expr) => (
+ {
+ let path = Path::new($path);
+
+ let act_root = path.has_root();
+ assert!(act_root == $has_root, "has_root: Expected {:?}, found {:?}",
+ $has_root, act_root);
+
+ let act_abs = path.is_absolute();
+ assert!(act_abs == $is_absolute, "is_absolute: Expected {:?}, found {:?}",
+ $is_absolute, act_abs);
+ }
+ );
+
+ ($path:expr, parent: $parent:expr, file_name: $file:expr) => (
+ {
+ let path = Path::new($path);
+
+ let parent = path.parent().map(|p| p.to_str().unwrap());
+ let exp_parent: Option<&str> = $parent;
+ assert!(parent == exp_parent, "parent: Expected {:?}, found {:?}",
+ exp_parent, parent);
+
+ let file = path.file_name().map(|p| p.to_str().unwrap());
+ let exp_file: Option<&str> = $file;
+ assert!(file == exp_file, "file_name: Expected {:?}, found {:?}",
+ exp_file, file);
+ }
+ );
+
+ ($path:expr, file_stem: $file_stem:expr, extension: $extension:expr) => (
+ {
+ let path = Path::new($path);
+
+ let stem = path.file_stem().map(|p| p.to_str().unwrap());
+ let exp_stem: Option<&str> = $file_stem;
+ assert!(stem == exp_stem, "file_stem: Expected {:?}, found {:?}",
+ exp_stem, stem);
+
+ let ext = path.extension().map(|p| p.to_str().unwrap());
+ let exp_ext: Option<&str> = $extension;
+ assert!(ext == exp_ext, "extension: Expected {:?}, found {:?}",
+ exp_ext, ext);
+ }
+ );
+
+ ($path:expr, iter: $iter:expr,
+ has_root: $has_root:expr, is_absolute: $is_absolute:expr,
+ parent: $parent:expr, file_name: $file:expr,
+ file_stem: $file_stem:expr, extension: $extension:expr) => (
+ {
+ t!($path, iter: $iter);
+ t!($path, has_root: $has_root, is_absolute: $is_absolute);
+ t!($path, parent: $parent, file_name: $file);
+ t!($path, file_stem: $file_stem, extension: $extension);
+ }
+ );
+ );
+
+ #[test]
+ fn into() {
+ use borrow::Cow;
+
+ let static_path = Path::new("/home/foo");
+ let static_cow_path: Cow<'static, Path> = static_path.into();
+ let pathbuf = PathBuf::from("/home/foo");
+
+ {
+ let path: &Path = &pathbuf;
+ let borrowed_cow_path: Cow<Path> = path.into();
+
+ assert_eq!(static_cow_path, borrowed_cow_path);
+ }
+
+ let owned_cow_path: Cow<'static, Path> = pathbuf.into();
+
+ assert_eq!(static_cow_path, owned_cow_path);
+ }
+
+ #[test]
+ #[cfg(unix)]
+ pub fn test_decompositions_unix() {
+ t!("",
+ iter: [],
+ has_root: false,
+ is_absolute: false,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("foo",
+ iter: ["foo"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("/",
+ iter: ["/"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("/foo",
+ iter: ["/", "foo"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("/"),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("foo/",
+ iter: ["foo"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("/foo/",
+ iter: ["/", "foo"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("/"),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("foo/bar",
+ iter: ["foo", "bar"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("foo"),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+ t!("/foo/bar",
+ iter: ["/", "foo", "bar"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("/foo"),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+ t!("///foo///",
+ iter: ["/", "foo"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("/"),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("///foo///bar",
+ iter: ["/", "foo", "bar"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("///foo"),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+ t!("./.",
+ iter: ["."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("/..",
+ iter: ["/", ".."],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("/"),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("../",
+ iter: [".."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("foo/.",
+ iter: ["foo"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("foo/..",
+ iter: ["foo", ".."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("foo"),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("foo/./",
+ iter: ["foo"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("foo/./bar",
+ iter: ["foo", "bar"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("foo"),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+ t!("foo/../",
+ iter: ["foo", ".."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("foo"),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("foo/../bar",
+ iter: ["foo", "..", "bar"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("foo/.."),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+ t!("./a",
+ iter: [".", "a"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("."),
+ file_name: Some("a"),
+ file_stem: Some("a"),
+ extension: None
+ );
+
+ t!(".",
+ iter: ["."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("./",
+ iter: ["."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("a/b",
+ iter: ["a", "b"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("a"),
+ file_name: Some("b"),
+ file_stem: Some("b"),
+ extension: None
+ );
+
+ t!("a//b",
+ iter: ["a", "b"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("a"),
+ file_name: Some("b"),
+ file_stem: Some("b"),
+ extension: None
+ );
+
+ t!("a/./b",
+ iter: ["a", "b"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("a"),
+ file_name: Some("b"),
+ file_stem: Some("b"),
+ extension: None
+ );
+
+ t!("a/b/c",
+ iter: ["a", "b", "c"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("a/b"),
+ file_name: Some("c"),
+ file_stem: Some("c"),
+ extension: None
+ );
+
+ t!(".foo",
+ iter: [".foo"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: Some(".foo"),
+ file_stem: Some(".foo"),
+ extension: None
+ );
+ }
+
+ #[test]
+ #[cfg(windows)]
+ pub fn test_decompositions_windows() {
+ t!("",
+ iter: [],
+ has_root: false,
+ is_absolute: false,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("foo",
+ iter: ["foo"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("/",
+ iter: ["\\"],
+ has_root: true,
+ is_absolute: false,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("\\",
+ iter: ["\\"],
+ has_root: true,
+ is_absolute: false,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("c:",
+ iter: ["c:"],
+ has_root: false,
+ is_absolute: false,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("c:\\",
+ iter: ["c:", "\\"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("c:/",
+ iter: ["c:", "\\"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("/foo",
+ iter: ["\\", "foo"],
+ has_root: true,
+ is_absolute: false,
+ parent: Some("/"),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("foo/",
+ iter: ["foo"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("/foo/",
+ iter: ["\\", "foo"],
+ has_root: true,
+ is_absolute: false,
+ parent: Some("/"),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("foo/bar",
+ iter: ["foo", "bar"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("foo"),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+ t!("/foo/bar",
+ iter: ["\\", "foo", "bar"],
+ has_root: true,
+ is_absolute: false,
+ parent: Some("/foo"),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+ t!("///foo///",
+ iter: ["\\", "foo"],
+ has_root: true,
+ is_absolute: false,
+ parent: Some("/"),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("///foo///bar",
+ iter: ["\\", "foo", "bar"],
+ has_root: true,
+ is_absolute: false,
+ parent: Some("///foo"),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+ t!("./.",
+ iter: ["."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("/..",
+ iter: ["\\", ".."],
+ has_root: true,
+ is_absolute: false,
+ parent: Some("/"),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("../",
+ iter: [".."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("foo/.",
+ iter: ["foo"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("foo/..",
+ iter: ["foo", ".."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("foo"),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("foo/./",
+ iter: ["foo"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: Some("foo"),
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("foo/./bar",
+ iter: ["foo", "bar"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("foo"),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+ t!("foo/../",
+ iter: ["foo", ".."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("foo"),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("foo/../bar",
+ iter: ["foo", "..", "bar"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("foo/.."),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+ t!("./a",
+ iter: [".", "a"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("."),
+ file_name: Some("a"),
+ file_stem: Some("a"),
+ extension: None
+ );
+
+ t!(".",
+ iter: ["."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("./",
+ iter: ["."],
+ has_root: false,
+ is_absolute: false,
+ parent: Some(""),
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("a/b",
+ iter: ["a", "b"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("a"),
+ file_name: Some("b"),
+ file_stem: Some("b"),
+ extension: None
+ );
+
+ t!("a//b",
+ iter: ["a", "b"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("a"),
+ file_name: Some("b"),
+ file_stem: Some("b"),
+ extension: None
+ );
+
+ t!("a/./b",
+ iter: ["a", "b"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("a"),
+ file_name: Some("b"),
+ file_stem: Some("b"),
+ extension: None
+ );
+
+ t!("a/b/c",
+ iter: ["a", "b", "c"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("a/b"),
+ file_name: Some("c"),
+ file_stem: Some("c"),
+ extension: None);
+
+ t!("a\\b\\c",
+ iter: ["a", "b", "c"],
+ has_root: false,
+ is_absolute: false,
+ parent: Some("a\\b"),
+ file_name: Some("c"),
+ file_stem: Some("c"),
+ extension: None
+ );
+
+ t!("\\a",
+ iter: ["\\", "a"],
+ has_root: true,
+ is_absolute: false,
+ parent: Some("\\"),
+ file_name: Some("a"),
+ file_stem: Some("a"),
+ extension: None
+ );
+
+ t!("c:\\foo.txt",
+ iter: ["c:", "\\", "foo.txt"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("c:\\"),
+ file_name: Some("foo.txt"),
+ file_stem: Some("foo"),
+ extension: Some("txt")
+ );
+
+ t!("\\\\server\\share\\foo.txt",
+ iter: ["\\\\server\\share", "\\", "foo.txt"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("\\\\server\\share\\"),
+ file_name: Some("foo.txt"),
+ file_stem: Some("foo"),
+ extension: Some("txt")
+ );
+
+ t!("\\\\server\\share",
+ iter: ["\\\\server\\share", "\\"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("\\\\server",
+ iter: ["\\", "server"],
+ has_root: true,
+ is_absolute: false,
+ parent: Some("\\"),
+ file_name: Some("server"),
+ file_stem: Some("server"),
+ extension: None
+ );
+
+ t!("\\\\?\\bar\\foo.txt",
+ iter: ["\\\\?\\bar", "\\", "foo.txt"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("\\\\?\\bar\\"),
+ file_name: Some("foo.txt"),
+ file_stem: Some("foo"),
+ extension: Some("txt")
+ );
+
+ t!("\\\\?\\bar",
+ iter: ["\\\\?\\bar"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("\\\\?\\",
+ iter: ["\\\\?\\"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("\\\\?\\UNC\\server\\share\\foo.txt",
+ iter: ["\\\\?\\UNC\\server\\share", "\\", "foo.txt"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("\\\\?\\UNC\\server\\share\\"),
+ file_name: Some("foo.txt"),
+ file_stem: Some("foo"),
+ extension: Some("txt")
+ );
+
+ t!("\\\\?\\UNC\\server",
+ iter: ["\\\\?\\UNC\\server"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("\\\\?\\UNC\\",
+ iter: ["\\\\?\\UNC\\"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("\\\\?\\C:\\foo.txt",
+ iter: ["\\\\?\\C:", "\\", "foo.txt"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("\\\\?\\C:\\"),
+ file_name: Some("foo.txt"),
+ file_stem: Some("foo"),
+ extension: Some("txt")
+ );
+
+
+ t!("\\\\?\\C:\\",
+ iter: ["\\\\?\\C:", "\\"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+
+ t!("\\\\?\\C:",
+ iter: ["\\\\?\\C:"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+
+ t!("\\\\?\\foo/bar",
+ iter: ["\\\\?\\foo/bar"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+
+ t!("\\\\?\\C:/foo",
+ iter: ["\\\\?\\C:/foo"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+
+ t!("\\\\.\\foo\\bar",
+ iter: ["\\\\.\\foo", "\\", "bar"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("\\\\.\\foo\\"),
+ file_name: Some("bar"),
+ file_stem: Some("bar"),
+ extension: None
+ );
+
+
+ t!("\\\\.\\foo",
+ iter: ["\\\\.\\foo", "\\"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+
+ t!("\\\\.\\foo/bar",
+ iter: ["\\\\.\\foo/bar", "\\"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+
+ t!("\\\\.\\foo\\bar/baz",
+ iter: ["\\\\.\\foo", "\\", "bar", "baz"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("\\\\.\\foo\\bar"),
+ file_name: Some("baz"),
+ file_stem: Some("baz"),
+ extension: None
+ );
+
+
+ t!("\\\\.\\",
+ iter: ["\\\\.\\", "\\"],
+ has_root: true,
+ is_absolute: true,
+ parent: None,
+ file_name: None,
+ file_stem: None,
+ extension: None
+ );
+
+ t!("\\\\?\\a\\b\\",
+ iter: ["\\\\?\\a", "\\", "b"],
+ has_root: true,
+ is_absolute: true,
+ parent: Some("\\\\?\\a\\"),
+ file_name: Some("b"),
+ file_stem: Some("b"),
+ extension: None
+ );
+ }
+
+ #[test]
+ pub fn test_stem_ext() {
+ t!("foo",
+ file_stem: Some("foo"),
+ extension: None
+ );
+
+ t!("foo.",
+ file_stem: Some("foo"),
+ extension: Some("")
+ );
+
+ t!(".foo",
+ file_stem: Some(".foo"),
+ extension: None
+ );
+
+ t!("foo.txt",
+ file_stem: Some("foo"),
+ extension: Some("txt")
+ );
+
+ t!("foo.bar.txt",
+ file_stem: Some("foo.bar"),
+ extension: Some("txt")
+ );
+
+ t!("foo.bar.",
+ file_stem: Some("foo.bar"),
+ extension: Some("")
+ );
+
+ t!(".",
+ file_stem: None,
+ extension: None
+ );
+
+ t!("..",
+ file_stem: None,
+ extension: None
+ );
+
+ t!("",
+ file_stem: None,
+ extension: None
+ );
+ }
+
+ #[test]
+ pub fn test_push() {
+ macro_rules! tp(
+ ($path:expr, $push:expr, $expected:expr) => ( {
+ let mut actual = PathBuf::from($path);
+ actual.push($push);
+ assert!(actual.to_str() == Some($expected),
+ "pushing {:?} onto {:?}: Expected {:?}, got {:?}",
+ $push, $path, $expected, actual.to_str().unwrap());
+ });
+ );
+
+ if cfg!(unix) {
+ tp!("", "foo", "foo");
+ tp!("foo", "bar", "foo/bar");
+ tp!("foo/", "bar", "foo/bar");
+ tp!("foo//", "bar", "foo//bar");
+ tp!("foo/.", "bar", "foo/./bar");
+ tp!("foo./.", "bar", "foo././bar");
+ tp!("foo", "", "foo/");
+ tp!("foo", ".", "foo/.");
+ tp!("foo", "..", "foo/..");
+ tp!("foo", "/", "/");
+ tp!("/foo/bar", "/", "/");
+ tp!("/foo/bar", "/baz", "/baz");
+ tp!("/foo/bar", "./baz", "/foo/bar/./baz");
+ } else {
+ tp!("", "foo", "foo");
+ tp!("foo", "bar", r"foo\bar");
+ tp!("foo/", "bar", r"foo/bar");
+ tp!(r"foo\", "bar", r"foo\bar");
+ tp!("foo//", "bar", r"foo//bar");
+ tp!(r"foo\\", "bar", r"foo\\bar");
+ tp!("foo/.", "bar", r"foo/.\bar");
+ tp!("foo./.", "bar", r"foo./.\bar");
+ tp!(r"foo\.", "bar", r"foo\.\bar");
+ tp!(r"foo.\.", "bar", r"foo.\.\bar");
+ tp!("foo", "", "foo\\");
+ tp!("foo", ".", r"foo\.");
+ tp!("foo", "..", r"foo\..");
+ tp!("foo", "/", "/");
+ tp!("foo", r"\", r"\");
+ tp!("/foo/bar", "/", "/");
+ tp!(r"\foo\bar", r"\", r"\");
+ tp!("/foo/bar", "/baz", "/baz");
+ tp!("/foo/bar", r"\baz", r"\baz");
+ tp!("/foo/bar", "./baz", r"/foo/bar\./baz");
+ tp!("/foo/bar", r".\baz", r"/foo/bar\.\baz");
+
+ tp!("c:\\", "windows", "c:\\windows");
+ tp!("c:", "windows", "c:windows");
+
+ tp!("a\\b\\c", "d", "a\\b\\c\\d");
+ tp!("\\a\\b\\c", "d", "\\a\\b\\c\\d");
+ tp!("a\\b", "c\\d", "a\\b\\c\\d");
+ tp!("a\\b", "\\c\\d", "\\c\\d");
+ tp!("a\\b", ".", "a\\b\\.");
+ tp!("a\\b", "..\\c", "a\\b\\..\\c");
+ tp!("a\\b", "C:a.txt", "C:a.txt");
+ tp!("a\\b", "C:\\a.txt", "C:\\a.txt");
+ tp!("C:\\a", "C:\\b.txt", "C:\\b.txt");
+ tp!("C:\\a\\b\\c", "C:d", "C:d");
+ tp!("C:a\\b\\c", "C:d", "C:d");
+ tp!("C:", r"a\b\c", r"C:a\b\c");
+ tp!("C:", r"..\a", r"C:..\a");
+ tp!("\\\\server\\share\\foo",
+ "bar",
+ "\\\\server\\share\\foo\\bar");
+ tp!("\\\\server\\share\\foo", "C:baz", "C:baz");
+ tp!("\\\\?\\C:\\a\\b", "C:c\\d", "C:c\\d");
+ tp!("\\\\?\\C:a\\b", "C:c\\d", "C:c\\d");
+ tp!("\\\\?\\C:\\a\\b", "C:\\c\\d", "C:\\c\\d");
+ tp!("\\\\?\\foo\\bar", "baz", "\\\\?\\foo\\bar\\baz");
+ tp!("\\\\?\\UNC\\server\\share\\foo",
+ "bar",
+ "\\\\?\\UNC\\server\\share\\foo\\bar");
+ tp!("\\\\?\\UNC\\server\\share", "C:\\a", "C:\\a");
+ tp!("\\\\?\\UNC\\server\\share", "C:a", "C:a");
+
+ // Note: modified from old path API
+ tp!("\\\\?\\UNC\\server", "foo", "\\\\?\\UNC\\server\\foo");
+
+ tp!("C:\\a",
+ "\\\\?\\UNC\\server\\share",
+ "\\\\?\\UNC\\server\\share");
+ tp!("\\\\.\\foo\\bar", "baz", "\\\\.\\foo\\bar\\baz");
+ tp!("\\\\.\\foo\\bar", "C:a", "C:a");
+ // again, not sure about the following, but I'm assuming \\.\ should be verbatim
+ tp!("\\\\.\\foo", "..\\bar", "\\\\.\\foo\\..\\bar");
+
+ tp!("\\\\?\\C:", "foo", "\\\\?\\C:\\foo"); // this is a weird one
+ }
+ }
+
+ #[test]
+ pub fn test_pop() {
+ macro_rules! tp(
+ ($path:expr, $expected:expr, $output:expr) => ( {
+ let mut actual = PathBuf::from($path);
+ let output = actual.pop();
+ assert!(actual.to_str() == Some($expected) && output == $output,
+ "popping from {:?}: Expected {:?}/{:?}, got {:?}/{:?}",
+ $path, $expected, $output,
+ actual.to_str().unwrap(), output);
+ });
+ );
+
+ tp!("", "", false);
+ tp!("/", "/", false);
+ tp!("foo", "", true);
+ tp!(".", "", true);
+ tp!("/foo", "/", true);
+ tp!("/foo/bar", "/foo", true);
+ tp!("foo/bar", "foo", true);
+ tp!("foo/.", "", true);
+ tp!("foo//bar", "foo", true);
+
+ if cfg!(windows) {
+ tp!("a\\b\\c", "a\\b", true);
+ tp!("\\a", "\\", true);
+ tp!("\\", "\\", false);
+
+ tp!("C:\\a\\b", "C:\\a", true);
+ tp!("C:\\a", "C:\\", true);
+ tp!("C:\\", "C:\\", false);
+ tp!("C:a\\b", "C:a", true);
+ tp!("C:a", "C:", true);
+ tp!("C:", "C:", false);
+ tp!("\\\\server\\share\\a\\b", "\\\\server\\share\\a", true);
+ tp!("\\\\server\\share\\a", "\\\\server\\share\\", true);
+ tp!("\\\\server\\share", "\\\\server\\share", false);
+ tp!("\\\\?\\a\\b\\c", "\\\\?\\a\\b", true);
+ tp!("\\\\?\\a\\b", "\\\\?\\a\\", true);
+ tp!("\\\\?\\a", "\\\\?\\a", false);
+ tp!("\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", true);
+ tp!("\\\\?\\C:\\a", "\\\\?\\C:\\", true);
+ tp!("\\\\?\\C:\\", "\\\\?\\C:\\", false);
+ tp!("\\\\?\\UNC\\server\\share\\a\\b",
+ "\\\\?\\UNC\\server\\share\\a",
+ true);
+ tp!("\\\\?\\UNC\\server\\share\\a",
+ "\\\\?\\UNC\\server\\share\\",
+ true);
+ tp!("\\\\?\\UNC\\server\\share",
+ "\\\\?\\UNC\\server\\share",
+ false);
+ tp!("\\\\.\\a\\b\\c", "\\\\.\\a\\b", true);
+ tp!("\\\\.\\a\\b", "\\\\.\\a\\", true);
+ tp!("\\\\.\\a", "\\\\.\\a", false);
+
+ tp!("\\\\?\\a\\b\\", "\\\\?\\a\\", true);
+ }
+ }
+
+ #[test]
+ pub fn test_set_file_name() {
+ macro_rules! tfn(
+ ($path:expr, $file:expr, $expected:expr) => ( {
+ let mut p = PathBuf::from($path);
+ p.set_file_name($file);
+ assert!(p.to_str() == Some($expected),
+ "setting file name of {:?} to {:?}: Expected {:?}, got {:?}",
+ $path, $file, $expected,
+ p.to_str().unwrap());
+ });
+ );
+
+ tfn!("foo", "foo", "foo");
+ tfn!("foo", "bar", "bar");
+ tfn!("foo", "", "");
+ tfn!("", "foo", "foo");
+ if cfg!(unix) {
+ tfn!(".", "foo", "./foo");
+ tfn!("foo/", "bar", "bar");
+ tfn!("foo/.", "bar", "bar");
+ tfn!("..", "foo", "../foo");
+ tfn!("foo/..", "bar", "foo/../bar");
+ tfn!("/", "foo", "/foo");
+ } else {
+ tfn!(".", "foo", r".\foo");
+ tfn!(r"foo\", "bar", r"bar");
+ tfn!(r"foo\.", "bar", r"bar");
+ tfn!("..", "foo", r"..\foo");
+ tfn!(r"foo\..", "bar", r"foo\..\bar");
+ tfn!(r"\", "foo", r"\foo");
+ }
+ }
+
+ #[test]
+ pub fn test_set_extension() {
+ macro_rules! tfe(
+ ($path:expr, $ext:expr, $expected:expr, $output:expr) => ( {
+ let mut p = PathBuf::from($path);
+ let output = p.set_extension($ext);
+ assert!(p.to_str() == Some($expected) && output == $output,
+ "setting extension of {:?} to {:?}: Expected {:?}/{:?}, got {:?}/{:?}",
+ $path, $ext, $expected, $output,
+ p.to_str().unwrap(), output);
+ });
+ );
+
+ tfe!("foo", "txt", "foo.txt", true);
+ tfe!("foo.bar", "txt", "foo.txt", true);
+ tfe!("foo.bar.baz", "txt", "foo.bar.txt", true);
+ tfe!(".test", "txt", ".test.txt", true);
+ tfe!("foo.txt", "", "foo", true);
+ tfe!("foo", "", "foo", true);
+ tfe!("", "foo", "", false);
+ tfe!(".", "foo", ".", false);
+ tfe!("foo/", "bar", "foo.bar", true);
+ tfe!("foo/.", "bar", "foo.bar", true);
+ tfe!("..", "foo", "..", false);
+ tfe!("foo/..", "bar", "foo/..", false);
+ tfe!("/", "foo", "/", false);
+ }
+
+ #[test]
+ fn test_eq_recievers() {
+ use borrow::Cow;
+
+ let borrowed: &Path = Path::new("foo/bar");
+ let mut owned: PathBuf = PathBuf::new();
+ owned.push("foo");
+ owned.push("bar");
+ let borrowed_cow: Cow<Path> = borrowed.into();
+ let owned_cow: Cow<Path> = owned.clone().into();
+
+ macro_rules! t {
+ ($($current:expr),+) => {
+ $(
+ assert_eq!($current, borrowed);
+ assert_eq!($current, owned);
+ assert_eq!($current, borrowed_cow);
+ assert_eq!($current, owned_cow);
+ )+
+ }
+ }
+
+ t!(borrowed, owned, borrowed_cow, owned_cow);
+ }
+
+ #[test]
+ pub fn test_compare() {
+ use hash::{Hash, Hasher};
+ use collections::hash_map::DefaultHasher;
+
+ fn hash<T: Hash>(t: T) -> u64 {
+ let mut s = DefaultHasher::new();
+ t.hash(&mut s);
+ s.finish()
+ }
+
+ macro_rules! tc(
+ ($path1:expr, $path2:expr, eq: $eq:expr,
+ starts_with: $starts_with:expr, ends_with: $ends_with:expr,
+ relative_from: $relative_from:expr) => ({
+ let path1 = Path::new($path1);
+ let path2 = Path::new($path2);
+
+ let eq = path1 == path2;
+ assert!(eq == $eq, "{:?} == {:?}, expected {:?}, got {:?}",
+ $path1, $path2, $eq, eq);
+ assert!($eq == (hash(path1) == hash(path2)),
+ "{:?} == {:?}, expected {:?}, got {} and {}",
+ $path1, $path2, $eq, hash(path1), hash(path2));
+
+ let starts_with = path1.starts_with(path2);
+ assert!(starts_with == $starts_with,
+ "{:?}.starts_with({:?}), expected {:?}, got {:?}", $path1, $path2,
+ $starts_with, starts_with);
+
+ let ends_with = path1.ends_with(path2);
+ assert!(ends_with == $ends_with,
+ "{:?}.ends_with({:?}), expected {:?}, got {:?}", $path1, $path2,
+ $ends_with, ends_with);
+
+ let relative_from = path1.strip_prefix(path2)
+ .map(|p| p.to_str().unwrap())
+ .ok();
+ let exp: Option<&str> = $relative_from;
+ assert!(relative_from == exp,
+ "{:?}.strip_prefix({:?}), expected {:?}, got {:?}",
+ $path1, $path2, exp, relative_from);
+ });
+ );
+
+ tc!("", "",
+ eq: true,
+ starts_with: true,
+ ends_with: true,
+ relative_from: Some("")
+ );
+
+ tc!("foo", "",
+ eq: false,
+ starts_with: true,
+ ends_with: true,
+ relative_from: Some("foo")
+ );
+
+ tc!("", "foo",
+ eq: false,
+ starts_with: false,
+ ends_with: false,
+ relative_from: None
+ );
+
+ tc!("foo", "foo",
+ eq: true,
+ starts_with: true,
+ ends_with: true,
+ relative_from: Some("")
+ );
+
+ tc!("foo/", "foo",
+ eq: true,
+ starts_with: true,
+ ends_with: true,
+ relative_from: Some("")
+ );
+
+ tc!("foo/bar", "foo",
+ eq: false,
+ starts_with: true,
+ ends_with: false,
+ relative_from: Some("bar")
+ );
+
+ tc!("foo/bar/baz", "foo/bar",
+ eq: false,
+ starts_with: true,
+ ends_with: false,
+ relative_from: Some("baz")
+ );
+
+ tc!("foo/bar", "foo/bar/baz",
+ eq: false,
+ starts_with: false,
+ ends_with: false,
+ relative_from: None
+ );
+
+ tc!("./foo/bar/", ".",
+ eq: false,
+ starts_with: true,
+ ends_with: false,
+ relative_from: Some("foo/bar")
+ );
+
+ if cfg!(windows) {
+ tc!(r"C:\src\rust\cargo-test\test\Cargo.toml",
+ r"c:\src\rust\cargo-test\test",
+ eq: false,
+ starts_with: true,
+ ends_with: false,
+ relative_from: Some("Cargo.toml")
+ );
+
+ tc!(r"c:\foo", r"C:\foo",
+ eq: true,
+ starts_with: true,
+ ends_with: true,
+ relative_from: Some("")
+ );
+ }
+ }
+
+ #[test]
+ fn test_components_debug() {
+ let path = Path::new("/tmp");
+
+ let mut components = path.components();
+
+ let expected = "Components([RootDir, Normal(\"tmp\")])";
+ let actual = format!("{:?}", components);
+ assert_eq!(expected, actual);
+
+ let _ = components.next().unwrap();
+ let expected = "Components([Normal(\"tmp\")])";
+ let actual = format!("{:?}", components);
+ assert_eq!(expected, actual);
+
+ let _ = components.next().unwrap();
+ let expected = "Components([])";
+ let actual = format!("{:?}", components);
+ assert_eq!(expected, actual);
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn test_iter_debug() {
+ let path = Path::new("/tmp");
+
+ let mut iter = path.iter();
+
+ let expected = "Iter([\"/\", \"tmp\"])";
+ let actual = format!("{:?}", iter);
+ assert_eq!(expected, actual);
+
+ let _ = iter.next().unwrap();
+ let expected = "Iter([\"tmp\"])";
+ let actual = format!("{:?}", iter);
+ assert_eq!(expected, actual);
+
+ let _ = iter.next().unwrap();
+ let expected = "Iter([])";
+ let actual = format!("{:?}", iter);
+ assert_eq!(expected, actual);
+ }
+}
diff --git a/ctr-std/src/prelude/mod.rs b/ctr-std/src/prelude/mod.rs
new file mode 100644
index 0000000..f4cd319
--- /dev/null
+++ b/ctr-std/src/prelude/mod.rs
@@ -0,0 +1,146 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! The Rust Prelude.
+//!
+//! Rust comes with a variety of things in its standard library. However, if
+//! you had to manually import every single thing that you used, it would be
+//! very verbose. But importing a lot of things that a program never uses isn't
+//! good either. A balance needs to be struck.
+//!
+//! The *prelude* is the list of things that Rust automatically imports into
+//! every Rust program. It's kept as small as possible, and is focused on
+//! things, particularly traits, which are used in almost every single Rust
+//! program.
+//!
+//! On a technical level, Rust inserts
+//!
+//! ```ignore
+//! extern crate std;
+//! ```
+//!
+//! into the crate root of every crate, and
+//!
+//! ```ignore
+//! use std::prelude::v1::*;
+//! ```
+//!
+//! into every module.
+//!
+//! # Other preludes
+//!
+//! Preludes can be seen as a pattern to make using multiple types more
+//! convenient. As such, you'll find other preludes in the standard library,
+//! such as [`std::io::prelude`]. Various libraries in the Rust ecosystem may
+//! also define their own preludes.
+//!
+//! [`std::io::prelude`]: ../io/prelude/index.html
+//!
+//! The difference between 'the prelude' and these other preludes is that they
+//! are not automatically `use`'d, and must be imported manually. This is still
+//! easier than importing all of their constituent components.
+//!
+//! # Prelude contents
+//!
+//! The current version of the prelude (version 1) lives in
+//! [`std::prelude::v1`], and reexports the following.
+//!
+//! * [`std::marker`]::{[`Copy`], [`Send`], [`Sized`], [`Sync`]}. The marker
+//! traits indicate fundamental properties of types.
+//! * [`std::ops`]::{[`Drop`], [`Fn`], [`FnMut`], [`FnOnce`]}. Various
+//! operations for both destructors and overloading `()`.
+//! * [`std::mem`]::[`drop`], a convenience function for explicitly dropping a
+//! value.
+//! * [`std::boxed`]::[`Box`], a way to allocate values on the heap.
+//! * [`std::borrow`]::[`ToOwned`], The conversion trait that defines
+//! [`to_owned()`], the generic method for creating an owned type from a
+//! borrowed type.
+//! * [`std::clone`]::[`Clone`], the ubiquitous trait that defines [`clone()`],
+//! the method for producing a copy of a value.
+//! * [`std::cmp`]::{[`PartialEq`], [`PartialOrd`], [`Eq`], [`Ord`] }. The
+//! comparison traits, which implement the comparison operators and are often
+//! seen in trait bounds.
+//! * [`std::convert`]::{[`AsRef`], [`AsMut`], [`Into`], [`From`]}. Generic
+//! conversions, used by savvy API authors to create overloaded methods.
+//! * [`std::default`]::[`Default`], types that have default values.
+//! * [`std::iter`]::{[`Iterator`], [`Extend`], [`IntoIterator`],
+//! [`DoubleEndedIterator`], [`ExactSizeIterator`]}. Iterators of various
+//! kinds.
+//! * [`std::option`]::[`Option`]::{`self`, `Some`, `None`}. A type which
+//! expresses the presence or absence of a value. This type is so commonly
+//! used, its variants are also exported.
+//! * [`std::result`]::[`Result`]::{`self`, `Ok`, `Err`}. A type for functions
+//! that may succeed or fail. Like [`Option`], its variants are exported as
+//! well.
+//! * [`std::slice`]::[`SliceConcatExt`], a trait that exists for technical
+//! reasons, but shouldn't have to exist. It provides a few useful methods on
+//! slices.
+//! * [`std::string`]::{[`String`], [`ToString`]}, heap allocated strings.
+//! * [`std::vec`]::[`Vec`](../vec/struct.Vec.html), a growable, heap-allocated
+//! vector.
+//!
+//! [`AsMut`]: ../convert/trait.AsMut.html
+//! [`AsRef`]: ../convert/trait.AsRef.html
+//! [`Box`]: ../boxed/struct.Box.html
+//! [`Clone`]: ../clone/trait.Clone.html
+//! [`Copy`]: ../marker/trait.Copy.html
+//! [`Default`]: ../default/trait.Default.html
+//! [`DoubleEndedIterator`]: ../iter/trait.DoubleEndedIterator.html
+//! [`Drop`]: ../ops/trait.Drop.html
+//! [`Eq`]: ../cmp/trait.Eq.html
+//! [`ExactSizeIterator`]: ../iter/trait.ExactSizeIterator.html
+//! [`Extend`]: ../iter/trait.Extend.html
+//! [`FnMut`]: ../ops/trait.FnMut.html
+//! [`FnOnce`]: ../ops/trait.FnOnce.html
+//! [`Fn`]: ../ops/trait.Fn.html
+//! [`From`]: ../convert/trait.From.html
+//! [`IntoIterator`]: ../iter/trait.IntoIterator.html
+//! [`Into`]: ../convert/trait.Into.html
+//! [`Iterator`]: ../iter/trait.Iterator.html
+//! [`Option`]: ../option/enum.Option.html
+//! [`Ord`]: ../cmp/trait.Ord.html
+//! [`PartialEq`]: ../cmp/trait.PartialEq.html
+//! [`PartialOrd`]: ../cmp/trait.PartialOrd.html
+//! [`Result`]: ../result/enum.Result.html
+//! [`Send`]: ../marker/trait.Send.html
+//! [`Sized`]: ../marker/trait.Sized.html
+//! [`SliceConcatExt`]: ../slice/trait.SliceConcatExt.html
+//! [`String`]: ../string/struct.String.html
+//! [`Sync`]: ../marker/trait.Sync.html
+//! [`ToOwned`]: ../borrow/trait.ToOwned.html
+//! [`ToString`]: ../string/trait.ToString.html
+//! [`Vec`]: ../vec/struct.Vec.html
+//! [`clone()`]: ../clone/trait.Clone.html#tymethod.clone
+//! [`drop`]: ../mem/fn.drop.html
+//! [`std::borrow`]: ../borrow/index.html
+//! [`std::boxed`]: ../boxed/index.html
+//! [`std::clone`]: ../clone/index.html
+//! [`std::cmp`]: ../cmp/index.html
+//! [`std::convert`]: ../convert/index.html
+//! [`std::default`]: ../default/index.html
+//! [`std::iter`]: ../iter/index.html
+//! [`std::marker`]: ../marker/index.html
+//! [`std::mem`]: ../mem/index.html
+//! [`std::ops`]: ../ops/index.html
+//! [`std::option`]: ../option/index.html
+//! [`std::prelude::v1`]: v1/index.html
+//! [`std::result`]: ../result/index.html
+//! [`std::slice`]: ../slice/index.html
+//! [`std::string`]: ../string/index.html
+//! [`std::vec`]: ../vec/index.html
+//! [`to_owned()`]: ../borrow/trait.ToOwned.html#tymethod.to_owned
+//! [book-closures]: ../../book/closures.html
+//! [book-dtor]: ../../book/drop.html
+//! [book-enums]: ../../book/enums.html
+//! [book-iter]: ../../book/iterators.html
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+pub mod v1;
diff --git a/ctr-std/src/prelude/v1.rs b/ctr-std/src/prelude/v1.rs
new file mode 100644
index 0000000..9ca5b44
--- /dev/null
+++ b/ctr-std/src/prelude/v1.rs
@@ -0,0 +1,53 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! The first version of the prelude of The Rust Standard Library.
+//!
+//! See the [module-level documentation](../index.html) for more.
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+// Reexported core operators
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use marker::{Copy, Send, Sized, Sync};
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use ops::{Drop, Fn, FnMut, FnOnce};
+
+// Reexported functions
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use mem::drop;
+
+// Reexported types and traits
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use boxed::Box;
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use borrow::ToOwned;
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use clone::Clone;
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use cmp::{PartialEq, PartialOrd, Eq, Ord};
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use convert::{AsRef, AsMut, Into, From};
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use default::Default;
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use iter::{Iterator, Extend, IntoIterator};
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use iter::{DoubleEndedIterator, ExactSizeIterator};
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use option::Option::{self, Some, None};
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use result::Result::{self, Ok, Err};
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use slice::SliceConcatExt;
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use string::{String, ToString};
+#[stable(feature = "rust1", since = "1.0.0")]
+#[doc(no_inline)] pub use vec::Vec;
diff --git a/ctr-std/src/rt.rs b/ctr-std/src/rt.rs
new file mode 100644
index 0000000..178b5a0
--- /dev/null
+++ b/ctr-std/src/rt.rs
@@ -0,0 +1,36 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Runtime services
+//!
+//! The `rt` module provides a narrow set of runtime services,
+//! including the global heap (exported in `heap`) and unwinding and
+//! backtrace support. The APIs in this module are highly unstable,
+//! and should be considered as private implementation details for the
+//! time being.
+
+#![unstable(feature = "rt",
+ reason = "this public module should not exist and is highly likely \
+ to disappear",
+ issue = "0")]
+#![doc(hidden)]
+
+use mem;
+
+// Reexport some of our utilities which are expected by other crates.
+pub use panicking::{begin_panic, begin_panic_fmt};
+
+//TODO: Handle argc/argv arguments
+#[lang = "start"]
+#[allow(unused_variables)]
+fn lang_start(main: *const u8, argc: isize, argv: *const *const u8) -> isize {
+ unsafe { mem::transmute::<_, fn()>(main)(); }
+ 0
+}
diff --git a/ctr-std/src/sync/mod.rs b/ctr-std/src/sync/mod.rs
new file mode 100644
index 0000000..487c4c9
--- /dev/null
+++ b/ctr-std/src/sync/mod.rs
@@ -0,0 +1,29 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Useful synchronization primitives.
+//!
+//! This module contains useful safe and unsafe synchronization primitives.
+//! Most of the primitives in this module do not provide any sort of locking
+//! and/or blocking at all, but rather provide the necessary tools to build
+//! other types of concurrent primitives.
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use alloc::arc::{Arc, Weak};
+#[stable(feature = "rust1", since = "1.0.0")]
+pub use core::sync::atomic;
+
+// Easy cheat until we get proper locks based on libctru code
+#[stable(feature = "3ds", since = "1.0.0")]
+pub use spin::{Mutex, MutexGuard};
+#[stable(feature = "3ds", since = "1.0.0")]
+pub use spin::{RwLock, RwLockReadGuard, RwLockWriteGuard};
diff --git a/ctr-std/src/sys/mod.rs b/ctr-std/src/sys/mod.rs
new file mode 100644
index 0000000..175e227
--- /dev/null
+++ b/ctr-std/src/sys/mod.rs
@@ -0,0 +1,37 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Platform-dependent platform abstraction
+//!
+//! The `std::sys` module is the abstracted interface through which
+//! `std` talks to the underlying operating system. It has different
+//! implementations for different operating system families, today
+//! just Unix and Windows.
+//!
+//! The centralization of platform-specific code in this module is
+//! enforced by the "platform abstraction layer" tidy script in
+//! `tools/tidy/pal.rs`.
+//!
+//! This module is closely related to the platform-independent system
+//! integration code in `std::sys_common`. See that module's
+//! documentation for details.
+//!
+//! In the future it would be desirable for the indepedent
+//! implementations of this module to be extracted to their own crates
+//! that `std` can link to, thus enabling their implementation
+//! out-of-tree via crate replacement. Though due to the complex
+//! inter-dependencies within `std` that will be a challenging goal to
+//! achieve.
+
+pub use self::imp::*;
+
+#[cfg(unix)]
+#[path = "unix/mod.rs"]
+mod imp;
diff --git a/ctr-std/src/sys/unix/ext/ffi.rs b/ctr-std/src/sys/unix/ext/ffi.rs
new file mode 100644
index 0000000..d59b4fc
--- /dev/null
+++ b/ctr-std/src/sys/unix/ext/ffi.rs
@@ -0,0 +1,61 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Unix-specific extension to the primitives in the `std::ffi` module
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+use ffi::{OsStr, OsString};
+use mem;
+use sys::os_str::Buf;
+use sys_common::{FromInner, IntoInner, AsInner};
+
+/// Unix-specific extensions to `OsString`.
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait OsStringExt {
+ /// Creates an `OsString` from a byte vector.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn from_vec(vec: Vec<u8>) -> Self;
+
+ /// Yields the underlying byte vector of this `OsString`.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn into_vec(self) -> Vec<u8>;
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl OsStringExt for OsString {
+ fn from_vec(vec: Vec<u8>) -> OsString {
+ FromInner::from_inner(Buf { inner: vec })
+ }
+ fn into_vec(self) -> Vec<u8> {
+ self.into_inner().inner
+ }
+}
+
+/// Unix-specific extensions to `OsStr`.
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait OsStrExt {
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn from_bytes(slice: &[u8]) -> &Self;
+
+ /// Gets the underlying byte view of the `OsStr` slice.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn as_bytes(&self) -> &[u8];
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl OsStrExt for OsStr {
+ fn from_bytes(slice: &[u8]) -> &OsStr {
+ unsafe { mem::transmute(slice) }
+ }
+ fn as_bytes(&self) -> &[u8] {
+ &self.as_inner().inner
+ }
+}
diff --git a/ctr-std/src/sys/unix/ext/mod.rs b/ctr-std/src/sys/unix/ext/mod.rs
new file mode 100644
index 0000000..04ea563
--- /dev/null
+++ b/ctr-std/src/sys/unix/ext/mod.rs
@@ -0,0 +1,41 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Experimental extensions to `std` for Unix platforms.
+//!
+//! For now, this module is limited to extracting file descriptors,
+//! but its functionality will grow over time.
+//!
+//! # Example
+//!
+//! ```no_run
+//! use std::fs::File;
+//! use std::os::unix::prelude::*;
+//!
+//! fn main() {
+//! let f = File::create("foo.txt").unwrap();
+//! let fd = f.as_raw_fd();
+//!
+//! // use fd with native unix bindings
+//! }
+//! ```
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+pub mod ffi;
+
+/// A prelude for conveniently writing platform-specific code.
+///
+/// Includes all extension traits, and some important type definitions.
+#[stable(feature = "rust1", since = "1.0.0")]
+pub mod prelude {
+ #[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")]
+ pub use super::ffi::{OsStrExt, OsStringExt};
+}
diff --git a/ctr-std/src/sys/unix/memchr.rs b/ctr-std/src/sys/unix/memchr.rs
new file mode 100644
index 0000000..d7e9c2b
--- /dev/null
+++ b/ctr-std/src/sys/unix/memchr.rs
@@ -0,0 +1,51 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+//
+// Original implementation taken from rust-memchr
+// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
+
+pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
+ use libc;
+
+ let p = unsafe {
+ libc::memchr(
+ haystack.as_ptr() as *const libc::c_void,
+ needle as libc::c_int,
+ haystack.len())
+ };
+ if p.is_null() {
+ None
+ } else {
+ Some(p as usize - (haystack.as_ptr() as usize))
+ }
+}
+
+pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
+
+ #[cfg(target_os = "linux")]
+ fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
+ use libc;
+
+ // GNU's memrchr() will - unlike memchr() - error if haystack is empty.
+ if haystack.is_empty() {return None}
+ let p = unsafe {
+ libc::memrchr(
+ haystack.as_ptr() as *const libc::c_void,
+ needle as libc::c_int,
+ haystack.len())
+ };
+ if p.is_null() {
+ None
+ } else {
+ Some(p as usize - (haystack.as_ptr() as usize))
+ }
+ }
+ memrchr_specific(needle, haystack)
+}
diff --git a/ctr-std/src/sys/unix/mod.rs b/ctr-std/src/sys/unix/mod.rs
new file mode 100644
index 0000000..698e31a
--- /dev/null
+++ b/ctr-std/src/sys/unix/mod.rs
@@ -0,0 +1,50 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![allow(missing_docs, bad_style)]
+
+pub mod ext;
+pub mod memchr;
+pub mod os;
+pub mod os_str;
+pub mod path;
+
+use io::ErrorKind;
+use libc;
+
+pub fn decode_error_kind(errno: i32) -> ErrorKind {
+ match errno as libc::c_int {
+ libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
+ libc::ECONNRESET => ErrorKind::ConnectionReset,
+ libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
+ libc::EPIPE => ErrorKind::BrokenPipe,
+ libc::ENOTCONN => ErrorKind::NotConnected,
+ libc::ECONNABORTED => ErrorKind::ConnectionAborted,
+ libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
+ libc::EADDRINUSE => ErrorKind::AddrInUse,
+ libc::ENOENT => ErrorKind::NotFound,
+ libc::EINTR => ErrorKind::Interrupted,
+ libc::EINVAL => ErrorKind::InvalidInput,
+ libc::ETIMEDOUT => ErrorKind::TimedOut,
+ libc::EEXIST => ErrorKind::AlreadyExists,
+
+ // These two constants can have the same value on some systems,
+ // but different values on others, so we can't use a match
+ // clause
+ x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
+ ErrorKind::WouldBlock,
+
+ _ => ErrorKind::Other,
+ }
+}
+
+pub unsafe fn abort_internal() -> ! {
+ ::libc::abort()
+}
diff --git a/ctr-std/src/sys/unix/os.rs b/ctr-std/src/sys/unix/os.rs
new file mode 100644
index 0000000..de087d9
--- /dev/null
+++ b/ctr-std/src/sys/unix/os.rs
@@ -0,0 +1,123 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Implementation of `std::os` functionality for unix systems
+
+#![allow(unused_imports)] // lots of cfg code here
+
+use os::unix::prelude::*;
+
+use error::Error as StdError;
+use ffi::{CString, CStr, OsString, OsStr};
+use fmt;
+use io;
+use iter;
+use libc::{self, c_int, c_char, c_void};
+use marker::PhantomData;
+use mem;
+use memchr;
+use path::{self, PathBuf};
+use ptr;
+use slice;
+use str;
+use vec;
+
+const TMPBUF_SZ: usize = 128;
+
+extern "C" {
+ fn __errno() -> *mut c_int;
+}
+
+/// Returns the platform-specific value of errno
+pub fn errno() -> i32 {
+ unsafe {
+ (*__errno()) as i32
+ }
+}
+
+/// Gets a detailed string description for the given error number.
+pub fn error_string(errno: i32) -> String {
+ extern {
+ #[cfg_attr(any(target_os = "linux", target_env = "newlib"),
+ link_name = "__xpg_strerror_r")]
+ fn strerror_r(errnum: c_int, buf: *mut c_char,
+ buflen: libc::size_t) -> c_int;
+ }
+
+ let mut buf = [0 as c_char; TMPBUF_SZ];
+
+ let p = buf.as_mut_ptr();
+ unsafe {
+ if strerror_r(errno as c_int, p, buf.len()) < 0 {
+ panic!("strerror_r failure");
+ }
+
+ let p = p as *const _;
+ str::from_utf8(CStr::from_ptr(p).to_bytes()).unwrap().to_owned()
+ }
+}
+
+
+pub struct SplitPaths<'a> {
+ iter: iter::Map<slice::Split<'a, u8, fn(&u8) -> bool>,
+ fn(&'a [u8]) -> PathBuf>,
+}
+
+pub fn split_paths(unparsed: &OsStr) -> SplitPaths {
+ fn bytes_to_path(b: &[u8]) -> PathBuf {
+ PathBuf::from(<OsStr as OsStrExt>::from_bytes(b))
+ }
+ fn is_colon(b: &u8) -> bool { *b == b':' }
+ let unparsed = unparsed.as_bytes();
+ SplitPaths {
+ iter: unparsed.split(is_colon as fn(&u8) -> bool)
+ .map(bytes_to_path as fn(&[u8]) -> PathBuf)
+ }
+}
+
+impl<'a> Iterator for SplitPaths<'a> {
+ type Item = PathBuf;
+ fn next(&mut self) -> Option<PathBuf> { self.iter.next() }
+ fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
+}
+
+#[derive(Debug)]
+pub struct JoinPathsError;
+
+pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
+ where I: Iterator<Item=T>, T: AsRef<OsStr>
+{
+ let mut joined = Vec::new();
+ let sep = b':';
+
+ for (i, path) in paths.enumerate() {
+ let path = path.as_ref().as_bytes();
+ if i > 0 { joined.push(sep) }
+ if path.contains(&sep) {
+ return Err(JoinPathsError)
+ }
+ joined.extend_from_slice(path);
+ }
+ Ok(OsStringExt::from_vec(joined))
+}
+
+impl fmt::Display for JoinPathsError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ "path segment contains separator `:`".fmt(f)
+ }
+}
+
+impl StdError for JoinPathsError {
+ fn description(&self) -> &str { "failed to join paths" }
+}
+
+pub fn exit(code: i32) -> ! {
+ unsafe { libc::exit(code as c_int) }
+}
diff --git a/ctr-std/src/sys/unix/os_str.rs b/ctr-std/src/sys/unix/os_str.rs
new file mode 100644
index 0000000..5a733c0
--- /dev/null
+++ b/ctr-std/src/sys/unix/os_str.rs
@@ -0,0 +1,119 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+/// The underlying OsString/OsStr implementation on Unix systems: just
+/// a `Vec<u8>`/`[u8]`.
+
+use borrow::Cow;
+use fmt::{self, Debug};
+use str;
+use mem;
+use sys_common::{AsInner, IntoInner};
+
+#[derive(Clone, Hash)]
+pub struct Buf {
+ pub inner: Vec<u8>
+}
+
+pub struct Slice {
+ pub inner: [u8]
+}
+
+impl Debug for Slice {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ self.to_string_lossy().fmt(formatter)
+ }
+}
+
+impl Debug for Buf {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ self.as_slice().fmt(formatter)
+ }
+}
+
+impl IntoInner<Vec<u8>> for Buf {
+ fn into_inner(self) -> Vec<u8> {
+ self.inner
+ }
+}
+
+impl AsInner<[u8]> for Buf {
+ fn as_inner(&self) -> &[u8] {
+ &self.inner
+ }
+}
+
+
+impl Buf {
+ pub fn from_string(s: String) -> Buf {
+ Buf { inner: s.into_bytes() }
+ }
+
+ #[inline]
+ pub fn with_capacity(capacity: usize) -> Buf {
+ Buf {
+ inner: Vec::with_capacity(capacity)
+ }
+ }
+
+ #[inline]
+ pub fn clear(&mut self) {
+ self.inner.clear()
+ }
+
+ #[inline]
+ pub fn capacity(&self) -> usize {
+ self.inner.capacity()
+ }
+
+ #[inline]
+ pub fn reserve(&mut self, additional: usize) {
+ self.inner.reserve(additional)
+ }
+
+ #[inline]
+ pub fn reserve_exact(&mut self, additional: usize) {
+ self.inner.reserve_exact(additional)
+ }
+
+ pub fn as_slice(&self) -> &Slice {
+ unsafe { mem::transmute(&*self.inner) }
+ }
+
+ pub fn into_string(self) -> Result<String, Buf> {
+ String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() } )
+ }
+
+ pub fn push_slice(&mut self, s: &Slice) {
+ self.inner.extend_from_slice(&s.inner)
+ }
+}
+
+impl Slice {
+ fn from_u8_slice(s: &[u8]) -> &Slice {
+ unsafe { mem::transmute(s) }
+ }
+
+ pub fn from_str(s: &str) -> &Slice {
+ Slice::from_u8_slice(s.as_bytes())
+ }
+
+ pub fn to_str(&self) -> Option<&str> {
+ str::from_utf8(&self.inner).ok()
+ }
+
+ pub fn to_string_lossy(&self) -> Cow<str> {
+ String::from_utf8_lossy(&self.inner)
+ }
+
+ pub fn to_owned(&self) -> Buf {
+ Buf { inner: self.inner.to_vec() }
+ }
+}
diff --git a/ctr-std/src/sys/unix/path.rs b/ctr-std/src/sys/unix/path.rs
new file mode 100644
index 0000000..bf9af7a
--- /dev/null
+++ b/ctr-std/src/sys/unix/path.rs
@@ -0,0 +1,29 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use path::Prefix;
+use ffi::OsStr;
+
+#[inline]
+pub fn is_sep_byte(b: u8) -> bool {
+ b == b'/'
+}
+
+#[inline]
+pub fn is_verbatim_sep(b: u8) -> bool {
+ b == b'/'
+}
+
+pub fn parse_prefix(_: &OsStr) -> Option<Prefix> {
+ None
+}
+
+pub const MAIN_SEP_STR: &'static str = "/";
+pub const MAIN_SEP: char = '/';
diff --git a/ctr-std/src/sys_common/io.rs b/ctr-std/src/sys_common/io.rs
new file mode 100644
index 0000000..23daeeb
--- /dev/null
+++ b/ctr-std/src/sys_common/io.rs
@@ -0,0 +1,179 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+use io;
+use io::ErrorKind;
+use io::Read;
+use slice::from_raw_parts_mut;
+
+pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
+
+// Provides read_to_end functionality over an uninitialized buffer.
+// This function is unsafe because it calls the underlying
+// read function with a slice into uninitialized memory. The default
+// implementation of read_to_end for readers will zero out new memory in
+// the buf before passing it to read, but avoiding this zero can often
+// lead to a fairly significant performance win.
+//
+// Implementations using this method have to adhere to two guarantees:
+// * The implementation of read never reads the buffer provided.
+// * The implementation of read correctly reports how many bytes were written.
+pub unsafe fn read_to_end_uninitialized(r: &mut Read, buf: &mut Vec<u8>) -> io::Result<usize> {
+
+ let start_len = buf.len();
+ buf.reserve(16);
+
+ // Always try to read into the empty space of the vector (from the length to the capacity).
+ // If the vector ever fills up then we reserve an extra byte which should trigger the normal
+ // reallocation routines for the vector, which will likely double the size.
+ //
+ // This function is similar to the read_to_end function in std::io, but the logic about
+ // reservations and slicing is different enough that this is duplicated here.
+ loop {
+ if buf.len() == buf.capacity() {
+ buf.reserve(1);
+ }
+
+ let buf_slice = from_raw_parts_mut(buf.as_mut_ptr().offset(buf.len() as isize),
+ buf.capacity() - buf.len());
+
+ match r.read(buf_slice) {
+ Ok(0) => { return Ok(buf.len() - start_len); }
+ Ok(n) => { let len = buf.len() + n; buf.set_len(len); },
+ Err(ref e) if e.kind() == ErrorKind::Interrupted => { }
+ Err(e) => { return Err(e); }
+ }
+ }
+}
+
+#[cfg(test)]
+#[allow(dead_code)] // not used on emscripten
+pub mod test {
+ use path::{Path, PathBuf};
+ use env;
+ use rand::{self, Rng};
+ use fs;
+
+ pub struct TempDir(PathBuf);
+
+ impl TempDir {
+ pub fn join(&self, path: &str) -> PathBuf {
+ let TempDir(ref p) = *self;
+ p.join(path)
+ }
+
+ pub fn path<'a>(&'a self) -> &'a Path {
+ let TempDir(ref p) = *self;
+ p
+ }
+ }
+
+ impl Drop for TempDir {
+ fn drop(&mut self) {
+ // Gee, seeing how we're testing the fs module I sure hope that we
+ // at least implement this correctly!
+ let TempDir(ref p) = *self;
+ fs::remove_dir_all(p).unwrap();
+ }
+ }
+
+ pub fn tmpdir() -> TempDir {
+ let p = env::temp_dir();
+ let mut r = rand::thread_rng();
+ let ret = p.join(&format!("rust-{}", r.next_u32()));
+ fs::create_dir(&ret).unwrap();
+ TempDir(ret)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use io::prelude::*;
+ use super::*;
+ use io;
+ use io::{ErrorKind, Take, Repeat, repeat};
+ use slice::from_raw_parts;
+
+ struct ErrorRepeat {
+ lr: Take<Repeat>
+ }
+
+ fn error_repeat(byte: u8, limit: u64) -> ErrorRepeat {
+ ErrorRepeat { lr: repeat(byte).take(limit) }
+ }
+
+ impl Read for ErrorRepeat {
+ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+ let ret = self.lr.read(buf);
+ if let Ok(0) = ret {
+ return Err(io::Error::new(ErrorKind::Other, ""))
+ }
+ ret
+ }
+ }
+
+ fn init_vec_data() -> Vec<u8> {
+ let mut vec = vec![10u8; 200];
+ unsafe { vec.set_len(0); }
+ vec
+ }
+
+ fn assert_all_eq(buf: &[u8], value: u8) {
+ for n in buf {
+ assert_eq!(*n, value);
+ }
+ }
+
+ fn validate(buf: &Vec<u8>, good_read_len: usize) {
+ assert_all_eq(buf, 1u8);
+ let cap = buf.capacity();
+ let end_slice = unsafe { from_raw_parts(buf.as_ptr().offset(good_read_len as isize),
+ cap - good_read_len) };
+ assert_all_eq(end_slice, 10u8);
+ }
+
+ #[test]
+ fn read_to_end_uninit_error() {
+ let mut er = error_repeat(1,100);
+ let mut vec = init_vec_data();
+ if let Err(_) = unsafe { read_to_end_uninitialized(&mut er, &mut vec) } {
+ validate(&vec, 100);
+ } else {
+ assert!(false);
+ }
+ }
+
+ #[test]
+ fn read_to_end_uninit_zero_len_vec() {
+ let mut er = repeat(1).take(100);
+ let mut vec = Vec::new();
+ let n = unsafe{ read_to_end_uninitialized(&mut er, &mut vec).unwrap() };
+ assert_all_eq(&vec, 1u8);
+ assert_eq!(vec.len(), n);
+ }
+
+ #[test]
+ fn read_to_end_uninit_good() {
+ let mut er = repeat(1).take(100);
+ let mut vec = init_vec_data();
+ let n = unsafe{ read_to_end_uninitialized(&mut er, &mut vec).unwrap() };
+ validate(&vec, 100);
+ assert_eq!(vec.len(), n);
+ }
+
+ #[bench]
+ #[cfg_attr(target_os = "emscripten", ignore)]
+ fn bench_uninitialized(b: &mut ::test::Bencher) {
+ b.iter(|| {
+ let mut lr = repeat(1).take(10000000);
+ let mut vec = Vec::with_capacity(1024);
+ unsafe { read_to_end_uninitialized(&mut lr, &mut vec) }
+ });
+ }
+}
diff --git a/ctr-std/src/sys_common/mod.rs b/ctr-std/src/sys_common/mod.rs
new file mode 100644
index 0000000..38057b8
--- /dev/null
+++ b/ctr-std/src/sys_common/mod.rs
@@ -0,0 +1,76 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Platform-independent platform abstraction
+//!
+//! This is the platform-independent portion of the standard libraries
+//! platform abstraction layer, whereas `std::sys` is the
+//! platform-specific portion.
+//!
+//! The relationship between `std::sys_common`, `std::sys` and the
+//! rest of `std` is complex, with dependencies going in all
+//! directions: `std` depending on `sys_common`, `sys_common`
+//! depending on `sys`, and `sys` depending on `sys_common` and `std`.
+//! Ideally `sys_common` would be split into two and the dependencies
+//! between them all would form a dag, facilitating the extraction of
+//! `std::sys` from the standard library.
+
+#![allow(missing_docs)]
+
+pub mod io;
+
+// common error constructors
+
+/// A trait for viewing representations from std types
+#[doc(hidden)]
+pub trait AsInner<Inner: ?Sized> {
+ fn as_inner(&self) -> &Inner;
+}
+
+/// A trait for viewing representations from std types
+#[doc(hidden)]
+pub trait AsInnerMut<Inner: ?Sized> {
+ fn as_inner_mut(&mut self) -> &mut Inner;
+}
+
+/// A trait for extracting representations from std types
+#[doc(hidden)]
+pub trait IntoInner<Inner> {
+ fn into_inner(self) -> Inner;
+}
+
+/// A trait for creating std types from internal representations
+#[doc(hidden)]
+pub trait FromInner<Inner> {
+ fn from_inner(inner: Inner) -> Self;
+}
+
+macro_rules! rtabort {
+ ($($t:tt)*) => (::sys_common::util::abort(format_args!($($t)*)))
+}
+
+// Computes (value*numer)/denom without overflow, as long as both
+// (numer*denom) and the overall result fit into i64 (which is the case
+// for our time conversions).
+#[allow(dead_code)] // not used on all platforms
+pub fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
+ let q = value / denom;
+ let r = value % denom;
+ // Decompose value as (value/denom*denom + value%denom),
+ // substitute into (value*numer)/denom and simplify.
+ // r < denom, so (denom*numer) is the upper bound of (r*numer)
+ q * numer + r * numer / denom
+}
+
+#[test]
+fn test_muldiv() {
+ assert_eq!(mul_div_u64( 1_000_000_000_001, 1_000_000_000, 1_000_000),
+ 1_000_000_000_001_000);
+}