From b358934c5c46101ccbb1a7c087086704e42ebc7e Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Mon, 31 Oct 2016 00:11:02 -0700 Subject: [PATCH] Keep track of individual patches instead of patched source versions --- .gitignore | 1 + Cargo.toml | 7 + README.md | 37 +- build-src.sh | 131 ++ ...309e4b0ea05ee905205278fb6d131341c41f.patch | 1534 +++++++++++++ ...bb879e6ef498ea04e08bd80688bf2fc4a183.patch | 1546 +++++++++++++ ...541afdd18423940e97c7a02b517b1f6d61be.patch | 1543 +++++++++++++ ...36872d7ea8bee91f8588e3c4a613c58c76cb.patch | 1465 ++++++++++++ ...949bf678abc1fae595e3f6eb59a5bf8a7564.patch | 1556 +++++++++++++ ...da70d33cead1398adfecce1a75e7a61e3daf.patch | 1543 +++++++++++++ ...9c627ebde2bbfa6021de146c502124da7975.patch | 1543 +++++++++++++ ...18199d69bcc801668bfbeea8190b2c73a939.patch | 1543 +++++++++++++ ...33385aa2ff150a5d6f83ecfe55afc7e19e68.patch | 1543 +++++++++++++ ...817119e479b0610685e3fc7a6ff21cde5abc.patch | 1543 +++++++++++++ ...e45988e81cb1a7bc9881aa7172d4f9caefd4.patch | 1534 +++++++++++++ ...332a27b020f5252238946fa9dccc3843d52a.patch | 1465 ++++++++++++ ...4e0e3f27d1917d11ee80de827698beb53891.patch | 1543 +++++++++++++ ...0cf8dcf411ce7e70ab2960639977d46d5b01.patch | 1551 +++++++++++++ ...120ad218b8d9c50e25a525dcff689c515776.patch | 1551 +++++++++++++ ...8783d91624261317316f996d8d2d09b7b6a4.patch | 1545 +++++++++++++ ...5c8dd891ec6eb0809b8438656d1b39c2e2f5.patch | 1543 +++++++++++++ ...c065b0e94b1e2ce7f14ab512475e79426ce4.patch | 1543 +++++++++++++ ...50f5d377a41dd5833e4621e9a14879647503.patch | 1614 ++++++++++++++ ...1d7c8c21aa860bda29c62207a6726837952b.patch | 1543 +++++++++++++ ...3eb369c9bdd0aef4917675e515f39f3b4a1e.patch | 1534 +++++++++++++ ...079a6f70577d02f35bb80d27eef7e2b18a4a.patch | 1464 ++++++++++++ ...593f42fafbac1ff3d827f6df96338b5b7d8b.patch | 1551 +++++++++++++ ...4e828c3dc3b7c417197990741b024f8c4ca3.patch | 1556 +++++++++++++ ...a6ad0845b76509c4f8967e8ca476471ab7e0.patch | 1545 +++++++++++++ ...c8b84969fbe52cae7c9fd61858fddc6e016b.patch | 1556 +++++++++++++ ...4c5d13f8819c72568f9675e84c1d17368c67.patch | 1561 +++++++++++++ ...91c0b959111aef19f3aa06b7f1743419470c.patch | 1614 ++++++++++++++ ...73f00fa07b9a8246b104396f9922e54bff16.patch | 1561 +++++++++++++ .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 314 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1780 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1105 --------- .../cursor.rs | 571 ----- .../error.rs | 480 ---- .../impls.rs | 288 --- .../memchr.rs | 319 --- .../mod.rs | 1875 ---------------- .../prelude.rs | 25 - .../util.rs | 206 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 314 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1834 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1146 ---------- .../cursor.rs | 594 ----- .../error.rs | 562 ----- .../impls.rs | 285 --- .../memchr.rs | 143 -- .../mod.rs | 1979 ----------------- .../prelude.rs | 24 - .../util.rs | 215 -- .../buffered.rs | 1104 --------- .../cursor.rs | 571 ----- .../error.rs | 480 ---- .../impls.rs | 288 --- .../memchr.rs | 319 --- .../mod.rs | 1912 ---------------- .../prelude.rs | 25 - .../util.rs | 206 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 314 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1834 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 313 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1845 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 314 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1846 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1106 --------- .../cursor.rs | 572 ----- .../error.rs | 313 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1845 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 314 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1834 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 314 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1780 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1146 ---------- .../cursor.rs | 594 ----- .../error.rs | 562 ----- .../impls.rs | 285 --- .../memchr.rs | 143 -- .../mod.rs | 1979 ----------------- .../prelude.rs | 24 - .../util.rs | 215 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 313 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1845 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1106 --------- .../cursor.rs | 572 ----- .../error.rs | 337 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1852 --------------- .../prelude.rs | 25 - .../util.rs | 208 -- .../buffered.rs | 1106 --------- .../cursor.rs | 572 ----- .../error.rs | 337 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1845 --------------- .../prelude.rs | 25 - .../util.rs | 208 -- .../buffered.rs | 1105 --------- .../cursor.rs | 571 ----- .../error.rs | 479 ---- .../impls.rs | 288 --- .../memchr.rs | 297 --- .../mod.rs | 1875 ---------------- .../prelude.rs | 25 - .../util.rs | 206 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 314 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1845 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 314 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1846 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 317 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1896 ---------------- .../prelude.rs | 25 - .../util.rs | 218 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 313 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1845 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1106 --------- .../cursor.rs | 570 ----- .../error.rs | 314 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1780 --------------- .../prelude.rs | 25 - .../util.rs | 191 -- .../buffered.rs | 1105 --------- .../cursor.rs | 571 ----- .../error.rs | 480 ---- .../impls.rs | 288 --- .../memchr.rs | 232 -- .../mod.rs | 1910 ---------------- .../prelude.rs | 25 - .../util.rs | 206 -- .../buffered.rs | 1106 --------- .../cursor.rs | 572 ----- .../error.rs | 337 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1845 --------------- .../prelude.rs | 25 - .../util.rs | 208 -- .../buffered.rs | 1104 --------- .../cursor.rs | 571 ----- .../error.rs | 480 ---- .../impls.rs | 288 --- .../memchr.rs | 319 --- .../mod.rs | 1901 ---------------- .../prelude.rs | 25 - .../util.rs | 206 -- .../buffered.rs | 1106 --------- .../cursor.rs | 572 ----- .../error.rs | 313 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1845 --------------- .../prelude.rs | 25 - .../util.rs | 208 -- .../buffered.rs | 1104 --------- .../cursor.rs | 571 ----- .../error.rs | 480 ---- .../impls.rs | 288 --- .../memchr.rs | 319 --- .../mod.rs | 1901 ---------------- .../prelude.rs | 25 - .../util.rs | 206 -- .../buffered.rs | 1106 --------- .../cursor.rs | 572 ----- .../error.rs | 480 ---- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1876 ---------------- .../prelude.rs | 25 - .../util.rs | 208 -- .../buffered.rs | 1076 --------- .../cursor.rs | 570 ----- .../error.rs | 317 --- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1896 ---------------- .../prelude.rs | 25 - .../util.rs | 218 -- .../buffered.rs | 1106 --------- .../cursor.rs | 572 ----- .../error.rs | 480 ---- .../impls.rs | 289 --- .../memchr.rs | 297 --- .../mod.rs | 1876 ---------------- .../prelude.rs | 25 - .../util.rs | 208 -- sync.sh | 18 - 266 files changed, 44874 insertions(+), 137038 deletions(-) create mode 100755 build-src.sh create mode 100644 patches/0f02309e4b0ea05ee905205278fb6d131341c41f.patch create mode 100644 patches/117cbb879e6ef498ea04e08bd80688bf2fc4a183.patch create mode 100644 patches/161c541afdd18423940e97c7a02b517b1f6d61be.patch create mode 100644 patches/1f9036872d7ea8bee91f8588e3c4a613c58c76cb.patch create mode 100644 patches/26f9949bf678abc1fae595e3f6eb59a5bf8a7564.patch create mode 100644 patches/552eda70d33cead1398adfecce1a75e7a61e3daf.patch create mode 100644 patches/62b19c627ebde2bbfa6021de146c502124da7975.patch create mode 100644 patches/78ab18199d69bcc801668bfbeea8190b2c73a939.patch create mode 100644 patches/80d733385aa2ff150a5d6f83ecfe55afc7e19e68.patch create mode 100644 patches/8128817119e479b0610685e3fc7a6ff21cde5abc.patch create mode 100644 patches/87aee45988e81cb1a7bc9881aa7172d4f9caefd4.patch create mode 100644 patches/8d06332a27b020f5252238946fa9dccc3843d52a.patch create mode 100644 patches/8e414e0e3f27d1917d11ee80de827698beb53891.patch create mode 100644 patches/92400cf8dcf411ce7e70ab2960639977d46d5b01.patch create mode 100644 patches/98e3120ad218b8d9c50e25a525dcff689c515776.patch create mode 100644 patches/9a2c8783d91624261317316f996d8d2d09b7b6a4.patch create mode 100644 patches/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5.patch create mode 100644 patches/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4.patch create mode 100644 patches/c1fb50f5d377a41dd5833e4621e9a14879647503.patch create mode 100644 patches/cae91d7c8c21aa860bda29c62207a6726837952b.patch create mode 100644 patches/ce943eb369c9bdd0aef4917675e515f39f3b4a1e.patch create mode 100644 patches/d311079a6f70577d02f35bb80d27eef7e2b18a4a.patch create mode 100644 patches/d40c593f42fafbac1ff3d827f6df96338b5b7d8b.patch create mode 100644 patches/d6aa4e828c3dc3b7c417197990741b024f8c4ca3.patch create mode 100644 patches/dd56a6ad0845b76509c4f8967e8ca476471ab7e0.patch create mode 100644 patches/e107c8b84969fbe52cae7c9fd61858fddc6e016b.patch create mode 100644 patches/e6cc4c5d13f8819c72568f9675e84c1d17368c67.patch create mode 100644 patches/f1e191c0b959111aef19f3aa06b7f1743419470c.patch create mode 100644 patches/fda473f00fa07b9a8246b104396f9922e54bff16.patch delete mode 100644 src/0f02309e4b0ea05ee905205278fb6d131341c41f/buffered.rs delete mode 100644 src/0f02309e4b0ea05ee905205278fb6d131341c41f/cursor.rs delete mode 100644 src/0f02309e4b0ea05ee905205278fb6d131341c41f/error.rs delete mode 100644 src/0f02309e4b0ea05ee905205278fb6d131341c41f/impls.rs delete mode 100644 src/0f02309e4b0ea05ee905205278fb6d131341c41f/memchr.rs delete mode 100644 src/0f02309e4b0ea05ee905205278fb6d131341c41f/mod.rs delete mode 100644 src/0f02309e4b0ea05ee905205278fb6d131341c41f/prelude.rs delete mode 100644 src/0f02309e4b0ea05ee905205278fb6d131341c41f/util.rs delete mode 100644 src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/buffered.rs delete mode 100644 src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/cursor.rs delete mode 100644 src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/error.rs delete mode 100644 src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/impls.rs delete mode 100644 src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/memchr.rs delete mode 100644 src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/mod.rs delete mode 100644 src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/prelude.rs delete mode 100644 src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/util.rs delete mode 100644 src/161c541afdd18423940e97c7a02b517b1f6d61be/buffered.rs delete mode 100644 src/161c541afdd18423940e97c7a02b517b1f6d61be/cursor.rs delete mode 100644 src/161c541afdd18423940e97c7a02b517b1f6d61be/error.rs delete mode 100644 src/161c541afdd18423940e97c7a02b517b1f6d61be/impls.rs delete mode 100644 src/161c541afdd18423940e97c7a02b517b1f6d61be/memchr.rs delete mode 100644 src/161c541afdd18423940e97c7a02b517b1f6d61be/mod.rs delete mode 100644 src/161c541afdd18423940e97c7a02b517b1f6d61be/prelude.rs delete mode 100644 src/161c541afdd18423940e97c7a02b517b1f6d61be/util.rs delete mode 100644 src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/buffered.rs delete mode 100644 src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/cursor.rs delete mode 100644 src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/error.rs delete mode 100644 src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/impls.rs delete mode 100644 src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/memchr.rs delete mode 100644 src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/mod.rs delete mode 100644 src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/prelude.rs delete mode 100644 src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/util.rs delete mode 100644 src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/buffered.rs delete mode 100644 src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/cursor.rs delete mode 100644 src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/error.rs delete mode 100644 src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/impls.rs delete mode 100644 src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/memchr.rs delete mode 100644 src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/mod.rs delete mode 100644 src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/prelude.rs delete mode 100644 src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/util.rs delete mode 100644 src/552eda70d33cead1398adfecce1a75e7a61e3daf/buffered.rs delete mode 100644 src/552eda70d33cead1398adfecce1a75e7a61e3daf/cursor.rs delete mode 100644 src/552eda70d33cead1398adfecce1a75e7a61e3daf/error.rs delete mode 100644 src/552eda70d33cead1398adfecce1a75e7a61e3daf/impls.rs delete mode 100644 src/552eda70d33cead1398adfecce1a75e7a61e3daf/memchr.rs delete mode 100644 src/552eda70d33cead1398adfecce1a75e7a61e3daf/mod.rs delete mode 100644 src/552eda70d33cead1398adfecce1a75e7a61e3daf/prelude.rs delete mode 100644 src/552eda70d33cead1398adfecce1a75e7a61e3daf/util.rs delete mode 100644 src/62b19c627ebde2bbfa6021de146c502124da7975/buffered.rs delete mode 100644 src/62b19c627ebde2bbfa6021de146c502124da7975/cursor.rs delete mode 100644 src/62b19c627ebde2bbfa6021de146c502124da7975/error.rs delete mode 100644 src/62b19c627ebde2bbfa6021de146c502124da7975/impls.rs delete mode 100644 src/62b19c627ebde2bbfa6021de146c502124da7975/memchr.rs delete mode 100644 src/62b19c627ebde2bbfa6021de146c502124da7975/mod.rs delete mode 100644 src/62b19c627ebde2bbfa6021de146c502124da7975/prelude.rs delete mode 100644 src/62b19c627ebde2bbfa6021de146c502124da7975/util.rs delete mode 100644 src/78ab18199d69bcc801668bfbeea8190b2c73a939/buffered.rs delete mode 100644 src/78ab18199d69bcc801668bfbeea8190b2c73a939/cursor.rs delete mode 100644 src/78ab18199d69bcc801668bfbeea8190b2c73a939/error.rs delete mode 100644 src/78ab18199d69bcc801668bfbeea8190b2c73a939/impls.rs delete mode 100644 src/78ab18199d69bcc801668bfbeea8190b2c73a939/memchr.rs delete mode 100644 src/78ab18199d69bcc801668bfbeea8190b2c73a939/mod.rs delete mode 100644 src/78ab18199d69bcc801668bfbeea8190b2c73a939/prelude.rs delete mode 100644 src/78ab18199d69bcc801668bfbeea8190b2c73a939/util.rs delete mode 100644 src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/buffered.rs delete mode 100644 src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/cursor.rs delete mode 100644 src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/error.rs delete mode 100644 src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/impls.rs delete mode 100644 src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/memchr.rs delete mode 100644 src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/mod.rs delete mode 100644 src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/prelude.rs delete mode 100644 src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/util.rs delete mode 100644 src/8128817119e479b0610685e3fc7a6ff21cde5abc/buffered.rs delete mode 100644 src/8128817119e479b0610685e3fc7a6ff21cde5abc/cursor.rs delete mode 100644 src/8128817119e479b0610685e3fc7a6ff21cde5abc/error.rs delete mode 100644 src/8128817119e479b0610685e3fc7a6ff21cde5abc/impls.rs delete mode 100644 src/8128817119e479b0610685e3fc7a6ff21cde5abc/memchr.rs delete mode 100644 src/8128817119e479b0610685e3fc7a6ff21cde5abc/mod.rs delete mode 100644 src/8128817119e479b0610685e3fc7a6ff21cde5abc/prelude.rs delete mode 100644 src/8128817119e479b0610685e3fc7a6ff21cde5abc/util.rs delete mode 100644 src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/buffered.rs delete mode 100644 src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/cursor.rs delete mode 100644 src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/error.rs delete mode 100644 src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/impls.rs delete mode 100644 src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/memchr.rs delete mode 100644 src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/mod.rs delete mode 100644 src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/prelude.rs delete mode 100644 src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/util.rs delete mode 100644 src/8d06332a27b020f5252238946fa9dccc3843d52a/buffered.rs delete mode 100644 src/8d06332a27b020f5252238946fa9dccc3843d52a/cursor.rs delete mode 100644 src/8d06332a27b020f5252238946fa9dccc3843d52a/error.rs delete mode 100644 src/8d06332a27b020f5252238946fa9dccc3843d52a/impls.rs delete mode 100644 src/8d06332a27b020f5252238946fa9dccc3843d52a/memchr.rs delete mode 100644 src/8d06332a27b020f5252238946fa9dccc3843d52a/mod.rs delete mode 100644 src/8d06332a27b020f5252238946fa9dccc3843d52a/prelude.rs delete mode 100644 src/8d06332a27b020f5252238946fa9dccc3843d52a/util.rs delete mode 100644 src/8e414e0e3f27d1917d11ee80de827698beb53891/buffered.rs delete mode 100644 src/8e414e0e3f27d1917d11ee80de827698beb53891/cursor.rs delete mode 100644 src/8e414e0e3f27d1917d11ee80de827698beb53891/error.rs delete mode 100644 src/8e414e0e3f27d1917d11ee80de827698beb53891/impls.rs delete mode 100644 src/8e414e0e3f27d1917d11ee80de827698beb53891/memchr.rs delete mode 100644 src/8e414e0e3f27d1917d11ee80de827698beb53891/mod.rs delete mode 100644 src/8e414e0e3f27d1917d11ee80de827698beb53891/prelude.rs delete mode 100644 src/8e414e0e3f27d1917d11ee80de827698beb53891/util.rs delete mode 100644 src/92400cf8dcf411ce7e70ab2960639977d46d5b01/buffered.rs delete mode 100644 src/92400cf8dcf411ce7e70ab2960639977d46d5b01/cursor.rs delete mode 100644 src/92400cf8dcf411ce7e70ab2960639977d46d5b01/error.rs delete mode 100644 src/92400cf8dcf411ce7e70ab2960639977d46d5b01/impls.rs delete mode 100644 src/92400cf8dcf411ce7e70ab2960639977d46d5b01/memchr.rs delete mode 100644 src/92400cf8dcf411ce7e70ab2960639977d46d5b01/mod.rs delete mode 100644 src/92400cf8dcf411ce7e70ab2960639977d46d5b01/prelude.rs delete mode 100644 src/92400cf8dcf411ce7e70ab2960639977d46d5b01/util.rs delete mode 100644 src/98e3120ad218b8d9c50e25a525dcff689c515776/buffered.rs delete mode 100644 src/98e3120ad218b8d9c50e25a525dcff689c515776/cursor.rs delete mode 100644 src/98e3120ad218b8d9c50e25a525dcff689c515776/error.rs delete mode 100644 src/98e3120ad218b8d9c50e25a525dcff689c515776/impls.rs delete mode 100644 src/98e3120ad218b8d9c50e25a525dcff689c515776/memchr.rs delete mode 100644 src/98e3120ad218b8d9c50e25a525dcff689c515776/mod.rs delete mode 100644 src/98e3120ad218b8d9c50e25a525dcff689c515776/prelude.rs delete mode 100644 src/98e3120ad218b8d9c50e25a525dcff689c515776/util.rs delete mode 100644 src/9a2c8783d91624261317316f996d8d2d09b7b6a4/buffered.rs delete mode 100644 src/9a2c8783d91624261317316f996d8d2d09b7b6a4/cursor.rs delete mode 100644 src/9a2c8783d91624261317316f996d8d2d09b7b6a4/error.rs delete mode 100644 src/9a2c8783d91624261317316f996d8d2d09b7b6a4/impls.rs delete mode 100644 src/9a2c8783d91624261317316f996d8d2d09b7b6a4/memchr.rs delete mode 100644 src/9a2c8783d91624261317316f996d8d2d09b7b6a4/mod.rs delete mode 100644 src/9a2c8783d91624261317316f996d8d2d09b7b6a4/prelude.rs delete mode 100644 src/9a2c8783d91624261317316f996d8d2d09b7b6a4/util.rs delete mode 100644 src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/buffered.rs delete mode 100644 src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/cursor.rs delete mode 100644 src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/error.rs delete mode 100644 src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/impls.rs delete mode 100644 src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/memchr.rs delete mode 100644 src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/mod.rs delete mode 100644 src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/prelude.rs delete mode 100644 src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/util.rs delete mode 100644 src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/buffered.rs delete mode 100644 src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/cursor.rs delete mode 100644 src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/error.rs delete mode 100644 src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/impls.rs delete mode 100644 src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/memchr.rs delete mode 100644 src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/mod.rs delete mode 100644 src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/prelude.rs delete mode 100644 src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/util.rs delete mode 100644 src/c1fb50f5d377a41dd5833e4621e9a14879647503/buffered.rs delete mode 100644 src/c1fb50f5d377a41dd5833e4621e9a14879647503/cursor.rs delete mode 100644 src/c1fb50f5d377a41dd5833e4621e9a14879647503/error.rs delete mode 100644 src/c1fb50f5d377a41dd5833e4621e9a14879647503/impls.rs delete mode 100644 src/c1fb50f5d377a41dd5833e4621e9a14879647503/memchr.rs delete mode 100644 src/c1fb50f5d377a41dd5833e4621e9a14879647503/mod.rs delete mode 100644 src/c1fb50f5d377a41dd5833e4621e9a14879647503/prelude.rs delete mode 100644 src/c1fb50f5d377a41dd5833e4621e9a14879647503/util.rs delete mode 100644 src/cae91d7c8c21aa860bda29c62207a6726837952b/buffered.rs delete mode 100644 src/cae91d7c8c21aa860bda29c62207a6726837952b/cursor.rs delete mode 100644 src/cae91d7c8c21aa860bda29c62207a6726837952b/error.rs delete mode 100644 src/cae91d7c8c21aa860bda29c62207a6726837952b/impls.rs delete mode 100644 src/cae91d7c8c21aa860bda29c62207a6726837952b/memchr.rs delete mode 100644 src/cae91d7c8c21aa860bda29c62207a6726837952b/mod.rs delete mode 100644 src/cae91d7c8c21aa860bda29c62207a6726837952b/prelude.rs delete mode 100644 src/cae91d7c8c21aa860bda29c62207a6726837952b/util.rs delete mode 100644 src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/buffered.rs delete mode 100644 src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/cursor.rs delete mode 100644 src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/error.rs delete mode 100644 src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/impls.rs delete mode 100644 src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/memchr.rs delete mode 100644 src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/mod.rs delete mode 100644 src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/prelude.rs delete mode 100644 src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/util.rs delete mode 100644 src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/buffered.rs delete mode 100644 src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/cursor.rs delete mode 100644 src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/error.rs delete mode 100644 src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/impls.rs delete mode 100644 src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/memchr.rs delete mode 100644 src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/mod.rs delete mode 100644 src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/prelude.rs delete mode 100644 src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/util.rs delete mode 100644 src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/buffered.rs delete mode 100644 src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/cursor.rs delete mode 100644 src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/error.rs delete mode 100644 src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/impls.rs delete mode 100644 src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/memchr.rs delete mode 100644 src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/mod.rs delete mode 100644 src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/prelude.rs delete mode 100644 src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/util.rs delete mode 100644 src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/buffered.rs delete mode 100644 src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/cursor.rs delete mode 100644 src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/error.rs delete mode 100644 src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/impls.rs delete mode 100644 src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/memchr.rs delete mode 100644 src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/mod.rs delete mode 100644 src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/prelude.rs delete mode 100644 src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/util.rs delete mode 100644 src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/buffered.rs delete mode 100644 src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/cursor.rs delete mode 100644 src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/error.rs delete mode 100644 src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/impls.rs delete mode 100644 src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/memchr.rs delete mode 100644 src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/mod.rs delete mode 100644 src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/prelude.rs delete mode 100644 src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/util.rs delete mode 100644 src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/buffered.rs delete mode 100644 src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/cursor.rs delete mode 100644 src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/error.rs delete mode 100644 src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/impls.rs delete mode 100644 src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/memchr.rs delete mode 100644 src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/mod.rs delete mode 100644 src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/prelude.rs delete mode 100644 src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/util.rs delete mode 100644 src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/buffered.rs delete mode 100644 src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/cursor.rs delete mode 100644 src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/error.rs delete mode 100644 src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/impls.rs delete mode 100644 src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/memchr.rs delete mode 100644 src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/mod.rs delete mode 100644 src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/prelude.rs delete mode 100644 src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/util.rs delete mode 100644 src/f1e191c0b959111aef19f3aa06b7f1743419470c/buffered.rs delete mode 100644 src/f1e191c0b959111aef19f3aa06b7f1743419470c/cursor.rs delete mode 100644 src/f1e191c0b959111aef19f3aa06b7f1743419470c/error.rs delete mode 100644 src/f1e191c0b959111aef19f3aa06b7f1743419470c/impls.rs delete mode 100644 src/f1e191c0b959111aef19f3aa06b7f1743419470c/memchr.rs delete mode 100644 src/f1e191c0b959111aef19f3aa06b7f1743419470c/mod.rs delete mode 100644 src/f1e191c0b959111aef19f3aa06b7f1743419470c/prelude.rs delete mode 100644 src/f1e191c0b959111aef19f3aa06b7f1743419470c/util.rs delete mode 100644 src/fda473f00fa07b9a8246b104396f9922e54bff16/buffered.rs delete mode 100644 src/fda473f00fa07b9a8246b104396f9922e54bff16/cursor.rs delete mode 100644 src/fda473f00fa07b9a8246b104396f9922e54bff16/error.rs delete mode 100644 src/fda473f00fa07b9a8246b104396f9922e54bff16/impls.rs delete mode 100644 src/fda473f00fa07b9a8246b104396f9922e54bff16/memchr.rs delete mode 100644 src/fda473f00fa07b9a8246b104396f9922e54bff16/mod.rs delete mode 100644 src/fda473f00fa07b9a8246b104396f9922e54bff16/prelude.rs delete mode 100644 src/fda473f00fa07b9a8246b104396f9922e54bff16/util.rs delete mode 100755 sync.sh diff --git a/.gitignore b/.gitignore index a9d37c5..4f357b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target Cargo.lock +src/???????????????????????????????????????? diff --git a/Cargo.toml b/Cargo.toml index 6ee0a6d..fd5a39b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,13 @@ selected automatically by the build script. repository = "https://github.com/jethrogb/rust-core_io" documentation = "https://doc.rust-lang.org/nightly/std/io/index.html" keywords = ["core", "no_std", "io", "read", "write"] +include = [ + "build.rs", + "Cargo.toml", + "LICENSE-*", + "mapping.rs", + "src/**/*.rs", +] build = "build.rs" diff --git a/README.md b/README.md index 9b26509..f53be9a 100644 --- a/README.md +++ b/README.md @@ -8,43 +8,10 @@ First, make sure the commit you want to add is fetch in the git tree at `/your/rust/dir/.git`. Then, import the right source files: ``` -$ echo FULL_COMMIT_ID ...|GIT_DIR=/your/rust/dir/.git ./sync.sh +$ echo FULL_COMMIT_ID ...|GIT_DIR=/your/rust/dir/.git ./build-src.sh ``` Instead of echoing in the commit IDs, you might pipe in `rustc-commit-db list-valid`. -Now look at the changes with `git status`. If nothing changed then the commit -you tried to add was already there. Otherwise commit all changes and new files -now. If only `mapping.rs` changed, the I/O code has not changed for this -particular commit. If a directory in `src/` was added, after committing, `cd` -into it to apply the patch. - -Find out which previously-existing commit is closest to the new one and search -this git repository for a commit with the description `Patch COMMIT for core`. -For example, if you're adding dd56a6ad0845b76509c4f8967e8ca476471ab7e0, the -best closest commit is 80d733385aa2ff150a5d6f83ecfe55afc7e19e68. - -``` -$ git log --pretty=oneline --grep=80d733385aa2ff150a5d6f83ecfe55afc7e19e68 -92fc0ad81c432b5fa3e848fc1892815ca2f55100 Patch 80d733385aa2ff150a5d6f83ecfe55afc7e19e68 for core -``` - -The commit ID at the start of the line is the patch we'll try to apply: - -```sh -$ git show 92fc0ad81c432b5fa3e848fc1892815ca2f55100|patch -p3 -$ cargo build -``` - -Now, fix any errors `cargo` reports. If `patch` also reported errors, you may -look at the rejects for inspiration ;). - -Finally, commit this new version: - -``` -$ git commit -m "Patch dd56a6ad0845b76509c4f8967e8ca476471ab7e0 for core" . -``` - -Do not commit any files in different directories, this will break the patching -scheme. +The build-src script will prompt you to create patches for new commits. diff --git a/build-src.sh b/build-src.sh new file mode 100755 index 0000000..b0124d0 --- /dev/null +++ b/build-src.sh @@ -0,0 +1,131 @@ +#!/bin/bash +# Recommended command-line: +# +# commit-db.rb list-valid nightly|GIT_DIR=/your/rust/dir/.git sync.sh + +git_file_exists() { + [ "$(git ls-tree --name-only $IO_COMMIT -- $1)" = "$1" ] +} + +git_extract() { + slashes=${1//[^\/]/} + git archive $IO_COMMIT $1|tar xf - -C src/$IO_COMMIT --strip-components=${#slashes} +} + +git_commits_ordered() { + format=$1 + shift + if [ $# -ge 1 ]; then + git log --topo-order --no-walk=sorted --date=iso-local --pretty=format:$format "$@" + fi +} + +echo_lines() { + for i in "$@"; do + echo $i + done +} + +get_io_commits() { + for COMPILER_COMMIT in $COMPILER_COMMITS; do + IO_COMMIT=$(git log -n1 --pretty=format:%H $COMPILER_COMMIT -- src/libstd/io) + if ! grep -q $COMPILER_COMMIT mapping.rs; then + echo "-Mapping(\"$COMPILER_COMMIT\",\"$IO_COMMIT\")" >> mapping.rs + fi + echo $IO_COMMIT + done +} + +get_patch_commits() { + find $PATCH_DIR -type f -printf %f\\n|cut -d. -f1 +} + +prepare_version() { + mkdir src/$IO_COMMIT + git_extract src/libstd/io/ + if git_file_exists src/libstd/sys/common/memchr.rs; then + git_extract src/libstd/sys/common/memchr.rs + else + git_extract src/libstd/memchr.rs + fi + rm -f src/$IO_COMMIT/stdio.rs src/$IO_COMMIT/lazy.rs +} + +bold_arrow() { + echo -ne '\e[1;36m==> \e[0m' +} + +prompt_changes() { + local MAIN_GIT_DIR="$GIT_DIR" + local GIT_DIR=./.git CORE_IO_COMMIT=$IO_COMMIT + git init > /dev/null + git add . + git commit -a -m "rust src import" > /dev/null + export CORE_IO_COMMIT + + bold_arrow; echo 'No patch found for' $IO_COMMIT + bold_arrow; echo 'Nearby commit(s) with patches:' + echo + GIT_DIR="$MAIN_GIT_DIR" git_commits_ordered '%H %cd' $(get_patch_commits) $IO_COMMIT | \ + grep --color=always -1 $IO_COMMIT | sed /$IO_COMMIT/'s/$/ <=== your commit/' + echo + bold_arrow; echo -e "Try applying one of those using: \e[1;36mpatch -p1 < ../../patches/COMMIT.patch\e[0m" + bold_arrow; echo -e "Remember to test your changes with: \e[1;36mcargo build\e[0m" + bold_arrow; echo -e "Make your changes now (\e[1;36mctrl-D\e[0m when finished)" + bash <> /dev/stderr + while git diff --exit-code > /dev/null; do + bold_arrow; echo "No changes were made" + while true; do + bold_arrow; echo -n "(T)ry again or (A)bort? " + read answer <> /dev/stderr + case "$answer" in + [tT]) + break + ;; + [aA]) + bold_arrow; echo "Aborting..." + exit 1 + ;; + esac + done + bash <> /dev/stderr + done + bold_arrow; echo "Saving changes as $IO_COMMIT.patch" + git clean -f -x + git diff > ../../patches/$IO_COMMIT.patch + rm -rf .git +} + +if [ ! -t 1 ] || [ ! -t 2 ]; then + echo "==> /dev/stdout or /dev/stderr is not attached to a terminal!" + echo "==> This script must be run interactively." + exit 1 +fi + +cd "$(dirname "$0")" +PATCH_DIR="$PWD/patches" +COMPILER_COMMITS=$(cat) +IO_COMMITS=$(get_io_commits|sort -u) +PATCH_COMMITS=$(get_patch_commits|sort -u) +NEW_COMMITS=$(comm -2 -3 <(echo_lines $IO_COMMITS) <(echo_lines $PATCH_COMMITS)) +OLD_COMMITS=$(comm -1 -2 <(echo_lines $IO_COMMITS) <(echo_lines $PATCH_COMMITS)) + +find src -mindepth 1 -type d -prune -exec rm -rf {} \; + +for IO_COMMIT in $OLD_COMMITS $(git_commits_ordered %H $NEW_COMMITS|tac); do + if ! [ -d src/$IO_COMMIT ]; then + prepare_version + + if [ -f patches/$IO_COMMIT.patch ]; then + patch -s -p1 -d src/$IO_COMMIT < patches/$IO_COMMIT.patch + else + cd src/$IO_COMMIT + prompt_changes + cd ../.. + fi + fi +done + +chmod 000 .git +cargo package +chmod 755 .git diff --git a/patches/0f02309e4b0ea05ee905205278fb6d131341c41f.patch b/patches/0f02309e4b0ea05ee905205278fb6d131341c41f.patch new file mode 100644 index 0000000..5270830 --- /dev/null +++ b/patches/0f02309e4b0ea05ee905205278fb6d131341c41f.patch @@ -0,0 +1,1534 @@ +diff --git a/buffered.rs b/buffered.rs +index 632ef3d..dd63ebe 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index 9a605fc..d3d0814 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 28492b3..924d626 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -248,49 +248,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 64 * 1024; + +@@ -312,6 +295,7 @@ const DEFAULT_BUF_SIZE: usize = 64 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -343,6 +327,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -425,7 +410,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -475,7 +459,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -517,7 +500,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -555,7 +538,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -616,7 +599,6 @@ pub trait Read { + /// # 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) { +@@ -668,7 +650,6 @@ pub trait Read { + /// # 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. +@@ -698,7 +679,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -735,10 +715,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -773,7 +749,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -807,7 +782,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -843,7 +817,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -883,7 +856,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -909,7 +881,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -936,7 +907,6 @@ pub trait Write { + /// # 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) { +@@ -988,7 +958,6 @@ pub trait Write { + /// # 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 +@@ -1044,7 +1013,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1074,7 +1042,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1088,35 +1055,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1196,7 +1159,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1241,7 +1204,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1263,7 +1225,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1304,7 +1265,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1352,7 +1312,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1385,7 +1344,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1410,7 +1368,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1422,14 +1379,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1448,7 +1403,6 @@ impl Read for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1462,11 +1416,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1481,7 +1433,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + let buf = self.inner.fill_buf()?; +@@ -1503,12 +1455,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1528,8 +1478,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1537,8 +1485,6 @@ pub struct Chars { + /// 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. +@@ -1548,8 +1494,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1581,25 +1525,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1618,13 +1543,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1649,12 +1574,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/117cbb879e6ef498ea04e08bd80688bf2fc4a183.patch b/patches/117cbb879e6ef498ea04e08bd80688bf2fc4a183.patch new file mode 100644 index 0000000..8bfc724 --- /dev/null +++ b/patches/117cbb879e6ef498ea04e08bd80688bf2fc4a183.patch @@ -0,0 +1,1546 @@ +diff --git a/buffered.rs b/buffered.rs +index a26a932..83ea558 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,14 +10,13 @@ + + //! Buffering wrappers for I/O traits + ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -43,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -66,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -87,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -115,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -136,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -157,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -179,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -196,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -206,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -295,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -330,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -344,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -362,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -411,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -429,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -447,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -456,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -477,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -487,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -497,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -536,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -569,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -640,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -660,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -681,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -702,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -725,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -747,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -755,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -774,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 1b836b7..c77f287 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,9 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -72,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -91,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -109,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -125,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -144,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -166,7 +162,6 @@ impl Cursor { + /// 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. +@@ -186,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -209,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -218,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -227,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -239,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index ddf0030..748b830 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,10 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use error; +-use fmt; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -43,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -55,20 +59,22 @@ pub type Result = result::Result; + /// + /// [`ErrorKind`]: enum.ErrorKind.html + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -80,47 +86,34 @@ struct Custom { + /// + /// [`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. + /// +@@ -130,10 +123,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -141,15 +132,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -158,15 +146,10 @@ pub enum ErrorKind { + /// 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, + } +@@ -190,14 +173,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -206,24 +188,6 @@ impl 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 +@@ -249,7 +213,6 @@ impl Error { + /// 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) } + } +@@ -280,7 +243,6 @@ impl Error { + /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -313,11 +275,10 @@ impl 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)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -383,11 +344,10 @@ impl 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)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -416,8 +376,7 @@ impl Error { + /// print_error(Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -442,10 +401,9 @@ impl 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::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -455,63 +413,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index cd05e6b..d048379 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,26 +8,31 @@ + // 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; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -38,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -56,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -80,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -102,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -120,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -147,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -171,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -180,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -204,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 03f55f7..f767e0c 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 1053792..5acfe6a 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,42 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -304,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -335,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -417,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -467,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -509,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -547,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -608,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -660,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -690,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -727,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -765,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -799,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -835,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -875,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -901,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -928,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -980,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1036,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1066,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1082,7 +1056,6 @@ pub trait Seek { + /// 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; + } + +@@ -1092,29 +1065,26 @@ pub trait Seek { + /// + /// [`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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1194,7 +1164,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1239,7 +1209,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1261,7 +1230,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1302,7 +1270,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1350,7 +1317,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1383,7 +1349,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1408,7 +1373,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1420,14 +1384,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1440,7 +1402,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1467,7 +1429,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1499,11 +1460,9 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1518,7 +1477,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1557,12 +1516,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1577,8 +1534,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1586,8 +1541,6 @@ pub struct Chars { + /// 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. +@@ -1597,8 +1550,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1630,25 +1581,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1667,13 +1599,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1698,12 +1630,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2c68802..93bcfc3 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/161c541afdd18423940e97c7a02b517b1f6d61be.patch b/patches/161c541afdd18423940e97c7a02b517b1f6d61be.patch new file mode 100644 index 0000000..fb2ae22 --- /dev/null +++ b/patches/161c541afdd18423940e97c7a02b517b1f6d61be.patch @@ -0,0 +1,1543 @@ +diff --git a/buffered.rs b/buffered.rs +index 632ef3d..dd63ebe 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index 9a605fc..d3d0814 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 0f988c7..d6cd245 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -248,49 +248,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 64 * 1024; + +@@ -312,6 +295,7 @@ const DEFAULT_BUF_SIZE: usize = 64 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -343,6 +327,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -425,7 +410,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -475,7 +459,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -517,7 +500,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -555,7 +538,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -616,7 +599,6 @@ pub trait Read { + /// # 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) { +@@ -668,7 +650,6 @@ pub trait Read { + /// # 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. +@@ -698,7 +679,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -735,10 +715,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -773,7 +749,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -807,7 +782,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -843,7 +817,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -883,7 +856,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -909,7 +881,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -936,7 +907,6 @@ pub trait Write { + /// # 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) { +@@ -988,7 +958,6 @@ pub trait Write { + /// # 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 +@@ -1044,7 +1013,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1074,7 +1042,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1088,35 +1055,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1196,7 +1159,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1241,7 +1204,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1263,7 +1225,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1304,7 +1265,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1352,7 +1312,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1385,7 +1344,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1410,7 +1368,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1422,14 +1379,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1442,7 +1397,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1469,7 +1424,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1483,11 +1437,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1502,7 +1454,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + let buf = self.inner.fill_buf()?; +@@ -1524,12 +1476,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1549,8 +1499,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1558,8 +1506,6 @@ pub struct Chars { + /// 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. +@@ -1569,8 +1515,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1602,25 +1546,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1639,13 +1564,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1670,12 +1595,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/1f9036872d7ea8bee91f8588e3c4a613c58c76cb.patch b/patches/1f9036872d7ea8bee91f8588e3c4a613c58c76cb.patch new file mode 100644 index 0000000..d645761 --- /dev/null +++ b/patches/1f9036872d7ea8bee91f8588e3c4a613c58c76cb.patch @@ -0,0 +1,1465 @@ +diff --git a/buffered.rs b/buffered.rs +index 21a0cc1..27c5ac1 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,14 +10,13 @@ + + //! Buffering wrappers for I/O traits + ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -43,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -66,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -87,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -115,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -136,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -157,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -179,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -196,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -206,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -295,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -330,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -344,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -362,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -411,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -429,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -447,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -456,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -476,7 +457,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -486,7 +466,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -496,7 +475,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -535,7 +513,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -568,23 +545,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -639,7 +606,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -659,7 +625,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -680,7 +645,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -701,7 +665,6 @@ impl LineWriter { + /// # 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. +@@ -724,7 +687,6 @@ impl LineWriter { + /// # 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. +@@ -746,7 +708,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -754,7 +715,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -773,7 +733,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index ae0085f..5392880 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,11 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + ++use core::prelude::v1::*; + use io::prelude::*; + + use core::convert::TryInto; +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +74,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +92,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +109,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +124,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +142,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +163,6 @@ impl Cursor { + /// 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. +@@ -187,11 +182,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +203,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +211,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +219,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -240,7 +230,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos: usize = self.position().try_into().map_err(|_| { +@@ -270,7 +259,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index ddf0030..748b830 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,10 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use error; +-use fmt; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -43,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -55,20 +59,22 @@ pub type Result = result::Result; + /// + /// [`ErrorKind`]: enum.ErrorKind.html + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -80,47 +86,34 @@ struct Custom { + /// + /// [`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. + /// +@@ -130,10 +123,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -141,15 +132,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -158,15 +146,10 @@ pub enum ErrorKind { + /// 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, + } +@@ -190,14 +173,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -206,24 +188,6 @@ impl 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 +@@ -249,7 +213,6 @@ impl Error { + /// 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) } + } +@@ -280,7 +243,6 @@ impl Error { + /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -313,11 +275,10 @@ impl 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)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -383,11 +344,10 @@ impl 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)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -416,8 +376,7 @@ impl Error { + /// print_error(Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -442,10 +401,9 @@ impl 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::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -455,63 +413,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index cd05e6b..d048379 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,26 +8,31 @@ + // 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; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -38,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -56,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -80,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -102,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -120,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -147,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -171,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -180,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -204,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 3824a5f..312cf47 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,10 +11,12 @@ + // Original implementation taken from rust-memchr + // Copyright 2015 Andrew Gallant, bluss and Nicolas Koch + ++pub use self::fallback::{memchr,memrchr}; ++ + #[allow(dead_code)] + pub mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 7310564..05d91fc 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,42 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -304,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -335,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -417,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -467,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -509,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -547,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -608,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -660,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -690,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -727,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -765,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -799,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -835,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -875,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -901,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -928,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -980,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1036,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1066,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1082,7 +1056,6 @@ pub trait Seek { + /// 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; + } + +@@ -1092,29 +1065,26 @@ pub trait Seek { + /// + /// [`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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1194,7 +1164,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1239,7 +1209,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1261,7 +1230,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[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. +@@ -1300,7 +1268,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1346,7 +1313,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1379,7 +1345,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1404,7 +1369,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1416,14 +1380,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1436,7 +1398,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1463,7 +1425,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1495,7 +1456,6 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + + /// Consumes the `Take`, returning the wrapped reader. +@@ -1520,13 +1480,11 @@ impl Take { + /// # 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 Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1541,7 +1499,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1580,12 +1538,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1600,8 +1556,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1609,8 +1563,6 @@ pub struct Chars { + /// 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. +@@ -1620,8 +1572,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1653,25 +1603,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1690,13 +1621,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1721,12 +1652,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2c68802..93bcfc3 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/26f9949bf678abc1fae595e3f6eb59a5bf8a7564.patch b/patches/26f9949bf678abc1fae595e3f6eb59a5bf8a7564.patch new file mode 100644 index 0000000..6b57510 --- /dev/null +++ b/patches/26f9949bf678abc1fae595e3f6eb59a5bf8a7564.patch @@ -0,0 +1,1556 @@ +diff --git a/buffered.rs b/buffered.rs +index 4ff8c6a..fd4eb13 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,14 +10,13 @@ + + //! Buffering wrappers for I/O traits + ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -43,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -66,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -87,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -115,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -136,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -157,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -179,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -196,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -206,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -295,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -330,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -344,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -362,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -411,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -429,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -447,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -456,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -476,7 +457,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -486,7 +466,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -496,7 +475,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -535,7 +513,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -568,23 +545,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -639,7 +606,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -659,7 +625,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -680,7 +645,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -701,7 +665,6 @@ impl LineWriter { + /// # 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. +@@ -724,7 +687,6 @@ impl LineWriter { + /// # 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. +@@ -746,7 +708,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -754,7 +715,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -773,7 +733,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 1b836b7..c77f287 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,9 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -72,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -91,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -109,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -125,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -144,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -166,7 +162,6 @@ impl Cursor { + /// 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. +@@ -186,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -209,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -218,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -227,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -239,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index ddf0030..748b830 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,10 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use error; +-use fmt; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -43,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -55,20 +59,22 @@ pub type Result = result::Result; + /// + /// [`ErrorKind`]: enum.ErrorKind.html + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -80,47 +86,34 @@ struct Custom { + /// + /// [`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. + /// +@@ -130,10 +123,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -141,15 +132,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -158,15 +146,10 @@ pub enum ErrorKind { + /// 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, + } +@@ -190,14 +173,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -206,24 +188,6 @@ impl 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 +@@ -249,7 +213,6 @@ impl Error { + /// 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) } + } +@@ -280,7 +243,6 @@ impl Error { + /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -313,11 +275,10 @@ impl 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)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -383,11 +344,10 @@ impl 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)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -416,8 +376,7 @@ impl Error { + /// print_error(Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -442,10 +401,9 @@ impl 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::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -455,63 +413,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index cd05e6b..d048379 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,26 +8,31 @@ + // 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; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -38,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -56,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -80,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -102,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -120,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -147,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -171,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -180,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -204,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 03f55f7..f767e0c 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 0de02cb..0f202c2 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,42 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -304,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -335,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -417,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -467,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -509,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -547,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -608,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -660,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -690,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -727,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -765,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -799,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -835,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -875,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -901,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -928,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -980,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1036,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1066,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1082,7 +1056,6 @@ pub trait Seek { + /// 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; + } + +@@ -1092,29 +1065,26 @@ pub trait Seek { + /// + /// [`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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1194,7 +1164,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1239,7 +1209,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1261,7 +1230,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1302,7 +1270,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1350,7 +1317,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1383,7 +1349,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1408,7 +1373,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1420,14 +1384,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1440,7 +1402,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1467,7 +1429,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1499,7 +1460,6 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + + /// Consumes the `Take`, returning the wrapped reader. +@@ -1524,13 +1484,11 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[unstable(feature = "io_take_into_inner", issue = "0")] + pub fn into_inner(self) -> T { + self.inner + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1545,7 +1503,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1584,12 +1542,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1604,8 +1560,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1613,8 +1567,6 @@ pub struct Chars { + /// 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. +@@ -1624,8 +1576,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1657,25 +1607,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1694,13 +1625,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1725,12 +1656,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2c68802..93bcfc3 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/552eda70d33cead1398adfecce1a75e7a61e3daf.patch b/patches/552eda70d33cead1398adfecce1a75e7a61e3daf.patch new file mode 100644 index 0000000..ade6454 --- /dev/null +++ b/patches/552eda70d33cead1398adfecce1a75e7a61e3daf.patch @@ -0,0 +1,1543 @@ +diff --git a/buffered.rs b/buffered.rs +index 632ef3d..dd63ebe 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index 9a605fc..d3d0814 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 6dd7273..ba07b2c 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -248,49 +248,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -312,6 +295,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -343,6 +327,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -425,7 +410,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -475,7 +459,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -517,7 +500,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -555,7 +538,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -616,7 +599,6 @@ pub trait Read { + /// # 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) { +@@ -668,7 +650,6 @@ pub trait Read { + /// # 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. +@@ -698,7 +679,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -735,10 +715,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -773,7 +749,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -807,7 +782,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -843,7 +817,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -883,7 +856,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -909,7 +881,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -936,7 +907,6 @@ pub trait Write { + /// # 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) { +@@ -988,7 +958,6 @@ pub trait Write { + /// # 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 +@@ -1044,7 +1013,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1074,7 +1042,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1088,35 +1055,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1196,7 +1159,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1241,7 +1204,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1263,7 +1225,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1304,7 +1265,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1352,7 +1312,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1385,7 +1344,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1410,7 +1368,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1422,14 +1379,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1442,7 +1397,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1469,7 +1424,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1483,11 +1437,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1502,7 +1454,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + let buf = self.inner.fill_buf()?; +@@ -1524,12 +1476,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1549,8 +1499,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1558,8 +1506,6 @@ pub struct Chars { + /// 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. +@@ -1569,8 +1515,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1602,25 +1546,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1639,13 +1564,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1670,12 +1595,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/62b19c627ebde2bbfa6021de146c502124da7975.patch b/patches/62b19c627ebde2bbfa6021de146c502124da7975.patch new file mode 100644 index 0000000..67d7802 --- /dev/null +++ b/patches/62b19c627ebde2bbfa6021de146c502124da7975.patch @@ -0,0 +1,1543 @@ +diff --git a/buffered.rs b/buffered.rs +index a92ca95..e23b74f 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index e142c78..eeb11d5 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index a058337..d190cba 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1087,35 +1054,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1195,7 +1158,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1240,7 +1203,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1262,7 +1224,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1303,7 +1264,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1351,7 +1311,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1384,7 +1343,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1409,7 +1367,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1421,14 +1378,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1441,7 +1396,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1468,7 +1423,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1482,11 +1436,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1501,7 +1453,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1528,12 +1480,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1553,8 +1503,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1562,8 +1510,6 @@ pub struct Chars { + /// 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. +@@ -1573,8 +1519,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1606,25 +1550,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1643,13 +1568,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1674,12 +1599,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/78ab18199d69bcc801668bfbeea8190b2c73a939.patch b/patches/78ab18199d69bcc801668bfbeea8190b2c73a939.patch new file mode 100644 index 0000000..37f232d --- /dev/null +++ b/patches/78ab18199d69bcc801668bfbeea8190b2c73a939.patch @@ -0,0 +1,1543 @@ +diff --git a/buffered.rs b/buffered.rs +index 632ef3d..dd63ebe 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index 9a605fc..d3d0814 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index d914d14..d17f3cf 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -248,49 +248,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -312,6 +295,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -343,6 +327,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -425,7 +410,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -475,7 +459,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -517,7 +500,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -555,7 +538,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -616,7 +599,6 @@ pub trait Read { + /// # 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) { +@@ -668,7 +650,6 @@ pub trait Read { + /// # 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. +@@ -698,7 +679,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -735,10 +715,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -773,7 +749,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -807,7 +782,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -843,7 +817,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -883,7 +856,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -909,7 +881,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -936,7 +907,6 @@ pub trait Write { + /// # 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) { +@@ -988,7 +958,6 @@ pub trait Write { + /// # 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 +@@ -1044,7 +1013,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1074,7 +1042,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1088,35 +1055,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1196,7 +1159,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1241,7 +1204,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1263,7 +1225,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1304,7 +1265,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1352,7 +1312,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1385,7 +1344,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1410,7 +1368,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1422,14 +1379,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1442,7 +1397,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1469,7 +1424,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1483,11 +1437,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1502,7 +1454,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1529,12 +1481,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1554,8 +1504,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1563,8 +1511,6 @@ pub struct Chars { + /// 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. +@@ -1574,8 +1520,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1607,25 +1551,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1644,13 +1569,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1675,12 +1600,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/80d733385aa2ff150a5d6f83ecfe55afc7e19e68.patch b/patches/80d733385aa2ff150a5d6f83ecfe55afc7e19e68.patch new file mode 100644 index 0000000..10d3ba6 --- /dev/null +++ b/patches/80d733385aa2ff150a5d6f83ecfe55afc7e19e68.patch @@ -0,0 +1,1543 @@ +diff --git a/buffered.rs b/buffered.rs +index a92ca95..e23b74f 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 2d78055..1db50ee 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -240,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -268,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index e142c78..eeb11d5 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index a058337..d190cba 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1087,35 +1054,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1195,7 +1158,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1240,7 +1203,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1262,7 +1224,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1303,7 +1264,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1351,7 +1311,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1384,7 +1343,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1409,7 +1367,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1421,14 +1378,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1441,7 +1396,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1468,7 +1423,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1482,11 +1436,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1501,7 +1453,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1528,12 +1480,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1553,8 +1503,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1562,8 +1510,6 @@ pub struct Chars { + /// 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. +@@ -1573,8 +1519,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1606,25 +1550,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1643,13 +1568,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1674,12 +1599,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/8128817119e479b0610685e3fc7a6ff21cde5abc.patch b/patches/8128817119e479b0610685e3fc7a6ff21cde5abc.patch new file mode 100644 index 0000000..955463f --- /dev/null +++ b/patches/8128817119e479b0610685e3fc7a6ff21cde5abc.patch @@ -0,0 +1,1543 @@ +diff --git a/buffered.rs b/buffered.rs +index 632ef3d..dd63ebe 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index 9a605fc..d3d0814 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index bcce8ee..4a492d7 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -248,49 +248,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -312,6 +295,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -343,6 +327,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -425,7 +410,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -475,7 +459,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -517,7 +500,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -555,7 +538,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -616,7 +599,6 @@ pub trait Read { + /// # 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) { +@@ -668,7 +650,6 @@ pub trait Read { + /// # 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. +@@ -698,7 +679,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -735,10 +715,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -773,7 +749,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -807,7 +782,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -843,7 +817,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -883,7 +856,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -909,7 +881,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -936,7 +907,6 @@ pub trait Write { + /// # 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) { +@@ -988,7 +958,6 @@ pub trait Write { + /// # 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 +@@ -1044,7 +1013,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1074,7 +1042,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1088,35 +1055,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1196,7 +1159,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1241,7 +1204,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1263,7 +1225,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1304,7 +1265,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1352,7 +1312,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1385,7 +1344,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1410,7 +1368,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1422,14 +1379,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1442,7 +1397,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1469,7 +1424,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1483,11 +1437,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1502,7 +1454,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + let buf = self.inner.fill_buf()?; +@@ -1524,12 +1476,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1549,8 +1499,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1558,8 +1506,6 @@ pub struct Chars { + /// 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. +@@ -1569,8 +1515,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1602,25 +1546,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1639,13 +1564,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1670,12 +1595,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/87aee45988e81cb1a7bc9881aa7172d4f9caefd4.patch b/patches/87aee45988e81cb1a7bc9881aa7172d4f9caefd4.patch new file mode 100644 index 0000000..5270830 --- /dev/null +++ b/patches/87aee45988e81cb1a7bc9881aa7172d4f9caefd4.patch @@ -0,0 +1,1534 @@ +diff --git a/buffered.rs b/buffered.rs +index 632ef3d..dd63ebe 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index 9a605fc..d3d0814 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 28492b3..924d626 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -248,49 +248,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 64 * 1024; + +@@ -312,6 +295,7 @@ const DEFAULT_BUF_SIZE: usize = 64 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -343,6 +327,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -425,7 +410,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -475,7 +459,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -517,7 +500,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -555,7 +538,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -616,7 +599,6 @@ pub trait Read { + /// # 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) { +@@ -668,7 +650,6 @@ pub trait Read { + /// # 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. +@@ -698,7 +679,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -735,10 +715,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -773,7 +749,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -807,7 +782,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -843,7 +817,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -883,7 +856,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -909,7 +881,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -936,7 +907,6 @@ pub trait Write { + /// # 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) { +@@ -988,7 +958,6 @@ pub trait Write { + /// # 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 +@@ -1044,7 +1013,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1074,7 +1042,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1088,35 +1055,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1196,7 +1159,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1241,7 +1204,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1263,7 +1225,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1304,7 +1265,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1352,7 +1312,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1385,7 +1344,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1410,7 +1368,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1422,14 +1379,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1448,7 +1403,6 @@ impl Read for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1462,11 +1416,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1481,7 +1433,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + let buf = self.inner.fill_buf()?; +@@ -1503,12 +1455,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1528,8 +1478,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1537,8 +1485,6 @@ pub struct Chars { + /// 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. +@@ -1548,8 +1494,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1581,25 +1525,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1618,13 +1543,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1649,12 +1574,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/8d06332a27b020f5252238946fa9dccc3843d52a.patch b/patches/8d06332a27b020f5252238946fa9dccc3843d52a.patch new file mode 100644 index 0000000..398c294 --- /dev/null +++ b/patches/8d06332a27b020f5252238946fa9dccc3843d52a.patch @@ -0,0 +1,1465 @@ +diff --git a/buffered.rs b/buffered.rs +index 93b2d34..47282c9 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,14 +10,13 @@ + + //! Buffering wrappers for I/O traits + ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -43,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -66,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -87,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -115,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -136,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -157,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -179,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -196,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -206,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -295,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -330,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -344,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -362,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -411,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -429,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -447,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -456,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -476,7 +457,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -486,7 +466,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -496,7 +475,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -535,7 +513,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -568,23 +545,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -639,7 +606,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -659,7 +625,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -680,7 +645,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -701,7 +665,6 @@ impl LineWriter { + /// # 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. +@@ -724,7 +687,6 @@ impl LineWriter { + /// # 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. +@@ -746,7 +708,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -754,7 +715,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -773,7 +733,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index ae0085f..5392880 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,11 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + ++use core::prelude::v1::*; + use io::prelude::*; + + use core::convert::TryInto; +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +74,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +92,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +109,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +124,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +142,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +163,6 @@ impl Cursor { + /// 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. +@@ -187,11 +182,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +203,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +211,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +219,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -240,7 +230,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos: usize = self.position().try_into().map_err(|_| { +@@ -270,7 +259,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index ddf0030..748b830 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,10 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use error; +-use fmt; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -43,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -55,20 +59,22 @@ pub type Result = result::Result; + /// + /// [`ErrorKind`]: enum.ErrorKind.html + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -80,47 +86,34 @@ struct Custom { + /// + /// [`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. + /// +@@ -130,10 +123,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -141,15 +132,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -158,15 +146,10 @@ pub enum ErrorKind { + /// 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, + } +@@ -190,14 +173,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -206,24 +188,6 @@ impl 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 +@@ -249,7 +213,6 @@ impl Error { + /// 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) } + } +@@ -280,7 +243,6 @@ impl Error { + /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -313,11 +275,10 @@ impl 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)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -383,11 +344,10 @@ impl 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)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -416,8 +376,7 @@ impl Error { + /// print_error(Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -442,10 +401,9 @@ impl 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::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -455,63 +413,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index cd05e6b..d048379 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,26 +8,31 @@ + // 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; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -38,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -56,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -80,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -102,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -120,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -147,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -171,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -180,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -204,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 3824a5f..312cf47 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,10 +11,12 @@ + // Original implementation taken from rust-memchr + // Copyright 2015 Andrew Gallant, bluss and Nicolas Koch + ++pub use self::fallback::{memchr,memrchr}; ++ + #[allow(dead_code)] + pub mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 7310564..05d91fc 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,42 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -304,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -335,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -417,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -467,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -509,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -547,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -608,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -660,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -690,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -727,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -765,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -799,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -835,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -875,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -901,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -928,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -980,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1036,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1066,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1082,7 +1056,6 @@ pub trait Seek { + /// 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; + } + +@@ -1092,29 +1065,26 @@ pub trait Seek { + /// + /// [`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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1194,7 +1164,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1239,7 +1209,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1261,7 +1230,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[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. +@@ -1300,7 +1268,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1346,7 +1313,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1379,7 +1345,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1404,7 +1369,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1416,14 +1380,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1436,7 +1398,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1463,7 +1425,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1495,7 +1456,6 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + + /// Consumes the `Take`, returning the wrapped reader. +@@ -1520,13 +1480,11 @@ impl Take { + /// # 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 Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1541,7 +1499,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1580,12 +1538,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1600,8 +1556,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1609,8 +1563,6 @@ pub struct Chars { + /// 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. +@@ -1620,8 +1572,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1653,25 +1603,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1690,13 +1621,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1721,12 +1652,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2c68802..93bcfc3 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/8e414e0e3f27d1917d11ee80de827698beb53891.patch b/patches/8e414e0e3f27d1917d11ee80de827698beb53891.patch new file mode 100644 index 0000000..9ab22e6 --- /dev/null +++ b/patches/8e414e0e3f27d1917d11ee80de827698beb53891.patch @@ -0,0 +1,1543 @@ +diff --git a/buffered.rs b/buffered.rs +index 632ef3d..dd63ebe 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index e142c78..eeb11d5 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index a058337..d190cba 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1087,35 +1054,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1195,7 +1158,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1240,7 +1203,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1262,7 +1224,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1303,7 +1264,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1351,7 +1311,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1384,7 +1343,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1409,7 +1367,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1421,14 +1378,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1441,7 +1396,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1468,7 +1423,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1482,11 +1436,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1501,7 +1453,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1528,12 +1480,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1553,8 +1503,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1562,8 +1510,6 @@ pub struct Chars { + /// 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. +@@ -1573,8 +1519,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1606,25 +1550,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1643,13 +1568,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1674,12 +1599,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/92400cf8dcf411ce7e70ab2960639977d46d5b01.patch b/patches/92400cf8dcf411ce7e70ab2960639977d46d5b01.patch new file mode 100644 index 0000000..5bd493e --- /dev/null +++ b/patches/92400cf8dcf411ce7e70ab2960639977d46d5b01.patch @@ -0,0 +1,1551 @@ +diff --git a/buffered.rs b/buffered.rs +index a92ca95..e23b74f 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 2d78055..1db50ee 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -240,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -268,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index 05ae8ed..5da1224 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,16 +181,6 @@ impl 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. +- #[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 +@@ -238,7 +206,6 @@ impl Error { + /// 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) } + } +@@ -248,7 +215,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -260,11 +226,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -273,11 +238,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -285,8 +249,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -294,10 +257,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -307,63 +269,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index a408b43..823a3ad 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index d5b255e..c14c50e 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1087,35 +1054,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1195,7 +1158,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1240,7 +1203,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1262,7 +1224,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1303,7 +1264,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1351,7 +1311,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1384,7 +1343,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1409,7 +1367,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1421,14 +1378,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1441,7 +1396,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1468,7 +1423,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1482,11 +1436,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1501,7 +1453,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1540,12 +1492,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1560,8 +1510,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1569,8 +1517,6 @@ pub struct Chars { + /// 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. +@@ -1580,8 +1526,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1613,25 +1557,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1650,13 +1575,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1681,12 +1606,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index c8b52fc..aa2e47e 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/98e3120ad218b8d9c50e25a525dcff689c515776.patch b/patches/98e3120ad218b8d9c50e25a525dcff689c515776.patch new file mode 100644 index 0000000..8ea7dad --- /dev/null +++ b/patches/98e3120ad218b8d9c50e25a525dcff689c515776.patch @@ -0,0 +1,1551 @@ +diff --git a/buffered.rs b/buffered.rs +index a92ca95..e23b74f 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 2d78055..1db50ee 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -240,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -268,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index 05ae8ed..5da1224 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,16 +181,6 @@ impl 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. +- #[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 +@@ -238,7 +206,6 @@ impl Error { + /// 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) } + } +@@ -248,7 +215,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -260,11 +226,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -273,11 +238,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -285,8 +249,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -294,10 +257,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -307,63 +269,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index a058337..d190cba 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1087,35 +1054,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1195,7 +1158,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1240,7 +1203,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1262,7 +1224,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1303,7 +1264,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1351,7 +1311,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1384,7 +1343,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1409,7 +1367,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1421,14 +1378,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1441,7 +1396,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1468,7 +1423,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1482,11 +1436,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1501,7 +1453,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1528,12 +1480,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1553,8 +1503,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1562,8 +1510,6 @@ pub struct Chars { + /// 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. +@@ -1573,8 +1519,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1606,25 +1550,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1643,13 +1568,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1674,12 +1599,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 07f43f7..f7aa6f7 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// let num_bytes = io::sink().write(&mut 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/9a2c8783d91624261317316f996d8d2d09b7b6a4.patch b/patches/9a2c8783d91624261317316f996d8d2d09b7b6a4.patch new file mode 100644 index 0000000..a3408dd --- /dev/null +++ b/patches/9a2c8783d91624261317316f996d8d2d09b7b6a4.patch @@ -0,0 +1,1545 @@ +diff --git a/buffered.rs b/buffered.rs +index a26a932..83ea558 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,14 +10,13 @@ + + //! Buffering wrappers for I/O traits + ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -43,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -66,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -87,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -115,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -136,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -157,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -179,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -196,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -206,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -295,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -330,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -344,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -362,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -411,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -429,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -447,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -456,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -477,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -487,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -497,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -536,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -569,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -640,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -660,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -681,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -702,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -725,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -747,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -755,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -774,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 1b836b7..c77f287 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,9 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -72,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -91,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -109,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -125,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -144,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -166,7 +162,6 @@ impl Cursor { + /// 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. +@@ -186,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -209,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -218,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -227,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -239,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index d90be2e..d23baa8 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,10 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use error; +-use fmt; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -43,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -55,20 +59,22 @@ pub type Result = result::Result; + /// + /// [`ErrorKind`]: enum.ErrorKind.html + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -80,47 +86,34 @@ struct Custom { + /// + /// [`io::Error`]: struct.Error.html + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -130,10 +123,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -141,15 +132,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -158,14 +146,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -189,14 +172,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -205,24 +187,6 @@ impl 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 +@@ -248,7 +212,6 @@ impl Error { + /// 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) } + } +@@ -279,7 +242,6 @@ impl Error { + /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -312,11 +274,10 @@ impl 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)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -382,11 +343,10 @@ impl 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)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -415,8 +375,7 @@ impl Error { + /// print_error(Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -441,10 +400,9 @@ impl 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::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -454,63 +412,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index cd05e6b..d048379 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,26 +8,31 @@ + // 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; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -38,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -56,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -80,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -102,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -120,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -147,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -171,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -180,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -204,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index a408b43..823a3ad 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 1053792..5acfe6a 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,42 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -304,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -335,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -417,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -467,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -509,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -547,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -608,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -660,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -690,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -727,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -765,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -799,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -835,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -875,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -901,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -928,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -980,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1036,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1066,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1082,7 +1056,6 @@ pub trait Seek { + /// 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; + } + +@@ -1092,29 +1065,26 @@ pub trait Seek { + /// + /// [`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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1194,7 +1164,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1239,7 +1209,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1261,7 +1230,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1302,7 +1270,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1350,7 +1317,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1383,7 +1349,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1408,7 +1373,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1420,14 +1384,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1440,7 +1402,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1467,7 +1429,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1499,11 +1460,9 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1518,7 +1477,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1557,12 +1516,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1577,8 +1534,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1586,8 +1541,6 @@ pub struct Chars { + /// 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. +@@ -1597,8 +1550,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1630,25 +1581,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1667,13 +1599,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1698,12 +1630,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2c68802..93bcfc3 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5.patch b/patches/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5.patch new file mode 100644 index 0000000..cda86a3 --- /dev/null +++ b/patches/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5.patch @@ -0,0 +1,1543 @@ +diff --git a/buffered.rs b/buffered.rs +index 632ef3d..dd63ebe 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index 9a605fc..d3d0814 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index a058337..d190cba 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1087,35 +1054,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1195,7 +1158,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1240,7 +1203,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1262,7 +1224,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1303,7 +1264,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1351,7 +1311,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1384,7 +1343,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1409,7 +1367,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1421,14 +1378,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1441,7 +1396,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1468,7 +1423,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1482,11 +1436,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1501,7 +1453,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1528,12 +1480,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1553,8 +1503,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1562,8 +1510,6 @@ pub struct Chars { + /// 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. +@@ -1573,8 +1519,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1606,25 +1550,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1643,13 +1568,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1674,12 +1599,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4.patch b/patches/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4.patch new file mode 100644 index 0000000..80d0b31 --- /dev/null +++ b/patches/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4.patch @@ -0,0 +1,1543 @@ +diff --git a/buffered.rs b/buffered.rs +index 632ef3d..dd63ebe 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index 9a605fc..d3d0814 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index ca15aa2..900ee2f 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -248,49 +248,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -312,6 +295,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -343,6 +327,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -425,7 +410,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -475,7 +459,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -517,7 +500,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -555,7 +538,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -616,7 +599,6 @@ pub trait Read { + /// # 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) { +@@ -668,7 +650,6 @@ pub trait Read { + /// # 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. +@@ -698,7 +679,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -735,10 +715,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -773,7 +749,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -807,7 +782,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -843,7 +817,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -883,7 +856,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -909,7 +881,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -936,7 +907,6 @@ pub trait Write { + /// # 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) { +@@ -988,7 +958,6 @@ pub trait Write { + /// # 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 +@@ -1044,7 +1013,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1074,7 +1042,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1088,35 +1055,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1196,7 +1159,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1241,7 +1204,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1263,7 +1225,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1304,7 +1265,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1352,7 +1312,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1385,7 +1344,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1410,7 +1368,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1422,14 +1379,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1442,7 +1397,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1469,7 +1424,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1483,11 +1437,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1502,7 +1454,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1529,12 +1481,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1554,8 +1504,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1563,8 +1511,6 @@ pub struct Chars { + /// 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. +@@ -1574,8 +1520,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1607,25 +1551,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1644,13 +1569,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1675,12 +1600,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/c1fb50f5d377a41dd5833e4621e9a14879647503.patch b/patches/c1fb50f5d377a41dd5833e4621e9a14879647503.patch new file mode 100644 index 0000000..f7fddd7 --- /dev/null +++ b/patches/c1fb50f5d377a41dd5833e4621e9a14879647503.patch @@ -0,0 +1,1614 @@ +diff --git a/buffered.rs b/buffered.rs +index 08877fe..a1af9d9 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 420fede..670e362 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = try!(Read::read(&mut try!(self.fill_buf()), buf)); +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index e3f17c8..50e3658 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,21 +126,15 @@ pub enum ErrorKind { + /// 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. +- #[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, + + #[allow(missing_docs)] +- #[unstable(feature = "read_exact_old", reason = "recently added", +- issue = "0")] +- #[rustc_deprecated(since = "1.6.0", reason = "renamed to UnexpectedEof")] + UnexpectedEOF, + + /// An error returned when an operation could not be completed because an +@@ -162,14 +143,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -193,14 +169,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -209,18 +184,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -230,7 +194,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -242,11 +205,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -255,11 +217,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -267,8 +228,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -276,10 +236,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -289,65 +248,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- #[allow(deprecated)] // remove with UnexpectedEOF +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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::UnexpectedEof => "unexpected end of file", +- ErrorKind::__Nonexhaustive => unreachable!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index ec63f14..ac172bf 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 61334f3..9d9263f 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -248,49 +248,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 64 * 1024; + +@@ -312,6 +295,7 @@ const DEFAULT_BUF_SIZE: usize = 64 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -343,6 +327,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -425,7 +410,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -475,7 +459,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -517,7 +500,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -555,7 +538,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -616,7 +599,6 @@ pub trait Read { + /// # 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) { +@@ -668,7 +650,6 @@ pub trait Read { + /// # 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. +@@ -698,7 +679,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -735,10 +715,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -773,7 +749,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -807,7 +782,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -843,13 +817,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[unstable(feature = "io", reason = "the semantics of a partial read/write \ +- of where errors happen is currently \ +- unclear and may change", +- issue = "27802")] +- #[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + #[allow(deprecated)] + fn tee(self, out: W) -> Tee where Self: Sized { + Tee { reader: self, writer: out } +@@ -886,7 +853,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -926,7 +892,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -952,7 +917,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -979,7 +943,6 @@ pub trait Write { + /// # 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) { +@@ -1031,7 +994,6 @@ pub trait Write { + /// # 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 +@@ -1087,7 +1049,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + + /// Creates a new writer which will write all data to both this writer and +@@ -1117,13 +1078,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[unstable(feature = "io", reason = "the semantics of a partial read/write \ +- of where errors happen is currently \ +- unclear and may change", +- issue = "27802")] +- #[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + #[allow(deprecated)] + fn broadcast(self, other: W) -> Broadcast + where Self: Sized +@@ -1158,7 +1112,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1172,35 +1125,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1280,7 +1229,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1325,7 +1274,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1347,7 +1295,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1388,7 +1335,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1436,7 +1382,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1469,7 +1414,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1494,7 +1438,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1506,21 +1449,11 @@ pub trait BufRead: Read { + /// writer. Please see the documentation of `broadcast()` for more details. + /// + /// [broadcast]: trait.Write.html#method.broadcast +-#[unstable(feature = "io", reason = "awaiting stability of Write::broadcast", +- issue = "27802")] +-#[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + pub struct Broadcast { + first: T, + second: U, + } + +-#[unstable(feature = "io", reason = "awaiting stability of Write::broadcast", +- issue = "27802")] +-#[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + #[allow(deprecated)] + impl Write for Broadcast { + fn write(&mut self, data: &[u8]) -> Result { +@@ -1541,14 +1474,12 @@ impl Write for Broadcast { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1567,7 +1498,6 @@ impl Read for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1581,11 +1511,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1600,7 +1528,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + let buf = try!(self.inner.fill_buf()); +@@ -1622,21 +1550,11 @@ impl BufRead for Take { + /// Please see the documentation of `tee()` for more details. + /// + /// [tee]: trait.Read.html#method.tee +-#[unstable(feature = "io", reason = "awaiting stability of Read::tee", +- issue = "27802")] +-#[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + pub struct Tee { + reader: R, + writer: W, + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::tee", +- issue = "27802")] +-#[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + #[allow(deprecated)] + impl Read for Tee { + fn read(&mut self, buf: &mut [u8]) -> Result { +@@ -1653,12 +1571,10 @@ impl Read for Tee { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1678,8 +1594,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1687,8 +1601,6 @@ pub struct Chars { + /// 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. +@@ -1698,8 +1610,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1731,25 +1641,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1768,13 +1659,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1799,12 +1690,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index e05a0d5..2f03121 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/cae91d7c8c21aa860bda29c62207a6726837952b.patch b/patches/cae91d7c8c21aa860bda29c62207a6726837952b.patch new file mode 100644 index 0000000..67d7802 --- /dev/null +++ b/patches/cae91d7c8c21aa860bda29c62207a6726837952b.patch @@ -0,0 +1,1543 @@ +diff --git a/buffered.rs b/buffered.rs +index a92ca95..e23b74f 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index a1002fd..3a00ffe 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index e142c78..eeb11d5 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index a058337..d190cba 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1087,35 +1054,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1195,7 +1158,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1240,7 +1203,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1262,7 +1224,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1303,7 +1264,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1351,7 +1311,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1384,7 +1343,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1409,7 +1367,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1421,14 +1378,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1441,7 +1396,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1468,7 +1423,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1482,11 +1436,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1501,7 +1453,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1528,12 +1480,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1553,8 +1503,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1562,8 +1510,6 @@ pub struct Chars { + /// 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. +@@ -1573,8 +1519,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1606,25 +1550,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1643,13 +1568,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1674,12 +1599,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2815c01..1edcae5 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/ce943eb369c9bdd0aef4917675e515f39f3b4a1e.patch b/patches/ce943eb369c9bdd0aef4917675e515f39f3b4a1e.patch new file mode 100644 index 0000000..8796a83 --- /dev/null +++ b/patches/ce943eb369c9bdd0aef4917675e515f39f3b4a1e.patch @@ -0,0 +1,1534 @@ +diff --git a/buffered.rs b/buffered.rs +index 08877fe..a1af9d9 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 420fede..670e362 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = try!(Read::read(&mut try!(self.fill_buf()), buf)); +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index 9a605fc..d3d0814 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index ec63f14..ac172bf 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 60a720e..85ce207 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -248,49 +248,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 64 * 1024; + +@@ -312,6 +295,7 @@ const DEFAULT_BUF_SIZE: usize = 64 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -343,6 +327,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -425,7 +410,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -475,7 +459,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -517,7 +500,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -555,7 +538,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -616,7 +599,6 @@ pub trait Read { + /// # 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) { +@@ -668,7 +650,6 @@ pub trait Read { + /// # 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. +@@ -698,7 +679,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -735,10 +715,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -773,7 +749,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -807,7 +782,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -843,7 +817,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -883,7 +856,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -909,7 +881,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -936,7 +907,6 @@ pub trait Write { + /// # 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) { +@@ -988,7 +958,6 @@ pub trait Write { + /// # 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 +@@ -1044,7 +1013,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1074,7 +1042,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1088,35 +1055,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1196,7 +1159,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1241,7 +1204,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1263,7 +1225,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1304,7 +1265,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1352,7 +1312,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1385,7 +1344,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1410,7 +1368,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1422,14 +1379,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1448,7 +1403,6 @@ impl Read for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1462,11 +1416,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1481,7 +1433,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + let buf = try!(self.inner.fill_buf()); +@@ -1503,12 +1455,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1528,8 +1478,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1537,8 +1485,6 @@ pub struct Chars { + /// 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. +@@ -1548,8 +1494,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1581,25 +1525,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1618,13 +1543,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1649,12 +1574,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index fddb095..5e80819 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/d311079a6f70577d02f35bb80d27eef7e2b18a4a.patch b/patches/d311079a6f70577d02f35bb80d27eef7e2b18a4a.patch new file mode 100644 index 0000000..6a21c98 --- /dev/null +++ b/patches/d311079a6f70577d02f35bb80d27eef7e2b18a4a.patch @@ -0,0 +1,1464 @@ +diff --git a/buffered.rs b/buffered.rs +index 21a0cc1..27c5ac1 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,14 +10,13 @@ + + //! Buffering wrappers for I/O traits + ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -43,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -66,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -87,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -115,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -136,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -157,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -179,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -196,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -206,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -295,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -330,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -344,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -362,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -411,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -429,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -447,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -456,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -476,7 +457,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -486,7 +466,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -496,7 +475,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -535,7 +513,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -568,23 +545,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -639,7 +606,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -659,7 +625,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -680,7 +645,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -701,7 +665,6 @@ impl LineWriter { + /// # 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. +@@ -724,7 +687,6 @@ impl LineWriter { + /// # 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. +@@ -746,7 +708,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -754,7 +715,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -773,7 +733,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 1b836b7..c77f287 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,9 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -72,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -91,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -109,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -125,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -144,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -166,7 +162,6 @@ impl Cursor { + /// 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. +@@ -186,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -209,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -218,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -227,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -239,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index ddf0030..748b830 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,10 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use error; +-use fmt; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -43,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -55,20 +59,22 @@ pub type Result = result::Result; + /// + /// [`ErrorKind`]: enum.ErrorKind.html + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -80,47 +86,34 @@ struct Custom { + /// + /// [`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. + /// +@@ -130,10 +123,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -141,15 +132,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -158,15 +146,10 @@ pub enum ErrorKind { + /// 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, + } +@@ -190,14 +173,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -206,24 +188,6 @@ impl 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 +@@ -249,7 +213,6 @@ impl Error { + /// 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) } + } +@@ -280,7 +243,6 @@ impl Error { + /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -313,11 +275,10 @@ impl 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)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -383,11 +344,10 @@ impl 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)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -416,8 +376,7 @@ impl Error { + /// print_error(Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -442,10 +401,9 @@ impl 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::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -455,63 +413,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index cd05e6b..d048379 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,26 +8,31 @@ + // 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; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -38,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -56,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -80,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -102,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -120,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -147,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -171,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -180,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -204,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 3824a5f..312cf47 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,10 +11,12 @@ + // Original implementation taken from rust-memchr + // Copyright 2015 Andrew Gallant, bluss and Nicolas Koch + ++pub use self::fallback::{memchr,memrchr}; ++ + #[allow(dead_code)] + pub mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 3becc0a..05d91fc 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,42 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -304,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -335,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -417,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -467,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -509,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -547,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -608,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -660,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -690,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -727,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -765,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -799,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -835,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -875,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -901,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -928,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -980,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1036,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1066,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1082,7 +1056,6 @@ pub trait Seek { + /// 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; + } + +@@ -1092,29 +1065,26 @@ pub trait Seek { + /// + /// [`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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1194,7 +1164,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1239,7 +1209,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1261,7 +1230,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[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. +@@ -1300,7 +1268,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1346,7 +1313,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1379,7 +1345,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1404,7 +1369,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1416,14 +1380,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1436,7 +1398,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1463,7 +1425,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1495,7 +1456,6 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + + /// Consumes the `Take`, returning the wrapped reader. +@@ -1520,13 +1480,11 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[unstable(feature = "io_take_into_inner", issue = "0")] + pub fn into_inner(self) -> T { + self.inner + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1541,7 +1499,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1580,12 +1538,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1600,8 +1556,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1609,8 +1563,6 @@ pub struct Chars { + /// 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. +@@ -1620,8 +1572,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1653,25 +1603,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1690,13 +1621,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1721,12 +1652,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2c68802..93bcfc3 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/d40c593f42fafbac1ff3d827f6df96338b5b7d8b.patch b/patches/d40c593f42fafbac1ff3d827f6df96338b5b7d8b.patch new file mode 100644 index 0000000..45d1d65 --- /dev/null +++ b/patches/d40c593f42fafbac1ff3d827f6df96338b5b7d8b.patch @@ -0,0 +1,1551 @@ +diff --git a/buffered.rs b/buffered.rs +index a92ca95..e23b74f 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 2d78055..1db50ee 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -240,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -268,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index 05ae8ed..5da1224 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,16 +181,6 @@ impl 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. +- #[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 +@@ -238,7 +206,6 @@ impl Error { + /// 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) } + } +@@ -248,7 +215,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -260,11 +226,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -273,11 +238,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -285,8 +249,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -294,10 +257,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -307,63 +269,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index a408b43..823a3ad 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index a058337..d190cba 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1087,35 +1054,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1195,7 +1158,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1240,7 +1203,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1262,7 +1224,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1303,7 +1264,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1351,7 +1311,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1384,7 +1343,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1409,7 +1367,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1421,14 +1378,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1441,7 +1396,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1468,7 +1423,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1482,11 +1436,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1501,7 +1453,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1528,12 +1480,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1553,8 +1503,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1562,8 +1510,6 @@ pub struct Chars { + /// 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. +@@ -1573,8 +1519,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1606,25 +1550,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1643,13 +1568,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1674,12 +1599,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index c8b52fc..aa2e47e 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/d6aa4e828c3dc3b7c417197990741b024f8c4ca3.patch b/patches/d6aa4e828c3dc3b7c417197990741b024f8c4ca3.patch new file mode 100644 index 0000000..f6cd4fb --- /dev/null +++ b/patches/d6aa4e828c3dc3b7c417197990741b024f8c4ca3.patch @@ -0,0 +1,1556 @@ +diff --git a/buffered.rs b/buffered.rs +index dbb45d5..a138b3e 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,14 +10,13 @@ + + //! Buffering wrappers for I/O traits + ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -43,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -66,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -87,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -115,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -136,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -157,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -179,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -196,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -206,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -295,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -330,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -344,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -362,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -411,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -429,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -447,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -456,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -476,7 +457,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -486,7 +466,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -496,7 +475,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -535,7 +513,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -568,23 +545,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -639,7 +606,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -659,7 +625,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -680,7 +645,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -701,7 +665,6 @@ impl LineWriter { + /// # 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. +@@ -724,7 +687,6 @@ impl LineWriter { + /// # 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. +@@ -746,7 +708,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -754,7 +715,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -773,7 +733,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 1b836b7..c77f287 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,9 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -72,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -91,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -109,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -125,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -144,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -166,7 +162,6 @@ impl Cursor { + /// 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. +@@ -186,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -209,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -218,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -227,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -239,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index ddf0030..748b830 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,10 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use error; +-use fmt; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -43,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -55,20 +59,22 @@ pub type Result = result::Result; + /// + /// [`ErrorKind`]: enum.ErrorKind.html + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -80,47 +86,34 @@ struct Custom { + /// + /// [`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. + /// +@@ -130,10 +123,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -141,15 +132,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -158,15 +146,10 @@ pub enum ErrorKind { + /// 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, + } +@@ -190,14 +173,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -206,24 +188,6 @@ impl 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 +@@ -249,7 +213,6 @@ impl Error { + /// 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) } + } +@@ -280,7 +243,6 @@ impl Error { + /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -313,11 +275,10 @@ impl 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)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -383,11 +344,10 @@ impl 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)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -416,8 +376,7 @@ impl Error { + /// print_error(Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -442,10 +401,9 @@ impl 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::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -455,63 +413,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index cd05e6b..d048379 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,26 +8,31 @@ + // 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; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -38,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -56,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -80,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -102,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -120,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -147,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -171,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -180,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -204,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 03f55f7..f767e0c 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 06609cf..a1bbe50 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,42 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -304,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -335,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -417,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -467,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -509,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -547,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -608,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -660,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -690,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -727,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -765,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -799,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -835,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -875,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -901,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -928,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -980,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1036,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1066,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1082,7 +1056,6 @@ pub trait Seek { + /// 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; + } + +@@ -1092,29 +1065,26 @@ pub trait Seek { + /// + /// [`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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1194,7 +1164,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1239,7 +1209,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1261,7 +1230,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1302,7 +1270,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1350,7 +1317,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1383,7 +1349,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1408,7 +1373,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1420,14 +1384,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1440,7 +1402,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1467,7 +1429,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1499,7 +1460,6 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + + /// Consumes the `Take`, returning the wrapped reader. +@@ -1524,13 +1484,11 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[unstable(feature = "io_take_into_inner", issue = "0")] + pub fn into_inner(self) -> T { + self.inner + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1545,7 +1503,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1584,12 +1542,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1604,8 +1560,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1613,8 +1567,6 @@ pub struct Chars { + /// 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. +@@ -1624,8 +1576,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1657,25 +1607,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1694,13 +1625,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1725,12 +1656,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2c68802..93bcfc3 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/dd56a6ad0845b76509c4f8967e8ca476471ab7e0.patch b/patches/dd56a6ad0845b76509c4f8967e8ca476471ab7e0.patch new file mode 100644 index 0000000..a0a9167 --- /dev/null +++ b/patches/dd56a6ad0845b76509c4f8967e8ca476471ab7e0.patch @@ -0,0 +1,1545 @@ +diff --git a/buffered.rs b/buffered.rs +index a92ca95..e23b74f 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 2d78055..1db50ee 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -240,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -268,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index e142c78..eeb11d5 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,15 +126,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -156,14 +140,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -187,14 +166,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -203,18 +181,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -224,7 +191,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -236,11 +202,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -249,11 +214,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -261,8 +225,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -270,10 +233,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -283,63 +245,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index a058337..d190cba 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1087,35 +1054,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1195,7 +1158,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1240,7 +1203,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1262,7 +1224,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1303,7 +1264,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1351,7 +1311,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1384,7 +1343,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1409,7 +1367,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1421,14 +1378,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1441,7 +1396,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1468,7 +1423,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1482,11 +1436,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1501,7 +1453,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1528,12 +1480,10 @@ impl BufRead for Take { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1553,8 +1503,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1562,8 +1510,6 @@ pub struct Chars { + /// 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. +@@ -1573,8 +1519,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1606,25 +1550,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1643,13 +1568,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1674,12 +1599,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 07f43f7..f7aa6f7 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// let num_bytes = io::sink().write(&mut 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/e107c8b84969fbe52cae7c9fd61858fddc6e016b.patch b/patches/e107c8b84969fbe52cae7c9fd61858fddc6e016b.patch new file mode 100644 index 0000000..2968c07 --- /dev/null +++ b/patches/e107c8b84969fbe52cae7c9fd61858fddc6e016b.patch @@ -0,0 +1,1556 @@ +diff --git a/buffered.rs b/buffered.rs +index 4ff8c6a..fd4eb13 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,14 +10,13 @@ + + //! Buffering wrappers for I/O traits + ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -43,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -66,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -87,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -115,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -136,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -157,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -179,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -196,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -206,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -295,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -330,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -344,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -362,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -411,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -429,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -447,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -456,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -476,7 +457,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -486,7 +466,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -496,7 +475,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -535,7 +513,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -568,23 +545,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -639,7 +606,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -659,7 +625,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -680,7 +645,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -701,7 +665,6 @@ impl LineWriter { + /// # 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. +@@ -724,7 +687,6 @@ impl LineWriter { + /// # 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. +@@ -746,7 +708,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -754,7 +715,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -773,7 +733,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 1b836b7..c77f287 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,9 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -72,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -91,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -109,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -125,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -144,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -166,7 +162,6 @@ impl Cursor { + /// 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. +@@ -186,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -209,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -218,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -227,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -239,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index ddf0030..748b830 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,10 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use error; +-use fmt; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -43,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -55,20 +59,22 @@ pub type Result = result::Result; + /// + /// [`ErrorKind`]: enum.ErrorKind.html + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -80,47 +86,34 @@ struct Custom { + /// + /// [`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. + /// +@@ -130,10 +123,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -141,15 +132,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -158,15 +146,10 @@ pub enum ErrorKind { + /// 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, + } +@@ -190,14 +173,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -206,24 +188,6 @@ impl 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 +@@ -249,7 +213,6 @@ impl Error { + /// 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) } + } +@@ -280,7 +243,6 @@ impl Error { + /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -313,11 +275,10 @@ impl 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)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -383,11 +344,10 @@ impl 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)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -416,8 +376,7 @@ impl Error { + /// print_error(Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -442,10 +401,9 @@ impl 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::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -455,63 +413,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index cd05e6b..d048379 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,26 +8,31 @@ + // 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; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -38,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -56,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -80,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -102,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -120,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -147,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -171,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -180,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -204,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 03f55f7..f767e0c 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 06609cf..a1bbe50 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,42 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -304,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -335,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -417,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -467,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -509,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -547,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -608,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -660,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -690,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -727,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -765,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -799,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -835,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -875,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -901,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -928,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -980,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1036,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1066,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1082,7 +1056,6 @@ pub trait Seek { + /// 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; + } + +@@ -1092,29 +1065,26 @@ pub trait Seek { + /// + /// [`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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1194,7 +1164,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1239,7 +1209,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1261,7 +1230,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1302,7 +1270,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1350,7 +1317,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1383,7 +1349,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1408,7 +1373,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1420,14 +1384,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1440,7 +1402,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1467,7 +1429,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1499,7 +1460,6 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + + /// Consumes the `Take`, returning the wrapped reader. +@@ -1524,13 +1484,11 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[unstable(feature = "io_take_into_inner", issue = "0")] + pub fn into_inner(self) -> T { + self.inner + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1545,7 +1503,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1584,12 +1542,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1604,8 +1560,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1613,8 +1567,6 @@ pub struct Chars { + /// 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. +@@ -1624,8 +1576,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1657,25 +1607,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1694,13 +1625,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1725,12 +1656,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index 2c68802..93bcfc3 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/e6cc4c5d13f8819c72568f9675e84c1d17368c67.patch b/patches/e6cc4c5d13f8819c72568f9675e84c1d17368c67.patch new file mode 100644 index 0000000..1770c6c --- /dev/null +++ b/patches/e6cc4c5d13f8819c72568f9675e84c1d17368c67.patch @@ -0,0 +1,1561 @@ +diff --git a/buffered.rs b/buffered.rs +index a92ca95..e23b74f 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 2d78055..1db50ee 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -240,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -268,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index 5333b0a..25bbd8b 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -59,20 +59,22 @@ pub type Result = result::Result; + /// + /// [`ErrorKind`]: enum.ErrorKind.html + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -84,47 +86,34 @@ struct Custom { + /// + /// [`io::Error`]: struct.Error.html + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -134,10 +123,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -145,15 +132,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -162,14 +146,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -193,14 +172,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -209,24 +187,6 @@ impl 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 +@@ -252,7 +212,6 @@ impl Error { + /// 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) } + } +@@ -283,7 +242,6 @@ impl Error { + /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -316,11 +274,10 @@ impl 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)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -386,11 +343,10 @@ impl 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)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -419,8 +375,7 @@ impl Error { + /// print_error(Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -445,10 +400,9 @@ impl 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::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -458,63 +412,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index a408b43..823a3ad 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 307d014..3f9472c 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1089,7 +1056,6 @@ pub trait Seek { + /// 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; + } + +@@ -1099,29 +1065,26 @@ pub trait Seek { + /// + /// [`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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1201,7 +1164,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1246,7 +1209,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1268,7 +1230,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1309,7 +1270,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1357,7 +1317,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1390,7 +1349,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1415,7 +1373,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1427,14 +1384,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1447,7 +1402,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1474,7 +1429,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1506,11 +1460,9 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1525,7 +1477,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1564,12 +1516,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1584,8 +1534,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1593,8 +1541,6 @@ pub struct Chars { + /// 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. +@@ -1604,8 +1550,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1637,25 +1581,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1674,13 +1599,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1705,12 +1630,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index c8b52fc..aa2e47e 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/f1e191c0b959111aef19f3aa06b7f1743419470c.patch b/patches/f1e191c0b959111aef19f3aa06b7f1743419470c.patch new file mode 100644 index 0000000..3c5a660 --- /dev/null +++ b/patches/f1e191c0b959111aef19f3aa06b7f1743419470c.patch @@ -0,0 +1,1614 @@ +diff --git a/buffered.rs b/buffered.rs +index ccebf36..17e6955 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -773,7 +732,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 420fede..670e362 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = try!(Read::read(&mut try!(self.fill_buf()), buf)); +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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]> { + fn write(&mut self, data: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +@@ -239,7 +228,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -267,7 +255,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let pos = cmp::min(self.pos, self.inner.len() as u64); +diff --git a/error.rs b/error.rs +index e3f17c8..50e3658 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -57,20 +57,22 @@ pub type Result = result::Result; + /// `Error` can be created with crafted error messages and a particular value of + /// `ErrorKind`. + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -78,47 +80,34 @@ struct Custom { + /// This list is intended to grow over time and it is not recommended to + /// exhaustively match against it. + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -128,10 +117,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -139,21 +126,15 @@ pub enum ErrorKind { + /// 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. +- #[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, + + #[allow(missing_docs)] +- #[unstable(feature = "read_exact_old", reason = "recently added", +- issue = "0")] +- #[rustc_deprecated(since = "1.6.0", reason = "renamed to UnexpectedEof")] + UnexpectedEOF, + + /// An error returned when an operation could not be completed because an +@@ -162,14 +143,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -193,14 +169,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -209,18 +184,7 @@ impl 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. +- #[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. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn from_raw_os_error(code: i32) -> Error { + Error { repr: Repr::Os(code) } + } +@@ -230,7 +194,6 @@ impl Error { + /// 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`. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -242,11 +205,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_ref(&self) -> Option<&(error::Error+Send+Sync+'static)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -255,11 +217,10 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn get_mut(&mut self) -> Option<&mut (error::Error+Send+Sync+'static)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -267,8 +228,7 @@ impl Error { + /// + /// If this `Error` was constructed via `new` then this function will + /// return `Some`, otherwise it will return `None`. +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -276,10 +236,9 @@ impl Error { + } + + /// Returns the corresponding `ErrorKind` for this error. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn kind(&self) -> ErrorKind { + match self.repr { +- Repr::Os(code) => sys::decode_error_kind(code), ++ Repr::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -289,65 +248,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- #[allow(deprecated)] // remove with UnexpectedEOF +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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::UnexpectedEof => "unexpected end of file", +- ErrorKind::__Nonexhaustive => unreachable!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index ec63f14..ac172bf 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index 1d97611..110cfac 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 61334f3..9d9263f 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -248,49 +248,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 64 * 1024; + +@@ -312,6 +295,7 @@ const DEFAULT_BUF_SIZE: usize = 64 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -343,6 +327,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -425,7 +410,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -475,7 +459,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -517,7 +500,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -555,7 +538,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -616,7 +599,6 @@ pub trait Read { + /// # 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) { +@@ -668,7 +650,6 @@ pub trait Read { + /// # 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. +@@ -698,7 +679,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -735,10 +715,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -773,7 +749,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -807,7 +782,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -843,13 +817,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[unstable(feature = "io", reason = "the semantics of a partial read/write \ +- of where errors happen is currently \ +- unclear and may change", +- issue = "27802")] +- #[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + #[allow(deprecated)] + fn tee(self, out: W) -> Tee where Self: Sized { + Tee { reader: self, writer: out } +@@ -886,7 +853,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -926,7 +892,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -952,7 +917,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -979,7 +943,6 @@ pub trait Write { + /// # 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) { +@@ -1031,7 +994,6 @@ pub trait Write { + /// # 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 +@@ -1087,7 +1049,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + + /// Creates a new writer which will write all data to both this writer and +@@ -1117,13 +1078,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[unstable(feature = "io", reason = "the semantics of a partial read/write \ +- of where errors happen is currently \ +- unclear and may change", +- issue = "27802")] +- #[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + #[allow(deprecated)] + fn broadcast(self, other: W) -> Broadcast + where Self: Sized +@@ -1158,7 +1112,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1172,35 +1125,31 @@ pub trait Seek { + /// # Errors + /// + /// Seeking to a negative offset is considered an error. +- #[stable(feature = "rust1", since = "1.0.0")] + fn seek(&mut self, pos: SeekFrom) -> Result; + } + + /// Enumeration of possible methods to seek within an I/O object. + #[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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1280,7 +1229,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1325,7 +1274,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1347,7 +1295,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1388,7 +1335,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1436,7 +1382,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1469,7 +1414,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1494,7 +1438,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1506,21 +1449,11 @@ pub trait BufRead: Read { + /// writer. Please see the documentation of `broadcast()` for more details. + /// + /// [broadcast]: trait.Write.html#method.broadcast +-#[unstable(feature = "io", reason = "awaiting stability of Write::broadcast", +- issue = "27802")] +-#[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + pub struct Broadcast { + first: T, + second: U, + } + +-#[unstable(feature = "io", reason = "awaiting stability of Write::broadcast", +- issue = "27802")] +-#[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + #[allow(deprecated)] + impl Write for Broadcast { + fn write(&mut self, data: &[u8]) -> Result { +@@ -1541,14 +1474,12 @@ impl Write for Broadcast { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1567,7 +1498,6 @@ impl Read for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1581,11 +1511,9 @@ impl Take { + /// + /// This instance may reach EOF after reading fewer bytes than indicated by + /// this method if the underlying `Read` instance reaches EOF. +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1600,7 +1528,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + let buf = try!(self.inner.fill_buf()); +@@ -1622,21 +1550,11 @@ impl BufRead for Take { + /// Please see the documentation of `tee()` for more details. + /// + /// [tee]: trait.Read.html#method.tee +-#[unstable(feature = "io", reason = "awaiting stability of Read::tee", +- issue = "27802")] +-#[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + pub struct Tee { + reader: R, + writer: W, + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::tee", +- issue = "27802")] +-#[rustc_deprecated(reason = "error handling semantics unclear and \ +- don't seem to have an ergonomic resolution", +- since = "1.6.0")] + #[allow(deprecated)] + impl Read for Tee { + fn read(&mut self, buf: &mut [u8]) -> Result { +@@ -1653,12 +1571,10 @@ impl Read for Tee { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1678,8 +1594,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1687,8 +1601,6 @@ pub struct Chars { + /// 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. +@@ -1698,8 +1610,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1731,25 +1641,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1768,13 +1659,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1799,12 +1690,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index e05a0d5..2f03121 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -87,14 +86,12 @@ pub struct Empty { _priv: () } + /// # Ok(buffer) + /// # } + /// ``` +-#[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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -106,17 +103,14 @@ impl BufRead for Empty { + /// 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. +-#[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 { + for slot in &mut *buf { +@@ -132,17 +126,14 @@ impl Read for Repeat { + /// 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. +-#[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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/patches/fda473f00fa07b9a8246b104396f9922e54bff16.patch b/patches/fda473f00fa07b9a8246b104396f9922e54bff16.patch new file mode 100644 index 0000000..6d5afb3 --- /dev/null +++ b/patches/fda473f00fa07b9a8246b104396f9922e54bff16.patch @@ -0,0 +1,1561 @@ +diff --git a/buffered.rs b/buffered.rs +index a92ca95..e23b74f 100644 +--- a/buffered.rs ++++ b/buffered.rs +@@ -10,15 +10,13 @@ + + //! Buffering wrappers for I/O traits + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use marker::Reflect; +-use cmp; +-use error; +-use fmt; ++use core::cmp; ++use core::fmt; + use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; +-use memchr; ++use io::memchr; + + /// The `BufReader` struct adds buffering to any reader. + /// +@@ -44,7 +42,6 @@ use memchr; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufReader { + inner: R, + buf: Box<[u8]>, +@@ -67,7 +64,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: R) -> BufReader { + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -88,7 +84,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: R) -> BufReader { + BufReader { + inner: inner, +@@ -116,7 +111,6 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn get_ref(&self) -> &R { &self.inner } + + /// Gets a mutable reference to the underlying reader. +@@ -137,7 +131,6 @@ impl BufReader { + /// # 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. +@@ -158,11 +151,9 @@ impl BufReader { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> R { self.inner } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for BufReader { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // If we don't have any buffered data and we're doing a massive read +@@ -180,7 +171,6 @@ impl Read for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for BufReader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + // If we've reached the end of our internal buffer then we need to fetch +@@ -197,7 +187,6 @@ impl BufRead for BufReader { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufReader where R: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufReader") +@@ -207,7 +196,6 @@ impl fmt::Debug for BufReader where R: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufReader { + /// Seek to an offset, in bytes, in the underlying reader. + /// +@@ -296,7 +284,6 @@ impl Seek for BufReader { + /// 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. +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct BufWriter { + inner: Option, + buf: Vec, +@@ -331,7 +318,6 @@ pub struct BufWriter { + /// }; + /// ``` + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct IntoInnerError(W, Error); + + impl BufWriter { +@@ -345,7 +331,6 @@ impl BufWriter { + /// + /// 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 { + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) + } +@@ -363,7 +348,6 @@ impl BufWriter { + /// 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 { + BufWriter { + inner: Some(inner), +@@ -412,7 +396,6 @@ impl BufWriter { + /// // 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. +@@ -430,7 +413,6 @@ impl BufWriter { + /// // 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. +@@ -448,7 +430,6 @@ impl BufWriter { + /// // 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>> { + match self.flush_buf() { + Err(e) => Err(IntoInnerError(self, e)), +@@ -457,7 +438,6 @@ impl BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.buf.len() + buf.len() > self.buf.capacity() { +@@ -478,7 +458,6 @@ impl Write for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for BufWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BufWriter") +@@ -488,7 +467,6 @@ impl fmt::Debug for BufWriter where W: fmt::Debug { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Seek for BufWriter { + /// Seek to the offset, in bytes, in the underlying writer. + /// +@@ -498,7 +476,6 @@ impl Seek for BufWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Drop for BufWriter { + fn drop(&mut self) { + if self.inner.is_some() && !self.panicked { +@@ -537,7 +514,6 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn error(&self) -> &Error { &self.1 } + + /// Returns the buffered writer instance which generated the error. +@@ -570,23 +546,13 @@ impl IntoInnerError { + /// } + /// }; + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> W { self.0 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl From> for Error { + fn from(iie: IntoInnerError) -> Error { iie.1 } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for IntoInnerError { +- fn description(&self) -> &str { +- error::Error::description(self.error()) +- } +-} +- +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Display for IntoInnerError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.error().fmt(f) +@@ -641,7 +607,6 @@ impl fmt::Display for IntoInnerError { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct LineWriter { + inner: BufWriter, + } +@@ -661,7 +626,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: W) -> LineWriter { + // Lines typically aren't that long, don't use a giant buffer + LineWriter::with_capacity(1024, inner) +@@ -682,7 +646,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn with_capacity(cap: usize, inner: W) -> LineWriter { + LineWriter { inner: BufWriter::with_capacity(cap, inner) } + } +@@ -703,7 +666,6 @@ impl LineWriter { + /// # 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. +@@ -726,7 +688,6 @@ impl LineWriter { + /// # 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. +@@ -748,7 +709,6 @@ impl LineWriter { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn into_inner(self) -> Result>> { + self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { + IntoInnerError(LineWriter { inner: buf }, e) +@@ -756,7 +716,6 @@ impl LineWriter { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for LineWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + match memchr::memrchr(b'\n', buf) { +@@ -775,7 +734,6 @@ impl Write for LineWriter { + fn flush(&mut self) -> io::Result<()> { self.inner.flush() } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl fmt::Debug for LineWriter where W: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("LineWriter") +diff --git a/cursor.rs b/cursor.rs +index 2d78055..1db50ee 100644 +--- a/cursor.rs ++++ b/cursor.rs +@@ -8,10 +8,10 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use prelude::v1::*; ++use core::prelude::v1::*; + use io::prelude::*; + +-use cmp; ++use core::cmp; + use io::{self, SeekFrom, Error, ErrorKind}; + + /// A `Cursor` wraps another type and provides it with a +@@ -73,7 +73,6 @@ use io::{self, SeekFrom, Error, ErrorKind}; + /// 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 { + inner: T, +@@ -92,7 +91,6 @@ impl Cursor { + /// # fn force_inference(_: &Cursor>) {} + /// # force_inference(&buff); + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(inner: T) -> Cursor { + Cursor { pos: 0, inner: inner } + } +@@ -110,7 +108,6 @@ impl Cursor { + /// + /// 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. +@@ -126,7 +123,6 @@ impl Cursor { + /// + /// 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. +@@ -145,7 +141,6 @@ impl Cursor { + /// + /// 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. +@@ -167,7 +162,6 @@ impl Cursor { + /// 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. +@@ -187,11 +181,9 @@ impl Cursor { + /// 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 io::Seek for Cursor where T: AsRef<[u8]> { + fn seek(&mut self, style: SeekFrom) -> io::Result { + let pos = match style { +@@ -210,7 +202,6 @@ impl io::Seek for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Cursor where T: AsRef<[u8]> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = Read::read(&mut self.fill_buf()?, buf)?; +@@ -219,7 +210,6 @@ impl Read for Cursor where T: AsRef<[u8]> { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl BufRead for Cursor 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); +@@ -228,7 +218,6 @@ impl BufRead for Cursor where T: AsRef<[u8]> { + 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 { +@@ -240,7 +229,6 @@ impl<'a> Write for Cursor<&'a mut [u8]> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Write for Cursor> { + fn write(&mut self, buf: &[u8]) -> io::Result { + // Make sure the internal buffer is as least as big as where we +@@ -268,7 +256,6 @@ impl Write for Cursor> { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "cursor_box_slice", since = "1.5.0")] + impl Write for Cursor> { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/error.rs b/error.rs +index 5333b0a..25bbd8b 100644 +--- a/error.rs ++++ b/error.rs +@@ -8,14 +8,15 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use convert::Into; +-use error; +-use fmt; +-use marker::{Send, Sync}; +-use option::Option::{self, Some, None}; +-use result; +-use sys; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++#[cfg(not(feature="alloc"))] use ::FakeBox as Box; ++use core::convert::Into; ++use core::fmt; ++use core::marker::{Send, Sync}; ++use core::option::Option::{self, Some, None}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(not(feature="collections"))] use ::ErrorString as String; + + /// A specialized [`Result`](../result/enum.Result.html) type for I/O + /// operations. +@@ -47,7 +48,6 @@ use sys; + /// Ok(buffer) + /// } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub type Result = result::Result; + + /// The error type for I/O operations of the `Read`, `Write`, `Seek`, and +@@ -59,20 +59,22 @@ pub type Result = result::Result; + /// + /// [`ErrorKind`]: enum.ErrorKind.html + #[derive(Debug)] +-#[stable(feature = "rust1", since = "1.0.0")] + pub struct Error { + repr: Repr, + } + + enum Repr { + Os(i32), ++ #[cfg(feature="alloc")] + Custom(Box), ++ #[cfg(not(feature="alloc"))] ++ Custom(Custom), + } + + #[derive(Debug)] + struct Custom { + kind: ErrorKind, +- error: Box, ++ error: String, + } + + /// A list specifying general categories of I/O error. +@@ -84,47 +86,34 @@ struct Custom { + /// + /// [`io::Error`]: struct.Error.html + #[derive(Copy, PartialEq, Eq, Clone, Debug)] +-#[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. + /// +@@ -134,10 +123,8 @@ pub enum ErrorKind { + /// + /// 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. +- #[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)`. +@@ -145,15 +132,12 @@ pub enum ErrorKind { + /// 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. +- #[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 +@@ -162,14 +146,9 @@ pub enum ErrorKind { + /// 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, + + /// Any I/O error not part of this list. +- #[unstable(feature = "io_error_internals", +- reason = "better expressed through extensible enums that this \ +- enum cannot be exhaustively matched against", +- issue = "0")] + #[doc(hidden)] + __Nonexhaustive, + } +@@ -193,14 +172,13 @@ impl Error { + /// // 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(kind: ErrorKind, error: E) -> Error +- where E: Into> ++ where E: Into + { + Self::_new(kind, error.into()) + } + +- fn _new(kind: ErrorKind, error: Box) -> Error { ++ fn _new(kind: ErrorKind, error: String) -> Error { + Error { + repr: Repr::Custom(Box::new(Custom { + kind: kind, +@@ -209,24 +187,6 @@ impl 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 +@@ -252,7 +212,6 @@ impl Error { + /// 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) } + } +@@ -283,7 +242,6 @@ impl Error { + /// print_os_error(&Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn raw_os_error(&self) -> Option { + match self.repr { + Repr::Os(i) => Some(i), +@@ -316,11 +274,10 @@ impl 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)> { ++ pub fn get_ref(&self) -> Option<&String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref c) => Some(&*c.error), ++ Repr::Custom(ref c) => Some(&c.error), + } + } + +@@ -386,11 +343,10 @@ impl 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)> { ++ pub fn get_mut(&mut self) -> Option<&mut String> { + match self.repr { + Repr::Os(..) => None, +- Repr::Custom(ref mut c) => Some(&mut *c.error), ++ Repr::Custom(ref mut c) => Some(&mut c.error), + } + } + +@@ -419,8 +375,7 @@ impl Error { + /// print_error(Error::new(ErrorKind::Other, "oh no!")); + /// } + /// ``` +- #[stable(feature = "io_error_inner", since = "1.3.0")] +- pub fn into_inner(self) -> Option> { ++ pub fn into_inner(self) -> Option { + match self.repr { + Repr::Os(..) => None, + Repr::Custom(c) => Some(c.error) +@@ -445,10 +400,9 @@ impl 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::Os(_code) => ErrorKind::Other, + Repr::Custom(ref c) => c.kind, + } + } +@@ -458,63 +412,23 @@ 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(), ++ fmt.debug_struct("Os").field("code", code).finish(), + Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).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) ++ write!(fmt, "os error {}", code) + } + Repr::Custom(ref c) => c.error.fmt(fmt), + } + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] +-impl error::Error for Error { +- fn description(&self) -> &str { +- match self.repr { +- Repr::Os(..) => match self.kind() { +- 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!() +- }, +- Repr::Custom(ref c) => c.error.description(), +- } +- } +- +- fn cause(&self) -> Option<&error::Error> { +- match self.repr { +- Repr::Os(..) => None, +- Repr::Custom(ref c) => c.error.cause(), +- } +- } +-} +- + fn _assert_error_is_sync_send() { + fn _is_sync_send() {} + _is_sync_send::(); +diff --git a/impls.rs b/impls.rs +index 3179938..291c69c 100644 +--- a/impls.rs ++++ b/impls.rs +@@ -8,29 +8,31 @@ + // option. This file may not be copied, modified, or distributed + // except according to those terms. + +-use boxed::Box; +-use cmp; +-use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; +-use fmt; +-use mem; +-use string::String; +-use vec::Vec; ++#[cfg(feature="alloc")] use alloc::boxed::Box; ++use core::cmp; ++use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; ++use core::fmt; ++use core::mem; ++#[cfg(feature="collections")] use collections::string::String; ++#[cfg(feature="collections")] use collections::vec::Vec; + + // ============================================================================= + // 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 { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -41,7 +43,6 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R { + (**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 { (**self).write(buf) } +@@ -59,12 +60,11 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W { + (**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 { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -83,18 +83,20 @@ impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Read for Box { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (**self).read(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (**self).read_to_end(buf) + } + ++ #[cfg(feature="collections")] + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (**self).read_to_string(buf) +@@ -105,7 +107,7 @@ impl Read for Box { + (**self).read_exact(buf) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Write for Box { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { (**self).write(buf) } +@@ -123,12 +125,12 @@ impl Write for Box { + (**self).write_fmt(fmt) + } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="alloc")] + impl Seek for Box { + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Box { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } +@@ -150,7 +152,6 @@ impl BufRead for Box { + // ============================================================================= + // In-memory buffer implementations + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Read for &'a [u8] { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { +@@ -174,7 +175,7 @@ impl<'a> Read for &'a [u8] { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl<'a> BufRead for &'a [u8] { + #[inline] + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } +@@ -183,7 +184,6 @@ impl<'a> BufRead for &'a [u8] { + fn consume(&mut self, amt: usize) { *self = &self[amt..]; } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl<'a> Write for &'a mut [u8] { + #[inline] + fn write(&mut self, data: &[u8]) -> io::Result { +@@ -207,7 +207,7 @@ impl<'a> Write for &'a mut [u8] { + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Write for Vec { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { +diff --git a/memchr.rs b/memchr.rs +index a408b43..823a3ad 100644 +--- a/memchr.rs ++++ b/memchr.rs +@@ -11,103 +11,12 @@ + // 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)); +-/// ``` +-pub fn memchr(needle: u8, haystack: &[u8]) -> Option { +- // libc memchr +- #[cfg(not(target_os = "windows"))] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- use libc; +- +- let p = unsafe { +- libc::memchr( +- haystack.as_ptr() as *const libc::c_void, +- needle as libc::c_int, +- haystack.len() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- // use fallback on windows, since it's faster +- #[cfg(target_os = "windows")] +- fn memchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memchr(needle, haystack) +- } +- +- memchr_specific(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)); +-/// ``` +-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { +- +- #[cfg(target_os = "linux")] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- 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() as libc::size_t) +- }; +- if p.is_null() { +- None +- } else { +- Some(p as usize - (haystack.as_ptr() as usize)) +- } +- } +- +- #[cfg(not(target_os = "linux"))] +- fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { +- fallback::memrchr(needle, haystack) +- } +- +- memrchr_specific(needle, haystack) +-} ++pub use self::fallback::{memchr,memrchr}; + + #[allow(dead_code)] + mod fallback { +- use cmp; +- use mem; ++ use core::cmp; ++ use core::mem; + + const LO_U64: u64 = 0x0101010101010101; + const HI_U64: u64 = 0x8080808080808080; +diff --git a/mod.rs b/mod.rs +index 88fd418..94508d2 100644 +--- a/mod.rs ++++ b/mod.rs +@@ -247,49 +247,32 @@ + //! contract. The implementation of many of these functions are subject to change over + //! time and may call fewer or more syscalls/library functions. + +-#![stable(feature = "rust1", since = "1.0.0")] +- +-use cmp; ++use core::cmp; + use rustc_unicode::str as core_str; +-use error as std_error; +-use fmt; +-use iter::{Iterator}; +-use marker::Sized; +-use ops::{Drop, FnOnce}; +-use option::Option::{self, Some, None}; +-use result::Result::{Ok, Err}; +-use result; +-use string::String; +-use str; +-use vec::Vec; +-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")] ++use core::fmt; ++use core::iter::{Iterator}; ++use core::marker::Sized; ++#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; ++use core::option::Option::{self, Some, None}; ++use core::result::Result::{Ok, Err}; ++use core::result; ++#[cfg(feature="collections")] use collections::string::String; ++use core::str; ++#[cfg(feature="collections")] use collections::vec::Vec; ++mod memchr; ++ ++#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; ++#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; ++#[cfg(feature="collections")] pub use self::cursor::Cursor; + 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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; +-#[unstable(feature = "libstd_io_internals", issue = "0")] +-#[doc(no_inline, hidden)] +-pub use self::stdio::{set_panic, set_print}; + + pub mod prelude; +-mod buffered; +-mod cursor; ++#[cfg(feature="collections")] mod buffered; ++#[cfg(feature="collections")] mod cursor; + mod error; + mod impls; +-mod lazy; + mod util; +-mod stdio; + + const DEFAULT_BUF_SIZE: usize = 8 * 1024; + +@@ -311,6 +294,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024; + // 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. ++#[cfg(feature="collections")] + fn append_to_string(buf: &mut String, f: F) -> Result + where F: FnOnce(&mut Vec) -> Result + { +@@ -342,6 +326,7 @@ fn append_to_string(buf: &mut String, f: F) -> Result + // 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. ++#[cfg(feature="collections")] + fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { + let start_len = buf.len(); + let mut len = start_len; +@@ -424,7 +409,6 @@ fn read_to_end(r: &mut R, buf: &mut Vec) -> Result + /// # 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. +@@ -474,7 +458,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read(&mut self, buf: &mut [u8]) -> Result; + + /// Read all bytes until EOF in this source, placing them into `buf`. +@@ -516,7 +499,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_end(&mut self, buf: &mut Vec) -> Result { + read_to_end(self, buf) + } +@@ -554,7 +537,7 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] ++ #[cfg(feature="collections")] + fn read_to_string(&mut self, buf: &mut String) -> Result { + // Note that we do *not* call `.read_to_end()` here. We are passing + // `&mut Vec` (the raw contents of `buf`) into the `read_to_end` +@@ -615,7 +598,6 @@ pub trait Read { + /// # 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) { +@@ -667,7 +649,6 @@ pub trait Read { + /// # 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. +@@ -697,7 +678,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn bytes(self) -> Bytes where Self: Sized { + Bytes { inner: self } + } +@@ -734,10 +714,6 @@ pub trait Read { + /// # 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 where Self: Sized { + Chars { inner: self } + } +@@ -772,7 +748,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn chain(self, next: R) -> Chain where Self: Sized { + Chain { first: self, second: next, done_first: false } + } +@@ -806,7 +781,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn take(self, limit: u64) -> Take where Self: Sized { + Take { inner: self, limit: limit } + } +@@ -842,7 +816,6 @@ pub trait Read { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Write { + /// Write a buffer into this object, returning how many bytes were written. + /// +@@ -882,7 +855,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn write(&mut self, buf: &[u8]) -> Result; + + /// Flush this output stream, ensuring that all intermediately buffered +@@ -908,7 +880,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn flush(&mut self) -> Result<()>; + + /// Attempts to write an entire buffer into this write. +@@ -935,7 +906,6 @@ pub trait Write { + /// # 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) { +@@ -987,7 +957,6 @@ pub trait Write { + /// # 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 +@@ -1043,7 +1012,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn by_ref(&mut self) -> &mut Self where Self: Sized { self } + } + +@@ -1073,7 +1041,6 @@ pub trait Write { + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub trait Seek { + /// Seek to an offset, in bytes, in a stream. + /// +@@ -1089,7 +1056,6 @@ pub trait Seek { + /// 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; + } + +@@ -1099,29 +1065,26 @@ pub trait Seek { + /// + /// [`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), ++ Start(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), ++ End(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), ++ Current(i64), + } + ++#[cfg(feature="collections")] + fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + -> Result { + let mut read = 0; +@@ -1201,7 +1164,7 @@ fn read_until(r: &mut R, delim: u8, buf: &mut Vec) + /// # } + /// ``` + /// +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub trait BufRead: Read { + /// Fills the internal buffer of this object, returning the buffer contents. + /// +@@ -1246,7 +1209,6 @@ pub trait BufRead: Read { + /// // 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, +@@ -1268,7 +1230,6 @@ pub trait BufRead: Read { + /// + /// Since `consume()` is meant to be used with [`fill_buf()`][fillbuf], + /// that method's example includes an example of `consume()`. +- #[stable(feature = "rust1", since = "1.0.0")] + fn consume(&mut self, amt: usize); + + /// Read all bytes into `buf` until the delimiter `byte` is reached. +@@ -1309,7 +1270,6 @@ pub trait BufRead: Read { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + read_until(self, byte, buf) + } +@@ -1357,7 +1317,6 @@ pub trait BufRead: Read { + /// buffer.clear(); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn read_line(&mut self, buf: &mut String) -> Result { + // Note that we are not calling the `.read_until` method here, but + // rather our hardcoded implementation. For more details as to why, see +@@ -1390,7 +1349,6 @@ pub trait BufRead: Read { + /// println!("{:?}", content.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn split(self, byte: u8) -> Split where Self: Sized { + Split { buf: self, delim: byte } + } +@@ -1415,7 +1373,6 @@ pub trait BufRead: Read { + /// println!("{}", line.unwrap()); + /// } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + fn lines(self) -> Lines where Self: Sized { + Lines { buf: self } + } +@@ -1427,14 +1384,12 @@ pub trait BufRead: Read { + /// 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 { + first: T, + second: U, + done_first: bool, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Chain { + fn read(&mut self, buf: &mut [u8]) -> Result { + if !self.done_first { +@@ -1447,7 +1402,7 @@ impl Read for Chain { + } + } + +-#[stable(feature = "chain_bufread", since = "1.9.0")] ++#[cfg(feature="collections")] + impl BufRead for Chain { + fn fill_buf(&mut self) -> Result<&[u8]> { + if !self.done_first { +@@ -1474,7 +1429,6 @@ impl BufRead for Chain { + /// 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 { + inner: T, + limit: u64, +@@ -1506,11 +1460,9 @@ impl Take { + /// # Ok(()) + /// # } + /// ``` +- #[stable(feature = "rust1", since = "1.0.0")] + pub fn limit(&self) -> u64 { self.limit } + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Read for Take { + fn read(&mut self, buf: &mut [u8]) -> Result { + // Don't call into inner reader at all at EOF because it may still block +@@ -1525,7 +1477,7 @@ impl Read for Take { + } + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + // Don't call into inner reader at all at EOF because it may still block +@@ -1564,12 +1516,10 @@ fn read_one_byte(reader: &mut Read) -> Option> { + /// 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 { + inner: R, + } + +-#[stable(feature = "rust1", since = "1.0.0")] + impl Iterator for Bytes { + type Item = Result; + +@@ -1584,8 +1534,6 @@ impl Iterator for Bytes { + /// 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 { + inner: R, + } +@@ -1593,8 +1541,6 @@ pub struct Chars { + /// 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. +@@ -1604,8 +1550,6 @@ pub enum CharsError { + Other(Error), + } + +-#[unstable(feature = "io", reason = "awaiting stability of Read::chars", +- issue = "27802")] + impl Iterator for Chars { + type Item = result::Result; + +@@ -1637,25 +1581,6 @@ impl Iterator for Chars { + } + } + +-#[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 { +@@ -1674,13 +1599,13 @@ impl fmt::Display for CharsError { + /// `BufRead`. Please see the documentation of `split()` for more details. + /// + /// [split]: trait.BufRead.html#method.split +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Split { + buf: B, + delim: u8, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Split { + type Item = Result>; + +@@ -1705,12 +1630,12 @@ impl Iterator for Split { + /// `BufRead`. Please see the documentation of `lines()` for more details. + /// + /// [lines]: trait.BufRead.html#method.lines +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + pub struct Lines { + buf: B, + } + +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl Iterator for Lines { + type Item = Result; + +diff --git a/prelude.rs b/prelude.rs +index 8772d0f..49d66c9 100644 +--- a/prelude.rs ++++ b/prelude.rs +@@ -18,7 +18,8 @@ + //! use std::io::prelude::*; + //! ``` + +-#![stable(feature = "rust1", since = "1.0.0")] ++pub use super::{Read, Write, Seek}; ++#[cfg(feature="collections")] pub use super::BufRead; + +-#[stable(feature = "rust1", since = "1.0.0")] +-pub use super::{Read, Write, BufRead, Seek}; ++#[cfg(feature="collections")] pub use alloc::boxed::Box; ++#[cfg(feature="collections")] pub use collections::vec::Vec; +diff --git a/util.rs b/util.rs +index c8b52fc..aa2e47e 100644 +--- a/util.rs ++++ b/util.rs +@@ -10,7 +10,8 @@ + + #![allow(missing_copy_implementations)] + +-use io::{self, Read, Write, ErrorKind, BufRead}; ++use io::{self, Read, Write, ErrorKind}; ++#[cfg(feature="collections")] use io::BufRead; + + /// Copies the entire contents of a reader into a writer. + /// +@@ -42,7 +43,6 @@ use io::{self, Read, Write, ErrorKind, BufRead}; + /// # Ok(()) + /// # } + /// ``` +-#[stable(feature = "rust1", since = "1.0.0")] + pub fn copy(reader: &mut R, writer: &mut W) -> io::Result + where R: Read, W: Write + { +@@ -66,7 +66,6 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< + /// 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. +@@ -84,14 +83,12 @@ pub struct Empty { _priv: () } + /// 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 { Ok(0) } + } +-#[stable(feature = "rust1", since = "1.0.0")] ++#[cfg(feature="collections")] + impl BufRead for Empty { + fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } + fn consume(&mut self, _n: usize) {} +@@ -103,7 +100,6 @@ impl BufRead for Empty { + /// 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. +@@ -120,10 +116,8 @@ pub struct Repeat { byte: u8 } + /// 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 { + for slot in &mut *buf { +@@ -139,7 +133,6 @@ impl Read for Repeat { + /// 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. +@@ -156,10 +149,8 @@ pub struct Sink { _priv: () } + /// 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 { Ok(buf.len()) } + fn flush(&mut self) -> io::Result<()> { Ok(()) } diff --git a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/buffered.rs b/src/0f02309e4b0ea05ee905205278fb6d131341c41f/buffered.rs deleted file mode 100644 index dd63ebe..0000000 --- a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/0f02309e4b0ea05ee905205278fb6d131341c41f/cursor.rs b/src/0f02309e4b0ea05ee905205278fb6d131341c41f/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/error.rs b/src/0f02309e4b0ea05ee905205278fb6d131341c41f/error.rs deleted file mode 100644 index d3d0814..0000000 --- a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/error.rs +++ /dev/null @@ -1,314 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/impls.rs b/src/0f02309e4b0ea05ee905205278fb6d131341c41f/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/0f02309e4b0ea05ee905205278fb6d131341c41f/memchr.rs b/src/0f02309e4b0ea05ee905205278fb6d131341c41f/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/mod.rs b/src/0f02309e4b0ea05ee905205278fb6d131341c41f/mod.rs deleted file mode 100644 index 924d626..0000000 --- a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/mod.rs +++ /dev/null @@ -1,1780 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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 mut reader = BufReader::new(f); -//! -//! for line in reader.lines() { -//! let line = try!(line); -//! println!("{}", line); -//! } -//! -//! # Ok(()) -//! # } -//! ``` -//! -//! ## Functions -//! -//! There are a number of [functions][functions] 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]: #functions -//! -//! ## io::Result -//! -//! Last, but certainly not least, is [`io::Result`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 64 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buff - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -/// 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - fn fill_buf(&mut self) -> Result<&[u8]> { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(e) => return Some(Err(CharsError::Other(e))), - } - } - } - Some(match str::from_utf8(&buf[..width]).ok() { - Some(s) => Ok(s.char_at(0)), - None => Err(CharsError::NotUtf8), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - Err(io::Error::new(io::ErrorKind::Other, "")) - } - } - - let mut buf = [0; 1]; - assert_eq!(0, R.take(0).read(&mut buf).unwrap()); - } - - #[bench] - 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/src/0f02309e4b0ea05ee905205278fb6d131341c41f/prelude.rs b/src/0f02309e4b0ea05ee905205278fb6d131341c41f/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/util.rs b/src/0f02309e4b0ea05ee905205278fb6d131341c41f/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/0f02309e4b0ea05ee905205278fb6d131341c41f/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/buffered.rs b/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/buffered.rs deleted file mode 100644 index 83ea558..0000000 --- a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/buffered.rs +++ /dev/null @@ -1,1105 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/cursor.rs b/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/cursor.rs deleted file mode 100644 index c77f287..0000000 --- a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/cursor.rs +++ /dev/null @@ -1,571 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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::::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()); - } -} diff --git a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/error.rs b/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/error.rs deleted file mode 100644 index 748b830..0000000 --- a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/error.rs +++ /dev/null @@ -1,480 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// A marker variant that tells the compiler that users of this enum cannot - /// match it exhaustively. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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!")); - /// } - /// ``` - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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!")); - /// } - /// ``` - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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::().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()))); - /// } - /// ``` - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/impls.rs b/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/impls.rs deleted file mode 100644 index d048379..0000000 --- a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/impls.rs +++ /dev/null @@ -1,288 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/memchr.rs b/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/memchr.rs deleted file mode 100644 index f767e0c..0000000 --- a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/memchr.rs +++ /dev/null @@ -1,319 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = if end_align >= len { 0 } else { len - end_align }; - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all platforms - #[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_reversed() { - let mut data = [1u8; 64]; - let needle = 2; - let pos = 40; - data[pos] = needle; - for start in 0..16 { - assert_eq!(Some(pos - start), memrchr(needle, &data[start..])); - } - } -} - -#[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/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/mod.rs b/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/mod.rs deleted file mode 100644 index 5acfe6a..0000000 --- a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/mod.rs +++ /dev/null @@ -1,1875 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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 - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// 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)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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(()) - /// # } - /// ``` - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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> { - 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()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/prelude.rs b/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/util.rs b/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/util.rs deleted file mode 100644 index 93bcfc3..0000000 --- a/src/117cbb879e6ef498ea04e08bd80688bf2fc4a183/util.rs +++ /dev/null @@ -1,206 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { 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/src/161c541afdd18423940e97c7a02b517b1f6d61be/buffered.rs b/src/161c541afdd18423940e97c7a02b517b1f6d61be/buffered.rs deleted file mode 100644 index dd63ebe..0000000 --- a/src/161c541afdd18423940e97c7a02b517b1f6d61be/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/161c541afdd18423940e97c7a02b517b1f6d61be/cursor.rs b/src/161c541afdd18423940e97c7a02b517b1f6d61be/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/161c541afdd18423940e97c7a02b517b1f6d61be/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/161c541afdd18423940e97c7a02b517b1f6d61be/error.rs b/src/161c541afdd18423940e97c7a02b517b1f6d61be/error.rs deleted file mode 100644 index d3d0814..0000000 --- a/src/161c541afdd18423940e97c7a02b517b1f6d61be/error.rs +++ /dev/null @@ -1,314 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/161c541afdd18423940e97c7a02b517b1f6d61be/impls.rs b/src/161c541afdd18423940e97c7a02b517b1f6d61be/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/161c541afdd18423940e97c7a02b517b1f6d61be/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/161c541afdd18423940e97c7a02b517b1f6d61be/memchr.rs b/src/161c541afdd18423940e97c7a02b517b1f6d61be/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/161c541afdd18423940e97c7a02b517b1f6d61be/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/161c541afdd18423940e97c7a02b517b1f6d61be/mod.rs b/src/161c541afdd18423940e97c7a02b517b1f6d61be/mod.rs deleted file mode 100644 index d6cd245..0000000 --- a/src/161c541afdd18423940e97c7a02b517b1f6d61be/mod.rs +++ /dev/null @@ -1,1834 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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 mut reader = BufReader::new(f); -//! -//! for line in reader.lines() { -//! let line = try!(line); -//! println!("{}", line); -//! } -//! -//! # Ok(()) -//! # } -//! ``` -//! -//! ## Functions -//! -//! There are a number of [functions][functions] 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]: #functions -//! -//! ## io::Result -//! -//! Last, but certainly not least, is [`io::Result`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 64 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - fn fill_buf(&mut self) -> Result<&[u8]> { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(e) => return Some(Err(CharsError::Other(e))), - } - } - } - Some(match str::from_utf8(&buf[..width]).ok() { - Some(s) => Ok(s.char_at(0)), - None => Err(CharsError::NotUtf8), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - Err(io::Error::new(io::ErrorKind::Other, "")) - } - } - - let mut buf = [0; 1]; - assert_eq!(0, R.take(0).read(&mut buf).unwrap()); - } - - fn cmp_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[..]); - } - - #[bench] - 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/src/161c541afdd18423940e97c7a02b517b1f6d61be/prelude.rs b/src/161c541afdd18423940e97c7a02b517b1f6d61be/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/161c541afdd18423940e97c7a02b517b1f6d61be/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/161c541afdd18423940e97c7a02b517b1f6d61be/util.rs b/src/161c541afdd18423940e97c7a02b517b1f6d61be/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/161c541afdd18423940e97c7a02b517b1f6d61be/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/buffered.rs b/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/buffered.rs deleted file mode 100644 index 21a0cc1..0000000 --- a/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/buffered.rs +++ /dev/null @@ -1,1146 +0,0 @@ -// 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 or the MIT license -// , 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 marker::Reflect; -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. -/// -/// # 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 { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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 { - 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 { - 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 Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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 BufRead for BufReader { - 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 fmt::Debug for BufReader 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 Seek for BufReader { - /// 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 { - 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 ebibytes 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. -#[stable(feature = "rust1", since = "1.0.0")] -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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, Error); - -impl BufWriter { - /// 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 { - 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 { - 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>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 fmt::Debug for BufWriter 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 Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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 From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl error::Error for IntoInnerError { - fn description(&self) -> &str { - error::Error::description(self.error()) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Display for IntoInnerError { - 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 { - inner: BufWriter, -} - -impl LineWriter { - /// 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 { - // 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 { - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 fmt::Debug for LineWriter 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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 { - 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/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/cursor.rs b/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/cursor.rs deleted file mode 100644 index ae0085f..0000000 --- a/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/cursor.rs +++ /dev/null @@ -1,594 +0,0 @@ -// 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 or the MIT license -// , 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`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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 { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # 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>) {} - /// # 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>) {} - /// # 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 io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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 Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl BufRead for Cursor 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 { - 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> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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::::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(::max_value() as u64 + 1); - assert!(c.write_all(&[1, 2, 3]).is_err()); - } -} diff --git a/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/error.rs b/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/error.rs deleted file mode 100644 index ddf0030..0000000 --- a/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/error.rs +++ /dev/null @@ -1,562 +0,0 @@ -// 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 or the MIT license -// , 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; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -#[stable(feature = "rust1", since = "1.0.0")] -pub type Result = result::Result; - -/// 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), - Custom(Box), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: Box, -} - -/// 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. - #[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. - #[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 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(kind: ErrorKind, error: E) -> Error - where E: Into> - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: Box) -> 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 { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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::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::().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::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> { - match self.repr { - Repr::Os(..) => 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, - } - } -} - -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(), - } - } -} - -#[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), - } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl error::Error for Error { - fn description(&self) -> &str { - match self.repr { - Repr::Os(..) => match self.kind() { - 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!() - }, - Repr::Custom(ref c) => c.error.description(), - } - } - - fn cause(&self) -> Option<&error::Error> { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(ref c) => c.error.cause(), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/impls.rs b/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/impls.rs deleted file mode 100644 index cd05e6b..0000000 --- a/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/impls.rs +++ /dev/null @@ -1,285 +0,0 @@ -// 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 or the MIT license -// , 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 { - (**self).read(buf) - } - - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**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 { (**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 { (**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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**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 Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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 Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[stable(feature = "rust1", since = "1.0.0")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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..]; } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/memchr.rs b/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/memchr.rs deleted file mode 100644 index 7c8c97a..0000000 --- a/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/memchr.rs +++ /dev/null @@ -1,143 +0,0 @@ -// 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 or the MIT license -// , 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 { - ::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 { - ::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/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/mod.rs b/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/mod.rs deleted file mode 100644 index 7310564..0000000 --- a/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/mod.rs +++ /dev/null @@ -1,1979 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try.html -//! -//! ## 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. - -#![stable(feature = "rust1", since = "1.0.0")] - -use cmp; -use rustc_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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; -#[stable(feature = "rust1", since = "1.0.0")] -pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; -#[unstable(feature = "libstd_io_internals", issue = "0")] -#[doc(no_inline, hidden)] -pub use self::stdio::{set_panic, set_print}; - -pub mod prelude; -mod buffered; -mod cursor; -mod error; -mod impls; -mod lazy; -mod util; -mod stdio; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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: &mut R, buf: &mut Vec) -> Result { - 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; - - /// 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) -> Result { - 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 { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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`. 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 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` 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 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(self, next: R) -> Chain 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 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; - - /// 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; -} - -/// 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: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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`][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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - #[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. - /// - /// ``` - /// 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) -> Result { - 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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 { - // 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>`. 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. - /// - /// ``` - /// 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 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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 where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][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 { - first: T, - second: U, - done_first: bool, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - 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 BufRead for Chain { - 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 { - inner: T, - limit: u64, -} - -impl Take { - /// 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 Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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 BufRead for Take { - 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> { - 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()`][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 { - inner: R, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 { - 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 Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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 { - buf: B, - delim: u8, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 { - buf: B, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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::>(); - 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 { - 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(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/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/prelude.rs b/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/prelude.rs deleted file mode 100644 index 8772d0f..0000000 --- a/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/prelude.rs +++ /dev/null @@ -1,24 +0,0 @@ -// 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 or the MIT license -// , 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/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/util.rs b/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/util.rs deleted file mode 100644 index 2c68802..0000000 --- a/src/1f9036872d7ea8bee91f8588e3c4a613c58c76cb/util.rs +++ /dev/null @@ -1,215 +0,0 @@ -// 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 or the MIT license -// , 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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -#[stable(feature = "rust1", since = "1.0.0")] -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 { 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 { - 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 { 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/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/buffered.rs b/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/buffered.rs deleted file mode 100644 index fd4eb13..0000000 --- a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/buffered.rs +++ /dev/null @@ -1,1104 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/cursor.rs b/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/cursor.rs deleted file mode 100644 index c77f287..0000000 --- a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/cursor.rs +++ /dev/null @@ -1,571 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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::::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()); - } -} diff --git a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/error.rs b/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/error.rs deleted file mode 100644 index 748b830..0000000 --- a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/error.rs +++ /dev/null @@ -1,480 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// A marker variant that tells the compiler that users of this enum cannot - /// match it exhaustively. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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!")); - /// } - /// ``` - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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!")); - /// } - /// ``` - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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::().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()))); - /// } - /// ``` - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/impls.rs b/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/impls.rs deleted file mode 100644 index d048379..0000000 --- a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/impls.rs +++ /dev/null @@ -1,288 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/memchr.rs b/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/memchr.rs deleted file mode 100644 index f767e0c..0000000 --- a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/memchr.rs +++ /dev/null @@ -1,319 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = if end_align >= len { 0 } else { len - end_align }; - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all platforms - #[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_reversed() { - let mut data = [1u8; 64]; - let needle = 2; - let pos = 40; - data[pos] = needle; - for start in 0..16 { - assert_eq!(Some(pos - start), memrchr(needle, &data[start..])); - } - } -} - -#[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/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/mod.rs b/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/mod.rs deleted file mode 100644 index 0f202c2..0000000 --- a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/mod.rs +++ /dev/null @@ -1,1912 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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 - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// 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)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> T { - self.inner - } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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> { - 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()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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] - 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::>(); - 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 { - 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(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] - 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/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/prelude.rs b/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/util.rs b/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/util.rs deleted file mode 100644 index 93bcfc3..0000000 --- a/src/26f9949bf678abc1fae595e3f6eb59a5bf8a7564/util.rs +++ /dev/null @@ -1,206 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { 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/src/552eda70d33cead1398adfecce1a75e7a61e3daf/buffered.rs b/src/552eda70d33cead1398adfecce1a75e7a61e3daf/buffered.rs deleted file mode 100644 index dd63ebe..0000000 --- a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/552eda70d33cead1398adfecce1a75e7a61e3daf/cursor.rs b/src/552eda70d33cead1398adfecce1a75e7a61e3daf/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/error.rs b/src/552eda70d33cead1398adfecce1a75e7a61e3daf/error.rs deleted file mode 100644 index d3d0814..0000000 --- a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/error.rs +++ /dev/null @@ -1,314 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/impls.rs b/src/552eda70d33cead1398adfecce1a75e7a61e3daf/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/552eda70d33cead1398adfecce1a75e7a61e3daf/memchr.rs b/src/552eda70d33cead1398adfecce1a75e7a61e3daf/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/mod.rs b/src/552eda70d33cead1398adfecce1a75e7a61e3daf/mod.rs deleted file mode 100644 index ba07b2c..0000000 --- a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/mod.rs +++ /dev/null @@ -1,1834 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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 mut reader = BufReader::new(f); -//! -//! for line in reader.lines() { -//! let line = try!(line); -//! println!("{}", line); -//! } -//! -//! # Ok(()) -//! # } -//! ``` -//! -//! ## Functions -//! -//! There are a number of [functions][functions] 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]: #functions -//! -//! ## io::Result -//! -//! Last, but certainly not least, is [`io::Result`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - fn fill_buf(&mut self) -> Result<&[u8]> { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - Err(io::Error::new(io::ErrorKind::Other, "")) - } - } - - let mut buf = [0; 1]; - assert_eq!(0, R.take(0).read(&mut buf).unwrap()); - } - - fn cmp_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[..]); - } - - #[bench] - 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/src/552eda70d33cead1398adfecce1a75e7a61e3daf/prelude.rs b/src/552eda70d33cead1398adfecce1a75e7a61e3daf/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/util.rs b/src/552eda70d33cead1398adfecce1a75e7a61e3daf/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/552eda70d33cead1398adfecce1a75e7a61e3daf/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/62b19c627ebde2bbfa6021de146c502124da7975/buffered.rs b/src/62b19c627ebde2bbfa6021de146c502124da7975/buffered.rs deleted file mode 100644 index e23b74f..0000000 --- a/src/62b19c627ebde2bbfa6021de146c502124da7975/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/62b19c627ebde2bbfa6021de146c502124da7975/cursor.rs b/src/62b19c627ebde2bbfa6021de146c502124da7975/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/62b19c627ebde2bbfa6021de146c502124da7975/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/62b19c627ebde2bbfa6021de146c502124da7975/error.rs b/src/62b19c627ebde2bbfa6021de146c502124da7975/error.rs deleted file mode 100644 index eeb11d5..0000000 --- a/src/62b19c627ebde2bbfa6021de146c502124da7975/error.rs +++ /dev/null @@ -1,313 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - 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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/62b19c627ebde2bbfa6021de146c502124da7975/impls.rs b/src/62b19c627ebde2bbfa6021de146c502124da7975/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/62b19c627ebde2bbfa6021de146c502124da7975/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/62b19c627ebde2bbfa6021de146c502124da7975/memchr.rs b/src/62b19c627ebde2bbfa6021de146c502124da7975/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/62b19c627ebde2bbfa6021de146c502124da7975/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/62b19c627ebde2bbfa6021de146c502124da7975/mod.rs b/src/62b19c627ebde2bbfa6021de146c502124da7975/mod.rs deleted file mode 100644 index d190cba..0000000 --- a/src/62b19c627ebde2bbfa6021de146c502124da7975/mod.rs +++ /dev/null @@ -1,1845 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/62b19c627ebde2bbfa6021de146c502124da7975/prelude.rs b/src/62b19c627ebde2bbfa6021de146c502124da7975/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/62b19c627ebde2bbfa6021de146c502124da7975/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/62b19c627ebde2bbfa6021de146c502124da7975/util.rs b/src/62b19c627ebde2bbfa6021de146c502124da7975/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/62b19c627ebde2bbfa6021de146c502124da7975/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/78ab18199d69bcc801668bfbeea8190b2c73a939/buffered.rs b/src/78ab18199d69bcc801668bfbeea8190b2c73a939/buffered.rs deleted file mode 100644 index dd63ebe..0000000 --- a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/78ab18199d69bcc801668bfbeea8190b2c73a939/cursor.rs b/src/78ab18199d69bcc801668bfbeea8190b2c73a939/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/error.rs b/src/78ab18199d69bcc801668bfbeea8190b2c73a939/error.rs deleted file mode 100644 index d3d0814..0000000 --- a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/error.rs +++ /dev/null @@ -1,314 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/impls.rs b/src/78ab18199d69bcc801668bfbeea8190b2c73a939/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/78ab18199d69bcc801668bfbeea8190b2c73a939/memchr.rs b/src/78ab18199d69bcc801668bfbeea8190b2c73a939/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/mod.rs b/src/78ab18199d69bcc801668bfbeea8190b2c73a939/mod.rs deleted file mode 100644 index d17f3cf..0000000 --- a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/mod.rs +++ /dev/null @@ -1,1846 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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 mut reader = BufReader::new(f); -//! -//! for line in reader.lines() { -//! let line = try!(line); -//! println!("{}", line); -//! } -//! -//! # Ok(()) -//! # } -//! ``` -//! -//! ## Functions -//! -//! There are a number of [functions][functions] 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]: #functions -//! -//! ## io::Result -//! -//! Last, but certainly not least, is [`io::Result`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/78ab18199d69bcc801668bfbeea8190b2c73a939/prelude.rs b/src/78ab18199d69bcc801668bfbeea8190b2c73a939/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/util.rs b/src/78ab18199d69bcc801668bfbeea8190b2c73a939/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/78ab18199d69bcc801668bfbeea8190b2c73a939/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/buffered.rs b/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/buffered.rs deleted file mode 100644 index e23b74f..0000000 --- a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/cursor.rs b/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/cursor.rs deleted file mode 100644 index 1db50ee..0000000 --- a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/cursor.rs +++ /dev/null @@ -1,572 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/error.rs b/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/error.rs deleted file mode 100644 index eeb11d5..0000000 --- a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/error.rs +++ /dev/null @@ -1,313 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - 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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/impls.rs b/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/memchr.rs b/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/mod.rs b/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/mod.rs deleted file mode 100644 index d190cba..0000000 --- a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/mod.rs +++ /dev/null @@ -1,1845 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/prelude.rs b/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/util.rs b/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/80d733385aa2ff150a5d6f83ecfe55afc7e19e68/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/8128817119e479b0610685e3fc7a6ff21cde5abc/buffered.rs b/src/8128817119e479b0610685e3fc7a6ff21cde5abc/buffered.rs deleted file mode 100644 index dd63ebe..0000000 --- a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/8128817119e479b0610685e3fc7a6ff21cde5abc/cursor.rs b/src/8128817119e479b0610685e3fc7a6ff21cde5abc/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/error.rs b/src/8128817119e479b0610685e3fc7a6ff21cde5abc/error.rs deleted file mode 100644 index d3d0814..0000000 --- a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/error.rs +++ /dev/null @@ -1,314 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/impls.rs b/src/8128817119e479b0610685e3fc7a6ff21cde5abc/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/8128817119e479b0610685e3fc7a6ff21cde5abc/memchr.rs b/src/8128817119e479b0610685e3fc7a6ff21cde5abc/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/mod.rs b/src/8128817119e479b0610685e3fc7a6ff21cde5abc/mod.rs deleted file mode 100644 index 4a492d7..0000000 --- a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/mod.rs +++ /dev/null @@ -1,1834 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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 mut reader = BufReader::new(f); -//! -//! for line in reader.lines() { -//! let line = try!(line); -//! println!("{}", line); -//! } -//! -//! # Ok(()) -//! # } -//! ``` -//! -//! ## Functions -//! -//! There are a number of [functions][functions] 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]: #functions -//! -//! ## io::Result -//! -//! Last, but certainly not least, is [`io::Result`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - fn fill_buf(&mut self) -> Result<&[u8]> { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(e) => return Some(Err(CharsError::Other(e))), - } - } - } - Some(match str::from_utf8(&buf[..width]).ok() { - Some(s) => Ok(s.char_at(0)), - None => Err(CharsError::NotUtf8), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - Err(io::Error::new(io::ErrorKind::Other, "")) - } - } - - let mut buf = [0; 1]; - assert_eq!(0, R.take(0).read(&mut buf).unwrap()); - } - - fn cmp_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[..]); - } - - #[bench] - 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/src/8128817119e479b0610685e3fc7a6ff21cde5abc/prelude.rs b/src/8128817119e479b0610685e3fc7a6ff21cde5abc/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/util.rs b/src/8128817119e479b0610685e3fc7a6ff21cde5abc/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/8128817119e479b0610685e3fc7a6ff21cde5abc/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/buffered.rs b/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/buffered.rs deleted file mode 100644 index dd63ebe..0000000 --- a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/cursor.rs b/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/error.rs b/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/error.rs deleted file mode 100644 index d3d0814..0000000 --- a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/error.rs +++ /dev/null @@ -1,314 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/impls.rs b/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/memchr.rs b/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/mod.rs b/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/mod.rs deleted file mode 100644 index 924d626..0000000 --- a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/mod.rs +++ /dev/null @@ -1,1780 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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 mut reader = BufReader::new(f); -//! -//! for line in reader.lines() { -//! let line = try!(line); -//! println!("{}", line); -//! } -//! -//! # Ok(()) -//! # } -//! ``` -//! -//! ## Functions -//! -//! There are a number of [functions][functions] 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]: #functions -//! -//! ## io::Result -//! -//! Last, but certainly not least, is [`io::Result`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 64 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buff - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -/// 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - fn fill_buf(&mut self) -> Result<&[u8]> { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(e) => return Some(Err(CharsError::Other(e))), - } - } - } - Some(match str::from_utf8(&buf[..width]).ok() { - Some(s) => Ok(s.char_at(0)), - None => Err(CharsError::NotUtf8), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - Err(io::Error::new(io::ErrorKind::Other, "")) - } - } - - let mut buf = [0; 1]; - assert_eq!(0, R.take(0).read(&mut buf).unwrap()); - } - - #[bench] - 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/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/prelude.rs b/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/util.rs b/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/87aee45988e81cb1a7bc9881aa7172d4f9caefd4/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/8d06332a27b020f5252238946fa9dccc3843d52a/buffered.rs b/src/8d06332a27b020f5252238946fa9dccc3843d52a/buffered.rs deleted file mode 100644 index 93b2d34..0000000 --- a/src/8d06332a27b020f5252238946fa9dccc3843d52a/buffered.rs +++ /dev/null @@ -1,1146 +0,0 @@ -// 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 or the MIT license -// , 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 marker::Reflect; -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. -/// -/// # 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 { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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 { - 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 { - 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 Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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 BufRead for BufReader { - 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 fmt::Debug for BufReader 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 Seek for BufReader { - /// 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 { - 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. -#[stable(feature = "rust1", since = "1.0.0")] -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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, Error); - -impl BufWriter { - /// 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 { - 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 { - 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>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 fmt::Debug for BufWriter 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 Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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 From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl error::Error for IntoInnerError { - fn description(&self) -> &str { - error::Error::description(self.error()) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Display for IntoInnerError { - 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 { - inner: BufWriter, -} - -impl LineWriter { - /// 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 { - // 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 { - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 fmt::Debug for LineWriter 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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 { - 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/src/8d06332a27b020f5252238946fa9dccc3843d52a/cursor.rs b/src/8d06332a27b020f5252238946fa9dccc3843d52a/cursor.rs deleted file mode 100644 index ae0085f..0000000 --- a/src/8d06332a27b020f5252238946fa9dccc3843d52a/cursor.rs +++ /dev/null @@ -1,594 +0,0 @@ -// 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 or the MIT license -// , 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`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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 { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # 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>) {} - /// # 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>) {} - /// # 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 io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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 Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl BufRead for Cursor 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 { - 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> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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::::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(::max_value() as u64 + 1); - assert!(c.write_all(&[1, 2, 3]).is_err()); - } -} diff --git a/src/8d06332a27b020f5252238946fa9dccc3843d52a/error.rs b/src/8d06332a27b020f5252238946fa9dccc3843d52a/error.rs deleted file mode 100644 index ddf0030..0000000 --- a/src/8d06332a27b020f5252238946fa9dccc3843d52a/error.rs +++ /dev/null @@ -1,562 +0,0 @@ -// 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 or the MIT license -// , 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; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -#[stable(feature = "rust1", since = "1.0.0")] -pub type Result = result::Result; - -/// 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), - Custom(Box), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: Box, -} - -/// 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. - #[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. - #[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 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(kind: ErrorKind, error: E) -> Error - where E: Into> - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: Box) -> 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 { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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::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::().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::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> { - match self.repr { - Repr::Os(..) => 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, - } - } -} - -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(), - } - } -} - -#[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), - } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl error::Error for Error { - fn description(&self) -> &str { - match self.repr { - Repr::Os(..) => match self.kind() { - 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!() - }, - Repr::Custom(ref c) => c.error.description(), - } - } - - fn cause(&self) -> Option<&error::Error> { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(ref c) => c.error.cause(), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/8d06332a27b020f5252238946fa9dccc3843d52a/impls.rs b/src/8d06332a27b020f5252238946fa9dccc3843d52a/impls.rs deleted file mode 100644 index cd05e6b..0000000 --- a/src/8d06332a27b020f5252238946fa9dccc3843d52a/impls.rs +++ /dev/null @@ -1,285 +0,0 @@ -// 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 or the MIT license -// , 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 { - (**self).read(buf) - } - - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**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 { (**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 { (**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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**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 Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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 Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[stable(feature = "rust1", since = "1.0.0")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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..]; } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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/src/8d06332a27b020f5252238946fa9dccc3843d52a/memchr.rs b/src/8d06332a27b020f5252238946fa9dccc3843d52a/memchr.rs deleted file mode 100644 index 7c8c97a..0000000 --- a/src/8d06332a27b020f5252238946fa9dccc3843d52a/memchr.rs +++ /dev/null @@ -1,143 +0,0 @@ -// 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 or the MIT license -// , 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 { - ::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 { - ::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/src/8d06332a27b020f5252238946fa9dccc3843d52a/mod.rs b/src/8d06332a27b020f5252238946fa9dccc3843d52a/mod.rs deleted file mode 100644 index 7310564..0000000 --- a/src/8d06332a27b020f5252238946fa9dccc3843d52a/mod.rs +++ /dev/null @@ -1,1979 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try.html -//! -//! ## 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. - -#![stable(feature = "rust1", since = "1.0.0")] - -use cmp; -use rustc_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::stdio::{stdin, stdout, stderr, _print, Stdin, Stdout, Stderr}; -#[stable(feature = "rust1", since = "1.0.0")] -pub use self::stdio::{StdoutLock, StderrLock, StdinLock}; -#[unstable(feature = "libstd_io_internals", issue = "0")] -#[doc(no_inline, hidden)] -pub use self::stdio::{set_panic, set_print}; - -pub mod prelude; -mod buffered; -mod cursor; -mod error; -mod impls; -mod lazy; -mod util; -mod stdio; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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: &mut R, buf: &mut Vec) -> Result { - 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; - - /// 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) -> Result { - 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 { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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`. 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 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` 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 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(self, next: R) -> Chain 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 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; - - /// 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; -} - -/// 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: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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`][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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - #[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. - /// - /// ``` - /// 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) -> Result { - 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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 { - // 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>`. 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. - /// - /// ``` - /// 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 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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 where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][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 { - first: T, - second: U, - done_first: bool, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - 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 BufRead for Chain { - 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 { - inner: T, - limit: u64, -} - -impl Take { - /// 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 Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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 BufRead for Take { - 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> { - 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()`][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 { - inner: R, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 { - 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 Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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 { - buf: B, - delim: u8, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 { - buf: B, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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::>(); - 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 { - 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(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/src/8d06332a27b020f5252238946fa9dccc3843d52a/prelude.rs b/src/8d06332a27b020f5252238946fa9dccc3843d52a/prelude.rs deleted file mode 100644 index 8772d0f..0000000 --- a/src/8d06332a27b020f5252238946fa9dccc3843d52a/prelude.rs +++ /dev/null @@ -1,24 +0,0 @@ -// 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 or the MIT license -// , 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/src/8d06332a27b020f5252238946fa9dccc3843d52a/util.rs b/src/8d06332a27b020f5252238946fa9dccc3843d52a/util.rs deleted file mode 100644 index 2c68802..0000000 --- a/src/8d06332a27b020f5252238946fa9dccc3843d52a/util.rs +++ /dev/null @@ -1,215 +0,0 @@ -// 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 or the MIT license -// , 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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -#[stable(feature = "rust1", since = "1.0.0")] -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 { 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 { - 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 { 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/src/8e414e0e3f27d1917d11ee80de827698beb53891/buffered.rs b/src/8e414e0e3f27d1917d11ee80de827698beb53891/buffered.rs deleted file mode 100644 index dd63ebe..0000000 --- a/src/8e414e0e3f27d1917d11ee80de827698beb53891/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/8e414e0e3f27d1917d11ee80de827698beb53891/cursor.rs b/src/8e414e0e3f27d1917d11ee80de827698beb53891/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/8e414e0e3f27d1917d11ee80de827698beb53891/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/8e414e0e3f27d1917d11ee80de827698beb53891/error.rs b/src/8e414e0e3f27d1917d11ee80de827698beb53891/error.rs deleted file mode 100644 index eeb11d5..0000000 --- a/src/8e414e0e3f27d1917d11ee80de827698beb53891/error.rs +++ /dev/null @@ -1,313 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - 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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/8e414e0e3f27d1917d11ee80de827698beb53891/impls.rs b/src/8e414e0e3f27d1917d11ee80de827698beb53891/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/8e414e0e3f27d1917d11ee80de827698beb53891/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/8e414e0e3f27d1917d11ee80de827698beb53891/memchr.rs b/src/8e414e0e3f27d1917d11ee80de827698beb53891/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/8e414e0e3f27d1917d11ee80de827698beb53891/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/8e414e0e3f27d1917d11ee80de827698beb53891/mod.rs b/src/8e414e0e3f27d1917d11ee80de827698beb53891/mod.rs deleted file mode 100644 index d190cba..0000000 --- a/src/8e414e0e3f27d1917d11ee80de827698beb53891/mod.rs +++ /dev/null @@ -1,1845 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/8e414e0e3f27d1917d11ee80de827698beb53891/prelude.rs b/src/8e414e0e3f27d1917d11ee80de827698beb53891/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/8e414e0e3f27d1917d11ee80de827698beb53891/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/8e414e0e3f27d1917d11ee80de827698beb53891/util.rs b/src/8e414e0e3f27d1917d11ee80de827698beb53891/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/8e414e0e3f27d1917d11ee80de827698beb53891/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/buffered.rs b/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/buffered.rs deleted file mode 100644 index e23b74f..0000000 --- a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/cursor.rs b/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/cursor.rs deleted file mode 100644 index 1db50ee..0000000 --- a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/cursor.rs +++ /dev/null @@ -1,572 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/error.rs b/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/error.rs deleted file mode 100644 index 5da1224..0000000 --- a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/error.rs +++ /dev/null @@ -1,337 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - 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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/impls.rs b/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/memchr.rs b/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/memchr.rs deleted file mode 100644 index 823a3ad..0000000 --- a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all platforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/mod.rs b/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/mod.rs deleted file mode 100644 index c14c50e..0000000 --- a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/mod.rs +++ /dev/null @@ -1,1852 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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> { - 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()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/prelude.rs b/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/util.rs b/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/util.rs deleted file mode 100644 index aa2e47e..0000000 --- a/src/92400cf8dcf411ce7e70ab2960639977d46d5b01/util.rs +++ /dev/null @@ -1,208 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/98e3120ad218b8d9c50e25a525dcff689c515776/buffered.rs b/src/98e3120ad218b8d9c50e25a525dcff689c515776/buffered.rs deleted file mode 100644 index e23b74f..0000000 --- a/src/98e3120ad218b8d9c50e25a525dcff689c515776/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/98e3120ad218b8d9c50e25a525dcff689c515776/cursor.rs b/src/98e3120ad218b8d9c50e25a525dcff689c515776/cursor.rs deleted file mode 100644 index 1db50ee..0000000 --- a/src/98e3120ad218b8d9c50e25a525dcff689c515776/cursor.rs +++ /dev/null @@ -1,572 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/98e3120ad218b8d9c50e25a525dcff689c515776/error.rs b/src/98e3120ad218b8d9c50e25a525dcff689c515776/error.rs deleted file mode 100644 index 5da1224..0000000 --- a/src/98e3120ad218b8d9c50e25a525dcff689c515776/error.rs +++ /dev/null @@ -1,337 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - 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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/98e3120ad218b8d9c50e25a525dcff689c515776/impls.rs b/src/98e3120ad218b8d9c50e25a525dcff689c515776/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/98e3120ad218b8d9c50e25a525dcff689c515776/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/98e3120ad218b8d9c50e25a525dcff689c515776/memchr.rs b/src/98e3120ad218b8d9c50e25a525dcff689c515776/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/98e3120ad218b8d9c50e25a525dcff689c515776/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/98e3120ad218b8d9c50e25a525dcff689c515776/mod.rs b/src/98e3120ad218b8d9c50e25a525dcff689c515776/mod.rs deleted file mode 100644 index d190cba..0000000 --- a/src/98e3120ad218b8d9c50e25a525dcff689c515776/mod.rs +++ /dev/null @@ -1,1845 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/98e3120ad218b8d9c50e25a525dcff689c515776/prelude.rs b/src/98e3120ad218b8d9c50e25a525dcff689c515776/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/98e3120ad218b8d9c50e25a525dcff689c515776/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/98e3120ad218b8d9c50e25a525dcff689c515776/util.rs b/src/98e3120ad218b8d9c50e25a525dcff689c515776/util.rs deleted file mode 100644 index f7aa6f7..0000000 --- a/src/98e3120ad218b8d9c50e25a525dcff689c515776/util.rs +++ /dev/null @@ -1,208 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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 mut buffer = vec![1, 2, 3, 5, 8]; -/// let num_bytes = io::sink().write(&mut buffer).unwrap(); -/// assert_eq!(num_bytes, 5); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/buffered.rs b/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/buffered.rs deleted file mode 100644 index 83ea558..0000000 --- a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/buffered.rs +++ /dev/null @@ -1,1105 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/cursor.rs b/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/cursor.rs deleted file mode 100644 index c77f287..0000000 --- a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/cursor.rs +++ /dev/null @@ -1,571 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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::::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()); - } -} diff --git a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/error.rs b/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/error.rs deleted file mode 100644 index d23baa8..0000000 --- a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/error.rs +++ /dev/null @@ -1,479 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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!")); - /// } - /// ``` - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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!")); - /// } - /// ``` - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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::().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()))); - /// } - /// ``` - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/impls.rs b/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/impls.rs deleted file mode 100644 index d048379..0000000 --- a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/impls.rs +++ /dev/null @@ -1,288 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/memchr.rs b/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/memchr.rs deleted file mode 100644 index 823a3ad..0000000 --- a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all platforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/mod.rs b/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/mod.rs deleted file mode 100644 index 5acfe6a..0000000 --- a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/mod.rs +++ /dev/null @@ -1,1875 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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 - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// 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)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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(()) - /// # } - /// ``` - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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> { - 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()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/prelude.rs b/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/util.rs b/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/util.rs deleted file mode 100644 index 93bcfc3..0000000 --- a/src/9a2c8783d91624261317316f996d8d2d09b7b6a4/util.rs +++ /dev/null @@ -1,206 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { 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/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/buffered.rs b/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/buffered.rs deleted file mode 100644 index dd63ebe..0000000 --- a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/cursor.rs b/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/error.rs b/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/error.rs deleted file mode 100644 index d3d0814..0000000 --- a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/error.rs +++ /dev/null @@ -1,314 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/impls.rs b/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/memchr.rs b/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/mod.rs b/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/mod.rs deleted file mode 100644 index d190cba..0000000 --- a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/mod.rs +++ /dev/null @@ -1,1845 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/prelude.rs b/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/util.rs b/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/9f935c8dd891ec6eb0809b8438656d1b39c2e2f5/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/buffered.rs b/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/buffered.rs deleted file mode 100644 index dd63ebe..0000000 --- a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/cursor.rs b/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/error.rs b/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/error.rs deleted file mode 100644 index d3d0814..0000000 --- a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/error.rs +++ /dev/null @@ -1,314 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/impls.rs b/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/memchr.rs b/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/mod.rs b/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/mod.rs deleted file mode 100644 index 900ee2f..0000000 --- a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/mod.rs +++ /dev/null @@ -1,1846 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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 mut reader = BufReader::new(f); -//! -//! for line in reader.lines() { -//! let line = try!(line); -//! println!("{}", 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/prelude.rs b/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/util.rs b/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/9fe3c065b0e94b1e2ce7f14ab512475e79426ce4/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/c1fb50f5d377a41dd5833e4621e9a14879647503/buffered.rs b/src/c1fb50f5d377a41dd5833e4621e9a14879647503/buffered.rs deleted file mode 100644 index a1af9d9..0000000 --- a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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 = try!(self.fill_buf()); - try!(rem.read(buf)) - }; - self.consume(nread); - Ok(nread) - } -} - -impl BufRead for BufReader { - 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 = try!(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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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 = try!(self.inner.seek(SeekFrom::Current(offset))); - } else { - // seek backwards by our remainder, and then by the offset - try!(self.inner.seek(SeekFrom::Current(-remainder))); - self.pos = self.cap; // empty the buffer - result = try!(self.inner.seek(SeekFrom::Current(n))); - } - } else { - // Seeking with Start/End doesn't care about our buffer length. - result = try!(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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - if self.buf.len() + buf.len() > self.buf.capacity() { - try!(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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - match memchr::memrchr(b'\n', buf) { - Some(i) => { - let n = try!(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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/c1fb50f5d377a41dd5833e4621e9a14879647503/cursor.rs b/src/c1fb50f5d377a41dd5833e4621e9a14879647503/cursor.rs deleted file mode 100644 index 670e362..0000000 --- a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = try!(Read::read(&mut try!(self.fill_buf()), buf)); - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - let pos = cmp::min(self.pos, self.inner.len() as u64); - let amt = try!((&mut self.inner[(pos as usize)..]).write(data)); - self.pos += amt as u64; - Ok(amt) - } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - let pos = cmp::min(self.pos, self.inner.len() as u64); - let amt = try!((&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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/error.rs b/src/c1fb50f5d377a41dd5833e4621e9a14879647503/error.rs deleted file mode 100644 index 50e3658..0000000 --- a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/error.rs +++ /dev/null @@ -1,317 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - Other, - - #[allow(missing_docs)] - UnexpectedEOF, - - /// 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/impls.rs b/src/c1fb50f5d377a41dd5833e4621e9a14879647503/impls.rs deleted file mode 100644 index ac172bf..0000000 --- a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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 try!(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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/c1fb50f5d377a41dd5833e4621e9a14879647503/memchr.rs b/src/c1fb50f5d377a41dd5833e4621e9a14879647503/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/mod.rs b/src/c1fb50f5d377a41dd5833e4621e9a14879647503/mod.rs deleted file mode 100644 index 9d9263f..0000000 --- a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/mod.rs +++ /dev/null @@ -1,1896 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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 mut reader = BufReader::new(f); -//! -//! for line in reader.lines() { -//! let line = try!(line); -//! println!("{}", line); -//! } -//! -//! # Ok(()) -//! # } -//! ``` -//! -//! ## Functions -//! -//! There are a number of [functions][functions] 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]: #functions -//! -//! ## io::Result -//! -//! Last, but certainly not least, is [`io::Result`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 64 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take where Self: Sized { - Take { inner: self, limit: limit } - } - - /// Creates a reader adaptor which will write all read data into the given - /// output stream. - /// - /// Whenever the returned `Read` instance is read it will write the read - /// data to `out`. The current semantics of this implementation imply that - /// a `write` error will not report how much data was initially read. - /// - /// # 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")); - /// let mut buffer1 = Vec::with_capacity(10); - /// let mut buffer2 = Vec::with_capacity(10); - /// - /// // write the output to buffer1 as we read - /// let mut handle = f.tee(&mut buffer1); - /// - /// try!(handle.read(&mut buffer2)); - /// # Ok(()) - /// # } - /// ``` - #[allow(deprecated)] - fn tee(self, out: W) -> Tee where Self: Sized { - Tee { reader: self, writer: out } - } -} - -/// 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - fn by_ref(&mut self) -> &mut Self where Self: Sized { self } - - /// Creates a new writer which will write all data to both this writer and - /// another writer. - /// - /// All data written to the returned writer will both be written to `self` - /// as well as `other`. Note that the error semantics of the current - /// implementation do not precisely track where errors happen. For example - /// an error on the second call to `write` will not report that the first - /// call to `write` succeeded. - /// - /// # Examples - /// - /// ``` - /// #![feature(io)] - /// use std::io::prelude::*; - /// use std::fs::File; - /// - /// # fn foo() -> std::io::Result<()> { - /// let mut buffer1 = try!(File::create("foo.txt")); - /// let mut buffer2 = Vec::new(); - /// - /// // write the output to buffer1 as we read - /// let mut handle = buffer1.broadcast(&mut buffer2); - /// - /// try!(handle.write(b"some bytes")); - /// # Ok(()) - /// # } - /// ``` - #[allow(deprecated)] - fn broadcast(self, other: W) -> Broadcast - where Self: Sized - { - Broadcast { first: self, second: other } - } -} - -/// 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buff - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// A `Write` adaptor which will write data to multiple locations. -/// -/// This struct is generally created by calling [`broadcast()`][broadcast] on a -/// writer. Please see the documentation of `broadcast()` for more details. -/// -/// [broadcast]: trait.Write.html#method.broadcast -pub struct Broadcast { - first: T, - second: U, -} - -#[allow(deprecated)] -impl Write for Broadcast { - fn write(&mut self, data: &[u8]) -> Result { - let n = try!(self.first.write(data)); - // FIXME: what if the write fails? (we wrote something) - try!(self.second.write_all(&data[..n])); - Ok(n) - } - - fn flush(&mut self) -> Result<()> { - self.first.flush().and(self.second.flush()) - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match try!(self.first.read(buf)) { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -/// 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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 = try!(self.inner.read(&mut buf[..max])); - self.limit -= n as u64; - Ok(n) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - fn fill_buf(&mut self) -> Result<&[u8]> { - let buf = try!(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); - } -} - -/// An adaptor which will emit all read data to a specified writer as well. -/// -/// This struct is generally created by calling [`tee()`][tee] on a reader. -/// Please see the documentation of `tee()` for more details. -/// -/// [tee]: trait.Read.html#method.tee -pub struct Tee { - reader: R, - writer: W, -} - -#[allow(deprecated)] -impl Read for Tee { - fn read(&mut self, buf: &mut [u8]) -> Result { - let n = try!(self.reader.read(buf)); - // FIXME: what if the write fails? (we read something) - try!(self.writer.write_all(&buf[..n])); - Ok(n) - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(e) => return Some(Err(CharsError::Other(e))), - } - } - } - Some(match str::from_utf8(&buf[..width]).ok() { - Some(s) => Ok(s.char_at(0)), - None => Err(CharsError::NotUtf8), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - Err(io::Error::new(io::ErrorKind::Other, "")) - } - } - - let mut buf = [0; 1]; - assert_eq!(0, R.take(0).read(&mut buf).unwrap()); - } - - #[bench] - 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/src/c1fb50f5d377a41dd5833e4621e9a14879647503/prelude.rs b/src/c1fb50f5d377a41dd5833e4621e9a14879647503/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/util.rs b/src/c1fb50f5d377a41dd5833e4621e9a14879647503/util.rs deleted file mode 100644 index 2f03121..0000000 --- a/src/c1fb50f5d377a41dd5833e4621e9a14879647503/util.rs +++ /dev/null @@ -1,218 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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), - }; - try!(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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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); - } - - #[test] - #[allow(deprecated)] - fn tee() { - let mut buf = [0; 10]; - { - let mut ptr: &mut [u8] = &mut buf; - assert_eq!(repeat(4).tee(&mut ptr).take(5).read(&mut [0; 10]).unwrap(), 5); - } - assert_eq!(buf, [4, 4, 4, 4, 4, 0, 0, 0, 0, 0]); - } - - #[test] - #[allow(deprecated)] - fn broadcast() { - let mut buf1 = [0; 10]; - let mut buf2 = [0; 10]; - { - let mut ptr1: &mut [u8] = &mut buf1; - let mut ptr2: &mut [u8] = &mut buf2; - - assert_eq!((&mut ptr1).broadcast(&mut ptr2) - .write(&[1, 2, 3]).unwrap(), 3); - } - assert_eq!(buf1, buf2); - assert_eq!(buf1, [1, 2, 3, 0, 0, 0, 0, 0, 0, 0]); - } -} diff --git a/src/cae91d7c8c21aa860bda29c62207a6726837952b/buffered.rs b/src/cae91d7c8c21aa860bda29c62207a6726837952b/buffered.rs deleted file mode 100644 index e23b74f..0000000 --- a/src/cae91d7c8c21aa860bda29c62207a6726837952b/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/cae91d7c8c21aa860bda29c62207a6726837952b/cursor.rs b/src/cae91d7c8c21aa860bda29c62207a6726837952b/cursor.rs deleted file mode 100644 index 3a00ffe..0000000 --- a/src/cae91d7c8c21aa860bda29c62207a6726837952b/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/cae91d7c8c21aa860bda29c62207a6726837952b/error.rs b/src/cae91d7c8c21aa860bda29c62207a6726837952b/error.rs deleted file mode 100644 index eeb11d5..0000000 --- a/src/cae91d7c8c21aa860bda29c62207a6726837952b/error.rs +++ /dev/null @@ -1,313 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - 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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/cae91d7c8c21aa860bda29c62207a6726837952b/impls.rs b/src/cae91d7c8c21aa860bda29c62207a6726837952b/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/cae91d7c8c21aa860bda29c62207a6726837952b/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/cae91d7c8c21aa860bda29c62207a6726837952b/memchr.rs b/src/cae91d7c8c21aa860bda29c62207a6726837952b/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/cae91d7c8c21aa860bda29c62207a6726837952b/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/cae91d7c8c21aa860bda29c62207a6726837952b/mod.rs b/src/cae91d7c8c21aa860bda29c62207a6726837952b/mod.rs deleted file mode 100644 index d190cba..0000000 --- a/src/cae91d7c8c21aa860bda29c62207a6726837952b/mod.rs +++ /dev/null @@ -1,1845 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/cae91d7c8c21aa860bda29c62207a6726837952b/prelude.rs b/src/cae91d7c8c21aa860bda29c62207a6726837952b/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/cae91d7c8c21aa860bda29c62207a6726837952b/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/cae91d7c8c21aa860bda29c62207a6726837952b/util.rs b/src/cae91d7c8c21aa860bda29c62207a6726837952b/util.rs deleted file mode 100644 index 1edcae5..0000000 --- a/src/cae91d7c8c21aa860bda29c62207a6726837952b/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/buffered.rs b/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/buffered.rs deleted file mode 100644 index a1af9d9..0000000 --- a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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 = try!(self.fill_buf()); - try!(rem.read(buf)) - }; - self.consume(nread); - Ok(nread) - } -} - -impl BufRead for BufReader { - 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 = try!(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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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 = try!(self.inner.seek(SeekFrom::Current(offset))); - } else { - // seek backwards by our remainder, and then by the offset - try!(self.inner.seek(SeekFrom::Current(-remainder))); - self.pos = self.cap; // empty the buffer - result = try!(self.inner.seek(SeekFrom::Current(n))); - } - } else { - // Seeking with Start/End doesn't care about our buffer length. - result = try!(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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - if self.buf.len() + buf.len() > self.buf.capacity() { - try!(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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - match memchr::memrchr(b'\n', buf) { - Some(i) => { - let n = try!(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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/cursor.rs b/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/cursor.rs deleted file mode 100644 index 670e362..0000000 --- a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = try!(Read::read(&mut try!(self.fill_buf()), buf)); - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - let pos = cmp::min(self.pos, self.inner.len() as u64); - let amt = try!((&mut self.inner[(pos as usize)..]).write(data)); - self.pos += amt as u64; - Ok(amt) - } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - let pos = cmp::min(self.pos, self.inner.len() as u64); - let amt = try!((&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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/error.rs b/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/error.rs deleted file mode 100644 index d3d0814..0000000 --- a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/error.rs +++ /dev/null @@ -1,314 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/impls.rs b/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/impls.rs deleted file mode 100644 index ac172bf..0000000 --- a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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 try!(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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/memchr.rs b/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/mod.rs b/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/mod.rs deleted file mode 100644 index 85ce207..0000000 --- a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/mod.rs +++ /dev/null @@ -1,1780 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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 mut reader = BufReader::new(f); -//! -//! for line in reader.lines() { -//! let line = try!(line); -//! println!("{}", line); -//! } -//! -//! # Ok(()) -//! # } -//! ``` -//! -//! ## Functions -//! -//! There are a number of [functions][functions] 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]: #functions -//! -//! ## io::Result -//! -//! Last, but certainly not least, is [`io::Result`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 64 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buff - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match try!(self.first.read(buf)) { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -/// 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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 = try!(self.inner.read(&mut buf[..max])); - self.limit -= n as u64; - Ok(n) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - fn fill_buf(&mut self) -> Result<&[u8]> { - let buf = try!(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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(e) => return Some(Err(CharsError::Other(e))), - } - } - } - Some(match str::from_utf8(&buf[..width]).ok() { - Some(s) => Ok(s.char_at(0)), - None => Err(CharsError::NotUtf8), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - Err(io::Error::new(io::ErrorKind::Other, "")) - } - } - - let mut buf = [0; 1]; - assert_eq!(0, R.take(0).read(&mut buf).unwrap()); - } - - #[bench] - 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/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/prelude.rs b/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/util.rs b/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/util.rs deleted file mode 100644 index 5e80819..0000000 --- a/src/ce943eb369c9bdd0aef4917675e515f39f3b4a1e/util.rs +++ /dev/null @@ -1,191 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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), - }; - try!(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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/buffered.rs b/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/buffered.rs deleted file mode 100644 index 27c5ac1..0000000 --- a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/buffered.rs +++ /dev/null @@ -1,1105 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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 { - 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/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/cursor.rs b/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/cursor.rs deleted file mode 100644 index c77f287..0000000 --- a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/cursor.rs +++ /dev/null @@ -1,571 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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::::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()); - } -} diff --git a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/error.rs b/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/error.rs deleted file mode 100644 index 748b830..0000000 --- a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/error.rs +++ /dev/null @@ -1,480 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// A marker variant that tells the compiler that users of this enum cannot - /// match it exhaustively. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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!")); - /// } - /// ``` - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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!")); - /// } - /// ``` - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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::().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()))); - /// } - /// ``` - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/impls.rs b/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/impls.rs deleted file mode 100644 index d048379..0000000 --- a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/impls.rs +++ /dev/null @@ -1,288 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/memchr.rs b/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/memchr.rs deleted file mode 100644 index 312cf47..0000000 --- a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/memchr.rs +++ /dev/null @@ -1,232 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -pub mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = if end_align >= len { 0 } else { len - end_align }; - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all platforms - #[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_reversed() { - let mut data = [1u8; 64]; - let needle = 2; - let pos = 40; - data[pos] = needle; - for start in 0..16 { - assert_eq!(Some(pos - start), memrchr(needle, &data[start..])); - } - } -} diff --git a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/mod.rs b/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/mod.rs deleted file mode 100644 index 05d91fc..0000000 --- a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/mod.rs +++ /dev/null @@ -1,1910 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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 - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// 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)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> T { - self.inner - } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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> { - 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()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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::>(); - 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 { - 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(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/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/prelude.rs b/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/util.rs b/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/util.rs deleted file mode 100644 index 93bcfc3..0000000 --- a/src/d311079a6f70577d02f35bb80d27eef7e2b18a4a/util.rs +++ /dev/null @@ -1,206 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { 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/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/buffered.rs b/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/buffered.rs deleted file mode 100644 index e23b74f..0000000 --- a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/cursor.rs b/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/cursor.rs deleted file mode 100644 index 1db50ee..0000000 --- a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/cursor.rs +++ /dev/null @@ -1,572 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/error.rs b/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/error.rs deleted file mode 100644 index 5da1224..0000000 --- a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/error.rs +++ /dev/null @@ -1,337 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - 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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/impls.rs b/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/memchr.rs b/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/memchr.rs deleted file mode 100644 index 823a3ad..0000000 --- a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all platforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/mod.rs b/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/mod.rs deleted file mode 100644 index d190cba..0000000 --- a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/mod.rs +++ /dev/null @@ -1,1845 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/prelude.rs b/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/util.rs b/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/util.rs deleted file mode 100644 index aa2e47e..0000000 --- a/src/d40c593f42fafbac1ff3d827f6df96338b5b7d8b/util.rs +++ /dev/null @@ -1,208 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/buffered.rs b/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/buffered.rs deleted file mode 100644 index a138b3e..0000000 --- a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/buffered.rs +++ /dev/null @@ -1,1104 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/cursor.rs b/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/cursor.rs deleted file mode 100644 index c77f287..0000000 --- a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/cursor.rs +++ /dev/null @@ -1,571 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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::::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()); - } -} diff --git a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/error.rs b/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/error.rs deleted file mode 100644 index 748b830..0000000 --- a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/error.rs +++ /dev/null @@ -1,480 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// A marker variant that tells the compiler that users of this enum cannot - /// match it exhaustively. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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!")); - /// } - /// ``` - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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!")); - /// } - /// ``` - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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::().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()))); - /// } - /// ``` - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/impls.rs b/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/impls.rs deleted file mode 100644 index d048379..0000000 --- a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/impls.rs +++ /dev/null @@ -1,288 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/memchr.rs b/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/memchr.rs deleted file mode 100644 index f767e0c..0000000 --- a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/memchr.rs +++ /dev/null @@ -1,319 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = if end_align >= len { 0 } else { len - end_align }; - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all platforms - #[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_reversed() { - let mut data = [1u8; 64]; - let needle = 2; - let pos = 40; - data[pos] = needle; - for start in 0..16 { - assert_eq!(Some(pos - start), memrchr(needle, &data[start..])); - } - } -} - -#[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/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/mod.rs b/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/mod.rs deleted file mode 100644 index a1bbe50..0000000 --- a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/mod.rs +++ /dev/null @@ -1,1901 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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 - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// 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)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> T { - self.inner - } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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> { - 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()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/prelude.rs b/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/util.rs b/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/util.rs deleted file mode 100644 index 93bcfc3..0000000 --- a/src/d6aa4e828c3dc3b7c417197990741b024f8c4ca3/util.rs +++ /dev/null @@ -1,206 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { 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/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/buffered.rs b/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/buffered.rs deleted file mode 100644 index e23b74f..0000000 --- a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/cursor.rs b/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/cursor.rs deleted file mode 100644 index 1db50ee..0000000 --- a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/cursor.rs +++ /dev/null @@ -1,572 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/error.rs b/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/error.rs deleted file mode 100644 index eeb11d5..0000000 --- a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/error.rs +++ /dev/null @@ -1,313 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - 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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/impls.rs b/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/memchr.rs b/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/mod.rs b/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/mod.rs deleted file mode 100644 index d190cba..0000000 --- a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/mod.rs +++ /dev/null @@ -1,1845 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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); - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/prelude.rs b/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/util.rs b/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/util.rs deleted file mode 100644 index f7aa6f7..0000000 --- a/src/dd56a6ad0845b76509c4f8967e8ca476471ab7e0/util.rs +++ /dev/null @@ -1,208 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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 mut buffer = vec![1, 2, 3, 5, 8]; -/// let num_bytes = io::sink().write(&mut buffer).unwrap(); -/// assert_eq!(num_bytes, 5); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/buffered.rs b/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/buffered.rs deleted file mode 100644 index fd4eb13..0000000 --- a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/buffered.rs +++ /dev/null @@ -1,1104 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/cursor.rs b/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/cursor.rs deleted file mode 100644 index c77f287..0000000 --- a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/cursor.rs +++ /dev/null @@ -1,571 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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::::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()); - } -} diff --git a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/error.rs b/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/error.rs deleted file mode 100644 index 748b830..0000000 --- a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/error.rs +++ /dev/null @@ -1,480 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// A marker variant that tells the compiler that users of this enum cannot - /// match it exhaustively. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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!")); - /// } - /// ``` - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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!")); - /// } - /// ``` - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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::().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()))); - /// } - /// ``` - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/impls.rs b/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/impls.rs deleted file mode 100644 index d048379..0000000 --- a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/impls.rs +++ /dev/null @@ -1,288 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/memchr.rs b/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/memchr.rs deleted file mode 100644 index f767e0c..0000000 --- a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/memchr.rs +++ /dev/null @@ -1,319 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = if end_align >= len { 0 } else { len - end_align }; - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all platforms - #[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_reversed() { - let mut data = [1u8; 64]; - let needle = 2; - let pos = 40; - data[pos] = needle; - for start in 0..16 { - assert_eq!(Some(pos - start), memrchr(needle, &data[start..])); - } - } -} - -#[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/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/mod.rs b/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/mod.rs deleted file mode 100644 index a1bbe50..0000000 --- a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/mod.rs +++ /dev/null @@ -1,1901 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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 - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// 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)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> T { - self.inner - } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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> { - 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()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/prelude.rs b/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/util.rs b/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/util.rs deleted file mode 100644 index 93bcfc3..0000000 --- a/src/e107c8b84969fbe52cae7c9fd61858fddc6e016b/util.rs +++ /dev/null @@ -1,206 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { 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/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/buffered.rs b/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/buffered.rs deleted file mode 100644 index e23b74f..0000000 --- a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/cursor.rs b/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/cursor.rs deleted file mode 100644 index 1db50ee..0000000 --- a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/cursor.rs +++ /dev/null @@ -1,572 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/error.rs b/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/error.rs deleted file mode 100644 index 25bbd8b..0000000 --- a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/error.rs +++ /dev/null @@ -1,480 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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!")); - /// } - /// ``` - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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!")); - /// } - /// ``` - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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::().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()))); - /// } - /// ``` - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - 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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/impls.rs b/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/memchr.rs b/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/memchr.rs deleted file mode 100644 index 823a3ad..0000000 --- a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all platforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/mod.rs b/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/mod.rs deleted file mode 100644 index 3f9472c..0000000 --- a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/mod.rs +++ /dev/null @@ -1,1876 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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 - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// 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)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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(()) - /// # } - /// ``` - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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> { - 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()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/prelude.rs b/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/util.rs b/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/util.rs deleted file mode 100644 index aa2e47e..0000000 --- a/src/e6cc4c5d13f8819c72568f9675e84c1d17368c67/util.rs +++ /dev/null @@ -1,208 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/src/f1e191c0b959111aef19f3aa06b7f1743419470c/buffered.rs b/src/f1e191c0b959111aef19f3aa06b7f1743419470c/buffered.rs deleted file mode 100644 index 17e6955..0000000 --- a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/buffered.rs +++ /dev/null @@ -1,1076 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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 = try!(self.fill_buf()); - try!(rem.read(buf)) - }; - self.consume(nread); - Ok(nread) - } -} - -impl BufRead for BufReader { - 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 = try!(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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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 = try!(self.inner.seek(SeekFrom::Current(offset))); - } else { - // seek backwards by our remainder, and then by the offset - try!(self.inner.seek(SeekFrom::Current(-remainder))); - self.pos = self.cap; // empty the buffer - result = try!(self.inner.seek(SeekFrom::Current(n))); - } - } else { - // Seeking with Start/End doesn't care about our buffer length. - result = try!(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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - if self.buf.len() + buf.len() > self.buf.capacity() { - try!(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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - match memchr::memrchr(b'\n', buf) { - Some(i) => { - let n = try!(self.inner.write(&buf[..i + 1])); - if n != i + 1 { return Ok(n) } - try!(self.inner.flush()); - self.inner.write(&buf[i + 1..]).map(|i| n + i) - } - None => self.inner.write(buf), - } - } - - fn flush(&mut self) -> io::Result<()> { self.inner.flush() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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() { - 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 { 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] - 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 { - 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().err().unwrap(); - - 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/src/f1e191c0b959111aef19f3aa06b7f1743419470c/cursor.rs b/src/f1e191c0b959111aef19f3aa06b7f1743419470c/cursor.rs deleted file mode 100644 index 670e362..0000000 --- a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/cursor.rs +++ /dev/null @@ -1,570 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = try!(Read::read(&mut try!(self.fill_buf()), buf)); - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - fn write(&mut self, data: &[u8]) -> io::Result { - let pos = cmp::min(self.pos, self.inner.len() as u64); - let amt = try!((&mut self.inner[(pos as usize)..]).write(data)); - self.pos += amt as u64; - Ok(amt) - } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - let pos = cmp::min(self.pos, self.inner.len() as u64); - let amt = try!((&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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/error.rs b/src/f1e191c0b959111aef19f3aa06b7f1743419470c/error.rs deleted file mode 100644 index 50e3658..0000000 --- a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/error.rs +++ /dev/null @@ -1,317 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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`. -#[derive(Debug)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - Other, - - #[allow(missing_docs)] - UnexpectedEOF, - - /// 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// Creates a new instance of an `Error` from a particular OS error code. - 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`. - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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`. - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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`. - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => None, - Repr::Custom(c) => Some(c.error) - } - } - - /// Returns the corresponding `ErrorKind` for this error. - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - use super::{Error, ErrorKind}; - use error; - use error::Error as error_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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/impls.rs b/src/f1e191c0b959111aef19f3aa06b7f1743419470c/impls.rs deleted file mode 100644 index ac172bf..0000000 --- a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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 try!(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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/f1e191c0b959111aef19f3aa06b7f1743419470c/memchr.rs b/src/f1e191c0b959111aef19f3aa06b7f1743419470c/memchr.rs deleted file mode 100644 index 110cfac..0000000 --- a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all plattforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/mod.rs b/src/f1e191c0b959111aef19f3aa06b7f1743419470c/mod.rs deleted file mode 100644 index 9d9263f..0000000 --- a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/mod.rs +++ /dev/null @@ -1,1896 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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 mut reader = BufReader::new(f); -//! -//! for line in reader.lines() { -//! let line = try!(line); -//! println!("{}", line); -//! } -//! -//! # Ok(()) -//! # } -//! ``` -//! -//! ## Functions -//! -//! There are a number of [functions][functions] 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]: #functions -//! -//! ## io::Result -//! -//! Last, but certainly not least, is [`io::Result`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 64 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take where Self: Sized { - Take { inner: self, limit: limit } - } - - /// Creates a reader adaptor which will write all read data into the given - /// output stream. - /// - /// Whenever the returned `Read` instance is read it will write the read - /// data to `out`. The current semantics of this implementation imply that - /// a `write` error will not report how much data was initially read. - /// - /// # 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")); - /// let mut buffer1 = Vec::with_capacity(10); - /// let mut buffer2 = Vec::with_capacity(10); - /// - /// // write the output to buffer1 as we read - /// let mut handle = f.tee(&mut buffer1); - /// - /// try!(handle.read(&mut buffer2)); - /// # Ok(()) - /// # } - /// ``` - #[allow(deprecated)] - fn tee(self, out: W) -> Tee where Self: Sized { - Tee { reader: self, writer: out } - } -} - -/// 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - fn by_ref(&mut self) -> &mut Self where Self: Sized { self } - - /// Creates a new writer which will write all data to both this writer and - /// another writer. - /// - /// All data written to the returned writer will both be written to `self` - /// as well as `other`. Note that the error semantics of the current - /// implementation do not precisely track where errors happen. For example - /// an error on the second call to `write` will not report that the first - /// call to `write` succeeded. - /// - /// # Examples - /// - /// ``` - /// #![feature(io)] - /// use std::io::prelude::*; - /// use std::fs::File; - /// - /// # fn foo() -> std::io::Result<()> { - /// let mut buffer1 = try!(File::create("foo.txt")); - /// let mut buffer2 = Vec::new(); - /// - /// // write the output to buffer1 as we read - /// let mut handle = buffer1.broadcast(&mut buffer2); - /// - /// try!(handle.write(b"some bytes")); - /// # Ok(()) - /// # } - /// ``` - #[allow(deprecated)] - fn broadcast(self, other: W) -> Broadcast - where Self: Sized - { - Broadcast { first: self, second: other } - } -} - -/// 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(()) -/// # } -/// ``` -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. - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// Enumeration of possible methods to seek within an I/O object. -#[derive(Copy, PartialEq, Eq, Clone, Debug)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buff - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// A `Write` adaptor which will write data to multiple locations. -/// -/// This struct is generally created by calling [`broadcast()`][broadcast] on a -/// writer. Please see the documentation of `broadcast()` for more details. -/// -/// [broadcast]: trait.Write.html#method.broadcast -pub struct Broadcast { - first: T, - second: U, -} - -#[allow(deprecated)] -impl Write for Broadcast { - fn write(&mut self, data: &[u8]) -> Result { - let n = try!(self.first.write(data)); - // FIXME: what if the write fails? (we wrote something) - try!(self.second.write_all(&data[..n])); - Ok(n) - } - - fn flush(&mut self) -> Result<()> { - self.first.flush().and(self.second.flush()) - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match try!(self.first.read(buf)) { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -/// 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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. - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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 = try!(self.inner.read(&mut buf[..max])); - self.limit -= n as u64; - Ok(n) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - fn fill_buf(&mut self) -> Result<&[u8]> { - let buf = try!(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); - } -} - -/// An adaptor which will emit all read data to a specified writer as well. -/// -/// This struct is generally created by calling [`tee()`][tee] on a reader. -/// Please see the documentation of `tee()` for more details. -/// -/// [tee]: trait.Read.html#method.tee -pub struct Tee { - reader: R, - writer: W, -} - -#[allow(deprecated)] -impl Read for Tee { - fn read(&mut self, buf: &mut [u8]) -> Result { - let n = try!(self.reader.read(buf)); - // FIXME: what if the write fails? (we read something) - try!(self.writer.write_all(&buf[..n])); - Ok(n) - } -} - -/// An iterator over `u8` values of a reader. -/// -/// This struct is generally created by calling [`bytes()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - match self.inner.read(&mut buf) { - Ok(0) => None, - Ok(..) => Some(Ok(buf[0])), - Err(e) => Some(Err(e)), - } - } -} - -/// 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - let mut buf = [0]; - let first_byte = match self.inner.read(&mut buf) { - Ok(0) => return None, - Ok(..) => buf[0], - 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(e) => return Some(Err(CharsError::Other(e))), - } - } - } - Some(match str::from_utf8(&buf[..width]).ok() { - Some(s) => Ok(s.char_at(0)), - None => Err(CharsError::NotUtf8), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - Err(io::Error::new(io::ErrorKind::Other, "")) - } - } - - let mut buf = [0; 1]; - assert_eq!(0, R.take(0).read(&mut buf).unwrap()); - } - - #[bench] - 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/src/f1e191c0b959111aef19f3aa06b7f1743419470c/prelude.rs b/src/f1e191c0b959111aef19f3aa06b7f1743419470c/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/util.rs b/src/f1e191c0b959111aef19f3aa06b7f1743419470c/util.rs deleted file mode 100644 index 2f03121..0000000 --- a/src/f1e191c0b959111aef19f3aa06b7f1743419470c/util.rs +++ /dev/null @@ -1,218 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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), - }; - try!(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 -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; -/// use std::io::Read; -/// -/// # fn foo() -> io::Result { -/// let mut buffer = String::new(); -/// try!(io::empty().read_to_string(&mut buffer)); -/// # Ok(buffer) -/// # } -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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. -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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. -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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); - } - - #[test] - #[allow(deprecated)] - fn tee() { - let mut buf = [0; 10]; - { - let mut ptr: &mut [u8] = &mut buf; - assert_eq!(repeat(4).tee(&mut ptr).take(5).read(&mut [0; 10]).unwrap(), 5); - } - assert_eq!(buf, [4, 4, 4, 4, 4, 0, 0, 0, 0, 0]); - } - - #[test] - #[allow(deprecated)] - fn broadcast() { - let mut buf1 = [0; 10]; - let mut buf2 = [0; 10]; - { - let mut ptr1: &mut [u8] = &mut buf1; - let mut ptr2: &mut [u8] = &mut buf2; - - assert_eq!((&mut ptr1).broadcast(&mut ptr2) - .write(&[1, 2, 3]).unwrap(), 3); - } - assert_eq!(buf1, buf2); - assert_eq!(buf1, [1, 2, 3, 0, 0, 0, 0, 0, 0, 0]); - } -} diff --git a/src/fda473f00fa07b9a8246b104396f9922e54bff16/buffered.rs b/src/fda473f00fa07b9a8246b104396f9922e54bff16/buffered.rs deleted file mode 100644 index e23b74f..0000000 --- a/src/fda473f00fa07b9a8246b104396f9922e54bff16/buffered.rs +++ /dev/null @@ -1,1106 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Buffering wrappers for I/O traits - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use core::fmt; -use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom}; -use io::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. -/// -/// # 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(()) -/// # } -/// ``` -pub struct BufReader { - inner: R, - buf: Box<[u8]>, - pos: usize, - cap: usize, -} - -impl BufReader { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: R) -> BufReader { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - pub fn into_inner(self) -> R { self.inner } -} - -impl Read for BufReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - // 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) - } -} - -impl BufRead for BufReader { - 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); - } -} - -impl fmt::Debug for BufReader 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() - } -} - -impl Seek for BufReader { - /// 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 - /// `.unwrap()` 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 { - 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 ebibytes 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. -pub struct BufWriter { - inner: Option, - buf: Vec, - // #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)] -pub struct IntoInnerError(W, Error); - -impl BufWriter { - /// 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()); - /// ``` - pub fn new(inner: W) -> BufWriter { - 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); - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> BufWriter { - 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(); - /// ``` - 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(); - /// ``` - 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(); - /// ``` - pub fn into_inner(mut self) -> Result>> { - match self.flush_buf() { - Err(e) => Err(IntoInnerError(self, e)), - Ok(()) => Ok(self.inner.take().unwrap()) - } - } -} - -impl Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { - let amt = cmp::min(buf.len(), self.buf.capacity()); - Write::write(&mut self.buf, &buf[..amt]) - } - } - fn flush(&mut self) -> io::Result<()> { - self.flush_buf().and_then(|()| self.get_mut().flush()) - } -} - -impl fmt::Debug for BufWriter 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() - } -} - -impl Seek for BufWriter { - /// 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 { - self.flush_buf().and_then(|_| self.get_mut().seek(pos)) - } -} - -impl Drop for BufWriter { - 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 IntoInnerError { - /// 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."); - /// } - /// }; - /// ``` - 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() - /// } - /// }; - /// ``` - pub fn into_inner(self) -> W { self.0 } -} - -impl From> for Error { - fn from(iie: IntoInnerError) -> Error { iie.1 } -} - -impl fmt::Display for IntoInnerError { - 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(()) -/// # } -/// ``` -pub struct LineWriter { - inner: BufWriter, -} - -impl LineWriter { - /// 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(()) - /// # } - /// ``` - pub fn new(inner: W) -> LineWriter { - // 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(()) - /// # } - /// ``` - pub fn with_capacity(cap: usize, inner: W) -> LineWriter { - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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 = LineWriter::new(file); - /// - /// let file: File = try!(writer.into_inner()); - /// # Ok(()) - /// # } - /// ``` - pub fn into_inner(self) -> Result>> { - self.inner.into_inner().map_err(|IntoInnerError(buf, e)| { - IntoInnerError(LineWriter { inner: buf }, e) - }) - } -} - -impl Write for LineWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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() } -} - -impl fmt::Debug for LineWriter 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 prelude::v1::*; - 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, - } - - impl Read for ShortReader { - fn read(&mut self, _: &mut [u8]) -> io::Result { - 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 { - 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 { - 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); - - impl<'a> Write for FailFlushWriter<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 { 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] - 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 { - 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/src/fda473f00fa07b9a8246b104396f9922e54bff16/cursor.rs b/src/fda473f00fa07b9a8246b104396f9922e54bff16/cursor.rs deleted file mode 100644 index 1db50ee..0000000 --- a/src/fda473f00fa07b9a8246b104396f9922e54bff16/cursor.rs +++ /dev/null @@ -1,572 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use core::prelude::v1::*; -use io::prelude::*; - -use core::cmp; -use io::{self, SeekFrom, Error, ErrorKind}; - -/// A `Cursor` wraps another type and provides it with a -/// [`Seek`](trait.Seek.html) implementation. -/// -/// Cursors 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>` and `Cursor<&[u8]>`. -/// -/// # Examples -/// -/// We may want to write bytes to a [`File`][file] in our production -/// code, but use an in-memory buffer in our tests. We can do this with -/// `Cursor`: -/// -/// [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(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]); -/// } -/// ``` -#[derive(Clone, Debug)] -pub struct Cursor { - inner: T, - pos: u64, -} - -impl Cursor { - /// 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>) {} - /// # force_inference(&buff); - /// ``` - pub fn new(inner: T) -> Cursor { - 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>) {} - /// # force_inference(&buff); - /// - /// let vec = buff.into_inner(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_ref(); - /// ``` - 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>) {} - /// # force_inference(&buff); - /// - /// let reference = buff.get_mut(); - /// ``` - 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); - /// ``` - 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); - /// ``` - pub fn set_position(&mut self, pos: u64) { self.pos = pos; } -} - -impl io::Seek for Cursor where T: AsRef<[u8]> { - fn seek(&mut self, style: SeekFrom) -> io::Result { - 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) - } - } -} - -impl Read for Cursor where T: AsRef<[u8]> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let n = Read::read(&mut self.fill_buf()?, buf)?; - self.pos += n as u64; - Ok(n) - } -} - -impl BufRead for Cursor 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; } -} - -impl<'a> Write for Cursor<&'a mut [u8]> { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -impl Write for Cursor> { - fn write(&mut self, buf: &[u8]) -> io::Result { - // Make sure the internal buffer is as least as big as where we - // currently are - let pos = self.position(); - let amt = pos.saturating_sub(self.inner.len() as u64); - // use `resize` so that the zero filling is as efficient as possible - let len = self.inner.len(); - self.inner.resize(len + amt as usize, 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 pos = pos as usize; - 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(()) } -} - -impl Write for Cursor> { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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}; - use vec::Vec; - - #[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::::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()); - } -} diff --git a/src/fda473f00fa07b9a8246b104396f9922e54bff16/error.rs b/src/fda473f00fa07b9a8246b104396f9922e54bff16/error.rs deleted file mode 100644 index 25bbd8b..0000000 --- a/src/fda473f00fa07b9a8246b104396f9922e54bff16/error.rs +++ /dev/null @@ -1,480 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -#[cfg(not(feature="alloc"))] use ::FakeBox as Box; -use core::convert::Into; -use core::fmt; -use core::marker::{Send, Sync}; -use core::option::Option::{self, Some, None}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(not(feature="collections"))] use ::ErrorString as String; - -/// 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 { -/// let mut buffer = String::new(); -/// -/// try!(io::stdin().read_line(&mut buffer)); -/// -/// Ok(buffer) -/// } -/// ``` -pub type Result = result::Result; - -/// 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)] -pub struct Error { - repr: Repr, -} - -enum Repr { - Os(i32), - #[cfg(feature="alloc")] - Custom(Box), - #[cfg(not(feature="alloc"))] - Custom(Custom), -} - -#[derive(Debug)] -struct Custom { - kind: ErrorKind, - error: String, -} - -/// 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(Copy, PartialEq, Eq, Clone, Debug)] -#[allow(deprecated)] -pub enum ErrorKind { - /// An entity was not found, often a file. - NotFound, - /// The operation lacked the necessary privileges to complete. - PermissionDenied, - /// The connection was refused by the remote server. - ConnectionRefused, - /// The connection was reset by the remote server. - ConnectionReset, - /// The connection was aborted (terminated) by the remote server. - ConnectionAborted, - /// The network operation failed because it was not connected yet. - NotConnected, - /// A socket address could not be bound because the address is already in - /// use elsewhere. - AddrInUse, - /// A nonexistent interface was requested or the requested address was not - /// local. - AddrNotAvailable, - /// The operation failed because a pipe was closed. - BrokenPipe, - /// An entity already exists, often a file. - AlreadyExists, - /// The operation needs to block to complete, but the blocking operation was - /// requested to not occur. - WouldBlock, - /// A parameter was incorrect. - 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. - InvalidData, - /// The I/O operation's timeout expired, causing it to be canceled. - 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. - WriteZero, - /// This operation was interrupted. - /// - /// Interrupted operations can typically be retried. - Interrupted, - /// Any I/O error not part of this list. - 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. - UnexpectedEof, - - /// Any I/O error not part of this list. - #[doc(hidden)] - __Nonexhaustive, -} - -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); - /// ``` - pub fn new(kind: ErrorKind, error: E) -> Error - where E: Into - { - Self::_new(kind, error.into()) - } - - fn _new(kind: ErrorKind, error: String) -> Error { - Error { - repr: Repr::Custom(Box::new(Custom { - kind: kind, - error: error, - })) - } - } - - /// 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); - /// # } - /// ``` - 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!")); - /// } - /// ``` - pub fn raw_os_error(&self) -> Option { - match self.repr { - Repr::Os(i) => Some(i), - Repr::Custom(..) => 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!")); - /// } - /// ``` - pub fn get_ref(&self) -> Option<&String> { - match self.repr { - Repr::Os(..) => 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::().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()))); - /// } - /// ``` - pub fn get_mut(&mut self) -> Option<&mut String> { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn into_inner(self) -> Option { - match self.repr { - Repr::Os(..) => 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!")); - /// } - /// ``` - pub fn kind(&self) -> ErrorKind { - match self.repr { - Repr::Os(_code) => ErrorKind::Other, - Repr::Custom(ref c) => c.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).finish(), - Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(), - } - } -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self.repr { - Repr::Os(code) => { - write!(fmt, "os error {}", code) - } - Repr::Custom(ref c) => c.error.fmt(fmt), - } - } -} - -fn _assert_error_is_sync_send() { - fn _is_sync_send() {} - _is_sync_send::(); -} - -#[cfg(test)] -mod test { - use prelude::v1::*; - 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::()); - assert_eq!("asdf", err.get_ref().unwrap().description()); - assert!(err.get_mut().unwrap().is::()); - let extracted = err.into_inner().unwrap(); - extracted.downcast::().unwrap(); - } -} diff --git a/src/fda473f00fa07b9a8246b104396f9922e54bff16/impls.rs b/src/fda473f00fa07b9a8246b104396f9922e54bff16/impls.rs deleted file mode 100644 index 291c69c..0000000 --- a/src/fda473f00fa07b9a8246b104396f9922e54bff16/impls.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[cfg(feature="alloc")] use alloc::boxed::Box; -use core::cmp; -use io::{self, SeekFrom, Read, Write, Seek, Error, ErrorKind}; -#[cfg(feature="collections")] use io::BufRead; -use core::fmt; -use core::mem; -#[cfg(feature="collections")] use collections::string::String; -#[cfg(feature="collections")] use collections::vec::Vec; - -// ============================================================================= -// Forwarding implementations - -impl<'a, R: Read + ?Sized> Read for &'a mut R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -impl<'a, W: Write + ?Sized> Write for &'a mut W { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -impl<'a, S: Seek + ?Sized> Seek for &'a mut S { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -#[cfg(feature="alloc")] -impl Read for Box { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - (**self).read(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { - (**self).read_to_end(buf) - } - - #[cfg(feature="collections")] - #[inline] - fn read_to_string(&mut self, buf: &mut String) -> io::Result { - (**self).read_to_string(buf) - } - - #[inline] - fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { - (**self).read_exact(buf) - } -} -#[cfg(feature="alloc")] -impl Write for Box { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { (**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) - } -} -#[cfg(feature="alloc")] -impl Seek for Box { - #[inline] - fn seek(&mut self, pos: SeekFrom) -> io::Result { (**self).seek(pos) } -} -#[cfg(feature="collections")] -impl BufRead for Box { - #[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) -> io::Result { - (**self).read_until(byte, buf) - } - - #[inline] - fn read_line(&mut self, buf: &mut String) -> io::Result { - (**self).read_line(buf) - } -} - -// ============================================================================= -// In-memory buffer implementations - -impl<'a> Read for &'a [u8] { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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(()) - } -} - -#[cfg(feature="collections")] -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..]; } -} - -impl<'a> Write for &'a mut [u8] { - #[inline] - fn write(&mut self, data: &[u8]) -> io::Result { - 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(()) } -} - -#[cfg(feature="collections")] -impl Write for Vec { - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { - 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 vec::Vec; - 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/src/fda473f00fa07b9a8246b104396f9922e54bff16/memchr.rs b/src/fda473f00fa07b9a8246b104396f9922e54bff16/memchr.rs deleted file mode 100644 index 823a3ad..0000000 --- a/src/fda473f00fa07b9a8246b104396f9922e54bff16/memchr.rs +++ /dev/null @@ -1,297 +0,0 @@ -// 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 or the MIT license -// , 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 use self::fallback::{memchr,memrchr}; - -#[allow(dead_code)] -mod fallback { - use core::cmp; - use core::mem; - - const LO_U64: u64 = 0x0101010101010101; - const HI_U64: u64 = 0x8080808080808080; - - // use truncation - const LO_USIZE: usize = LO_U64 as usize; - const HI_USIZE: usize = HI_U64 as usize; - - /// Return `true` if `x` contains any zero byte. - /// - /// From *Matters Computational*, J. Arndt - /// - /// "The idea is to subtract one from each of the bytes and then look for - /// bytes where the borrow propagated all the way to the most significant - /// bit." - #[inline] - fn contains_zero_byte(x: usize) -> bool { - x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0 - } - - #[cfg(target_pointer_width = "32")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep - } - - #[cfg(target_pointer_width = "64")] - #[inline] - fn repeat_byte(b: u8) -> usize { - let mut rep = (b as usize) << 8 | b as usize; - rep = rep << 16 | rep; - rep = rep << 32 | rep; - rep - } - - /// Return the first index matching the byte `a` in `text`. - pub fn memchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned initial part, before the first word aligned address in text - // - body, scan by 2 words at a time - // - the last remaining part, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search up to an aligned boundary - let align = (ptr as usize) & (usize_bytes- 1); - let mut offset; - if align > 0 { - offset = cmp::min(usize_bytes - align, len); - if let Some(index) = text[..offset].iter().position(|elt| *elt == x) { - return Some(index); - } - } else { - offset = 0; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - if len >= 2 * usize_bytes { - while offset <= len - 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize) as *const usize); - let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset += usize_bytes * 2; - } - } - - // find the byte after the point the body loop stopped - text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i) - } - - /// Return the last index matching the byte `a` in `text`. - pub fn memrchr(x: u8, text: &[u8]) -> Option { - // Scan for a single byte value by reading two `usize` words at a time. - // - // Split `text` in three parts - // - unaligned tail, after the last word aligned address in text - // - body, scan by 2 words at a time - // - the first remaining bytes, < 2 word size - let len = text.len(); - let ptr = text.as_ptr(); - let usize_bytes = mem::size_of::(); - - // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); - let mut offset; - if end_align > 0 { - offset = len - cmp::min(usize_bytes - end_align, len); - if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { - return Some(offset + index); - } - } else { - offset = len; - } - - // search the body of the text - let repeated_x = repeat_byte(x); - - while offset >= 2 * usize_bytes { - unsafe { - let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize); - let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize); - - // break if there is a matching byte - let zu = contains_zero_byte(u ^ repeated_x); - let zv = contains_zero_byte(v ^ repeated_x); - if zu || zv { - break; - } - } - offset -= 2 * usize_bytes; - } - - // find the byte before the point the body loop stopped - text[..offset].iter().rposition(|elt| *elt == x) - } - - // test fallback implementations on all platforms - #[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")); - } -} - -#[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")); - } -} diff --git a/src/fda473f00fa07b9a8246b104396f9922e54bff16/mod.rs b/src/fda473f00fa07b9a8246b104396f9922e54bff16/mod.rs deleted file mode 100644 index 94508d2..0000000 --- a/src/fda473f00fa07b9a8246b104396f9922e54bff16/mod.rs +++ /dev/null @@ -1,1876 +0,0 @@ -// 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 or the MIT license -// , 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`][read] and [`Write`][write] traits, which provide the -//! most general interface for reading and writing input and output. -//! -//! [read]: trait.Read.html -//! [write]: trait.Write.html -//! -//! # 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`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`][seek] -//! and [`BufRead`][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(()) -//! # } -//! ``` -//! -//! [seek]: trait.Seek.html -//! [bufread]: trait.BufRead.html -//! -//! `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()`][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(()) -//! # } -//! ``` -//! -//! [write()]: trait.Write.html#tymethod.write -//! -//! ## 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`][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!`][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<()>`, 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 `()`. -//! -//! [result]: type.Result.html -//! [try]: ../macro.try!.html -//! -//! ## 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. - -use core::cmp; -use rustc_unicode::str as core_str; -use core::fmt; -use core::iter::{Iterator}; -use core::marker::Sized; -#[cfg(feature="collections")] use core::ops::{Drop, FnOnce}; -use core::option::Option::{self, Some, None}; -use core::result::Result::{Ok, Err}; -use core::result; -#[cfg(feature="collections")] use collections::string::String; -use core::str; -#[cfg(feature="collections")] use collections::vec::Vec; -mod memchr; - -#[cfg(feature="collections")] pub use self::buffered::{BufReader, BufWriter, LineWriter}; -#[cfg(feature="collections")] pub use self::buffered::IntoInnerError; -#[cfg(feature="collections")] pub use self::cursor::Cursor; -pub use self::error::{Result, Error, ErrorKind}; -pub use self::util::{copy, sink, Sink, empty, Empty, repeat, Repeat}; - -pub mod prelude; -#[cfg(feature="collections")] mod buffered; -#[cfg(feature="collections")] mod cursor; -mod error; -mod impls; -mod util; - -const DEFAULT_BUF_SIZE: usize = 8 * 1024; - -// 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. -#[cfg(feature="collections")] -fn append_to_string(buf: &mut String, f: F) -> Result - where F: FnOnce(&mut Vec) -> Result -{ - struct Guard<'a> { s: &'a mut Vec, 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. -#[cfg(feature="collections")] -fn read_to_end(r: &mut R, buf: &mut Vec) -> Result { - 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn read(&mut self, buf: &mut [u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_end(&mut self, buf: &mut Vec) -> Result { - 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(()) - /// # } - /// ``` - #[cfg(feature="collections")] - fn read_to_string(&mut self, buf: &mut String) -> Result { - // Note that we do *not* call `.read_to_end()` here. We are passing - // `&mut Vec` (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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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`. 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(()) - /// # } - /// ``` - fn bytes(self) -> Bytes 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` 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(()) - /// # } - /// ``` - fn chars(self) -> Chars 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(()) - /// # } - /// ``` - fn chain(self, next: R) -> Chain 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(()) - /// # } - /// ``` - fn take(self, limit: u64) -> Take 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(()) -/// # } -/// ``` -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(()) - /// # } - /// ``` - fn write(&mut self, buf: &[u8]) -> Result; - - /// 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) - /// # } - /// ``` - 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(()) -/// # } -/// ``` -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 - fn seek(&mut self, pos: SeekFrom) -> Result; -} - -/// 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)] -pub enum SeekFrom { - /// Set the offset to the provided number of bytes. - Start(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. - End(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. - Current(i64), -} - -#[cfg(feature="collections")] -fn read_until(r: &mut R, delim: u8, buf: &mut Vec) - -> Result { - 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()`][readline] method as well as a [`lines()`][lines] iterator. -/// -/// [readline]: #method.read_line -/// [lines]: #method.lines -/// -/// # 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`][file] implements `Read`, but not `BufRead`. -/// `BufReader` to the rescue! -/// -/// [bufreader]: struct.BufReader.html -/// [file]: ../fs/struct.File.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(()) -/// # } -/// ``` -/// -#[cfg(feature="collections")] -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`][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); - /// ``` - 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`][fillbuf] 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. - /// - /// [fillbuf]: #tymethod.fill_buf - /// - /// 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()`][fillbuf], - /// that method's example includes an example of `consume()`. - fn consume(&mut self, amt: usize); - - /// Read all bytes into `buf` until the delimiter `byte` 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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. - /// - /// ``` - /// 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(()) - /// # } - /// ``` - fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { - 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 this reader is currently at EOF then this function will not modify - /// `buf` and will return `Ok(n)` where `n` is the number of bytes which - /// were 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()`][lines] method would be easier, of - /// course. - /// - /// [lines]: #method.lines - /// - /// ``` - /// 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(); - /// } - /// ``` - fn read_line(&mut self, buf: &mut String) -> Result { - // 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>`. 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. - /// - /// ``` - /// use std::io; - /// use std::io::prelude::*; - /// - /// let stdin = io::stdin(); - /// - /// for content in stdin.lock().split(b',') { - /// println!("{:?}", content.unwrap()); - /// } - /// ``` - fn split(self, byte: u8) -> Split 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`. Each string returned will *not* have a newline - /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end. - /// - /// # 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()); - /// } - /// ``` - fn lines(self) -> Lines where Self: Sized { - Lines { buf: self } - } -} - -/// Adaptor to chain together two readers. -/// -/// This struct is generally created by calling [`chain()`][chain] on a reader. -/// Please see the documentation of `chain()` for more details. -/// -/// [chain]: trait.Read.html#method.chain -pub struct Chain { - first: T, - second: U, - done_first: bool, -} - -impl Read for Chain { - fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.done_first { - match self.first.read(buf)? { - 0 => { self.done_first = true; } - n => return Ok(n), - } - } - self.second.read(buf) - } -} - -#[cfg(feature="collections")] -impl BufRead for Chain { - 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 -pub struct Take { - inner: T, - limit: u64, -} - -impl Take { - /// 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(()) - /// # } - /// ``` - pub fn limit(&self) -> u64 { self.limit } -} - -impl Read for Take { - fn read(&mut self, buf: &mut [u8]) -> Result { - // 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) - } -} - -#[cfg(feature="collections")] -impl BufRead for Take { - 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> { - 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()`][bytes] on a reader. -/// Please see the documentation of `bytes()` for more details. -/// -/// [bytes]: trait.Read.html#method.bytes -pub struct Bytes { - inner: R, -} - -impl Iterator for Bytes { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 -pub struct Chars { - inner: R, -} - -/// An enumeration of possible errors that can be generated from the `Chars` -/// adapter. -#[derive(Debug)] -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), -} - -impl Iterator for Chars { - type Item = result::Result; - - fn next(&mut self) -> Option> { - 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), - }) - } -} - -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 -#[cfg(feature="collections")] -pub struct Split { - buf: B, - delim: u8, -} - -#[cfg(feature="collections")] -impl Iterator for Split { - type Item = Result>; - - fn next(&mut self) -> Option>> { - 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 -#[cfg(feature="collections")] -pub struct Lines { - buf: B, -} - -#[cfg(feature="collections")] -impl Iterator for Lines { - type Item = Result; - - fn next(&mut self) -> Option> { - 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 prelude::v1::*; - use io::prelude::*; - use io; - use super::Cursor; - use test; - use super::repeat; - - #[test] - 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::>(); - 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 { - 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(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[..]); - } - - #[bench] - 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/src/fda473f00fa07b9a8246b104396f9922e54bff16/prelude.rs b/src/fda473f00fa07b9a8246b104396f9922e54bff16/prelude.rs deleted file mode 100644 index 49d66c9..0000000 --- a/src/fda473f00fa07b9a8246b104396f9922e54bff16/prelude.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 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 or the MIT license -// , 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::*; -//! ``` - -pub use super::{Read, Write, Seek}; -#[cfg(feature="collections")] pub use super::BufRead; - -#[cfg(feature="collections")] pub use alloc::boxed::Box; -#[cfg(feature="collections")] pub use collections::vec::Vec; diff --git a/src/fda473f00fa07b9a8246b104396f9922e54bff16/util.rs b/src/fda473f00fa07b9a8246b104396f9922e54bff16/util.rs deleted file mode 100644 index aa2e47e..0000000 --- a/src/fda473f00fa07b9a8246b104396f9922e54bff16/util.rs +++ /dev/null @@ -1,208 +0,0 @@ -// 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 or the MIT license -// , 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}; -#[cfg(feature="collections")] use io::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 = vec![]; -/// -/// try!(io::copy(&mut reader, &mut writer)); -/// -/// assert_eq!(reader, &writer[..]); -/// # Ok(()) -/// # } -/// ``` -pub fn copy(reader: &mut R, writer: &mut W) -> io::Result - 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 -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()); -/// ``` -pub fn empty() -> Empty { Empty { _priv: () } } - -impl Read for Empty { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { Ok(0) } -} -#[cfg(feature="collections")] -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 -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]); -/// ``` -pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } } - -impl Read for Repeat { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - 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 -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); -/// ``` -pub fn sink() -> Sink { Sink { _priv: () } } - -impl Write for Sink { - fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { Ok(()) } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - 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/sync.sh b/sync.sh deleted file mode 100755 index 8235b5c..0000000 --- a/sync.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh -# Recommended command-line: -# -# commit-db.rb list-valid nightly|GIT_DIR=/your/rust/dir/.git sync.sh - -cd "$(dirname "$0")" -for COMPILER_COMMIT in $(sort -u); do - IO_COMMIT=$(git log -n1 --pretty=format:%H $COMPILER_COMMIT -- src/libstd/io) - if ! [ -d src/$IO_COMMIT ]; then - mkdir src/$IO_COMMIT - git archive $IO_COMMIT src/libstd/io|tar xf - -C src/$IO_COMMIT --strip-components=3 - git archive $IO_COMMIT src/libstd/sys/common/memchr.rs|tar xf - -C src/$IO_COMMIT --strip-components=4 - rm -f src/$IO_COMMIT/stdio.rs src/$IO_COMMIT/lazy.rs - fi - if ! grep -q $COMPILER_COMMIT mapping.rs; then - echo "-Mapping(\"$COMPILER_COMMIT\",\"$IO_COMMIT\")" >> mapping.rs - fi -done