diff --git a/src/librustdoc/html/static/playpen.js b/src/librustdoc/html/static/playpen.js index ff947d93fca16..b7a5f202629fe 100644 --- a/src/librustdoc/html/static/playpen.js +++ b/src/librustdoc/html/static/playpen.js @@ -16,6 +16,7 @@ document.addEventListener('DOMContentLoaded', function() { return; } + var featureRegexp = new RegExp('^\s*#!\\[feature\\(\.*?\\)\\]'); var elements = document.querySelectorAll('pre.rust'); Array.prototype.forEach.call(elements, function(el) { @@ -29,8 +30,14 @@ document.addEventListener('DOMContentLoaded', function() { a.setAttribute('class', 'test-arrow'); var code = el.previousElementSibling.textContent; + + var channel = ''; + if (featureRegexp.test(code)) { + channel = '&version=nightly'; + } + a.setAttribute('href', window.playgroundUrl + '?code=' + - encodeURIComponent(code)); + encodeURIComponent(code) + channel); a.setAttribute('target', '_blank'); el.appendChild(a); diff --git a/src/libstd/array.rs b/src/libstd/array.rs index e79e5d5a680f6..0dfcc72e37910 100644 --- a/src/libstd/array.rs +++ b/src/libstd/array.rs @@ -9,7 +9,7 @@ // except according to those terms. //! A fixed-size array is denoted `[T; N]` for the element type `T` and -//! the compile time constant size `N`. The size should be zero or positive. +//! the compile time constant size `N`. The size must be zero or positive. //! //! Arrays values are created either with an explicit expression that lists //! each element: `[x, y, z]` or a repeat expression: `[x; N]`. The repeat @@ -32,7 +32,9 @@ //! //! [slice]: primitive.slice.html //! -//! ## Examples +//! Rust does not currently support generics over the size of an array type. +//! +//! # Examples //! //! ``` //! let mut array: [i32; 3] = [0; 3]; @@ -49,7 +51,5 @@ //! //! ``` //! -//! Rust does not currently support generics over the size of an array type. -//! #![doc(primitive = "array")] diff --git a/src/libstd/io/error.rs b/src/libstd/io/error.rs index a66789bf2873d..3b48ff3096043 100644 --- a/src/libstd/io/error.rs +++ b/src/libstd/io/error.rs @@ -17,11 +17,37 @@ use option::Option::{self, Some, None}; use result; use sys; -/// A type for results generated by I/O related functions where the `Err` type -/// is hard-wired to `io::Error`. +/// A specialized [`Result`][result] type for I/O operations. +/// +/// [result]: ../result/enum.Result.html +/// +/// 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 `std::result::Result`. +/// 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; diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 2a64d0f6e8c19..62bbb939a71f5 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -154,15 +154,42 @@ pub struct StdinLock<'a> { inner: MutexGuard<'a, BufReader>>, } -/// Creates a new handle to the global standard input stream of this process. +/// Constructs a new handle to the standard input of the current process. /// -/// The handle returned refers to a globally shared buffer between all threads. -/// Access is synchronized and can be explicitly controlled with the `lock()` -/// method. +/// Each handle returned is a reference to a shared global buffer whose access +/// is synchronized via a mutex. If you need more explicit control over +/// locking, see the [lock() method][lock]. +/// +/// [lock]: struct.Stdin.html#method.lock +/// +/// # Examples +/// +/// Using implicit synchronization: +/// +/// ``` +/// use std::io::{self, Read}; +/// +/// # fn foo() -> io::Result { +/// let mut buffer = String::new(); +/// try!(io::stdin().read_to_string(&mut buffer)); +/// # Ok(buffer) +/// # } +/// ``` +/// +/// Using explicit synchronization: /// -/// The `Read` trait is implemented for the returned value but the `BufRead` -/// trait is not due to the global nature of the standard input stream. The -/// locked version, `StdinLock`, implements both `Read` and `BufRead`, however. +/// ``` +/// use std::io::{self, Read}; +/// +/// # fn foo() -> io::Result { +/// let mut buffer = String::new(); +/// let stdin = io::stdin(); +/// let mut handle = stdin.lock(); +/// +/// try!(handle.read_to_string(&mut buffer)); +/// # Ok(buffer) +/// # } +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn stdin() -> Stdin { static INSTANCE: Lazy>>> = Lazy::new(stdin_init); @@ -298,13 +325,42 @@ pub struct StdoutLock<'a> { inner: ReentrantMutexGuard<'a, RefCell>>>, } -/// Constructs a new reference to the standard output of the current process. +/// Constructs a new handle to the standard output of the current process. /// /// Each handle returned is a reference to a shared global buffer whose access -/// is synchronized via a mutex. Explicit control over synchronization is -/// provided via the `lock` method. +/// is synchronized via a mutex. If you need more explicit control over +/// locking, see the [lock() method][lock]. +/// +/// [lock]: struct.Stdout.html#method.lock +/// +/// # Examples +/// +/// Using implicit synchronization: +/// +/// ``` +/// use std::io::{self, Write}; +/// +/// # fn foo() -> io::Result<()> { +/// try!(io::stdout().write(b"hello world")); /// -/// The returned handle implements the `Write` trait. +/// # Ok(()) +/// # } +/// ``` +/// +/// Using explicit synchronization: +/// +/// ``` +/// use std::io::{self, Write}; +/// +/// # fn foo() -> io::Result<()> { +/// let stdout = io::stdout(); +/// let mut handle = stdout.lock(); +/// +/// try!(handle.write(b"hello world")); +/// +/// # Ok(()) +/// # } +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn stdout() -> Stdout { static INSTANCE: Lazy>>>> @@ -376,12 +432,38 @@ pub struct StderrLock<'a> { inner: ReentrantMutexGuard<'a, RefCell>>, } -/// Constructs a new reference to the standard error stream of a process. +/// Constructs a new handle to the standard error of the current process. +/// +/// This handle is not buffered. +/// +/// # Examples +/// +/// Using implicit synchronization: +/// +/// ``` +/// use std::io::{self, Write}; +/// +/// # fn foo() -> io::Result<()> { +/// try!(io::stderr().write(b"hello world")); +/// +/// # Ok(()) +/// # } +/// ``` +/// +/// Using explicit synchronization: +/// +/// ``` +/// use std::io::{self, Write}; +/// +/// # fn foo() -> io::Result<()> { +/// let stderr = io::stderr(); +/// let mut handle = stderr.lock(); /// -/// Each returned handle is synchronized amongst all other handles created from -/// this function. No handles are buffered, however. +/// try!(handle.write(b"hello world")); /// -/// The returned handle implements the `Write` trait. +/// # Ok(()) +/// # } +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn stderr() -> Stderr { static INSTANCE: Lazy>>> = Lazy::new(stderr_init); diff --git a/src/libstd/io/util.rs b/src/libstd/io/util.rs index d8c999f894837..c0bced26beffa 100644 --- a/src/libstd/io/util.rs +++ b/src/libstd/io/util.rs @@ -28,6 +28,22 @@ use io::{self, Read, Write, ErrorKind, BufRead}; /// 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 { let mut buf = [0; super::DEFAULT_BUF_SIZE]; @@ -48,9 +64,24 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result io::Result { +/// let mut buffer = String::new(); +/// try!(io::empty().read_to_string(&mut buffer)); +/// # Ok(buffer) +/// # } +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn empty() -> Empty { Empty { _priv: () } } diff --git a/src/rustbook/build.rs b/src/rustbook/build.rs index a7a6ed3bfe749..8152ea530d561 100644 --- a/src/rustbook/build.rs +++ b/src/rustbook/build.rs @@ -22,7 +22,7 @@ use term::Term; use error::{err, CliResult, CommandResult}; use book; use book::{Book, BookItem}; -use css; + use javascript; use rustdoc; @@ -195,9 +195,16 @@ impl Subcommand for Build { } try!(fs::create_dir(&tgt)); - try!(File::create(&tgt.join("rust-book.css")).and_then(|mut f| { - f.write_all(css::STYLE.as_bytes()) - })); + // Copy static files + try!(fs::copy( + &cwd.join("src/rustbook/static/rustbook.css"), + &tgt.join("rust-book.css") + )); + + try!(fs::copy( + &cwd.join("src/rustbook/static/rustbook.js"), + &tgt.join("rust-book.js") + )); let mut summary = try!(File::open(&src.join("SUMMARY.md"))); match book::parse_summary(&mut summary, &src) { diff --git a/src/rustbook/javascript.rs b/src/rustbook/javascript.rs index f33b79cc1888f..beddc23fe2bdd 100644 --- a/src/rustbook/javascript.rs +++ b/src/rustbook/javascript.rs @@ -11,65 +11,6 @@ // The rust-book JavaScript in string form. pub static JAVASCRIPT: &'static str = r#" - + "#; diff --git a/src/rustbook/main.rs b/src/rustbook/main.rs index acb1c5cbd90ba..81f8c8c40fdff 100644 --- a/src/rustbook/main.rs +++ b/src/rustbook/main.rs @@ -35,7 +35,6 @@ mod build; mod serve; mod test; -mod css; mod javascript; static EXIT_STATUS: AtomicIsize = ATOMIC_ISIZE_INIT; diff --git a/src/rustbook/css.rs b/src/rustbook/static/rustbook.css similarity index 62% rename from src/rustbook/css.rs rename to src/rustbook/static/rustbook.css index aae5f21a73dc6..3e0537c5551f6 100644 --- a/src/rustbook/css.rs +++ b/src/rustbook/static/rustbook.css @@ -1,20 +1,28 @@ -// 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. - -// The rust-book CSS in string form. - -pub static STYLE: &'static str = r#" +/** + * 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. + */ + @import url("../rust.css"); body { max-width:none; + font: 16px/1.4 'Source Serif Pro', Georgia, Times, 'Times New Roman', serif; + line-height: 1.6; + color: #333; +} + +h1, h2, h3, h4, h5, h6 { + font-family: 'Open Sans', 'Fira Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; + font-weight: bold; + color: #333; } @media only screen { @@ -23,20 +31,21 @@ body { left: 0px; top: 0px; bottom: 0px; - width: 250px; + width: 300px; overflow-y: auto; border-right: 1px solid rgba(0, 0, 0, 0.07); padding: 10px 10px; - font-size: 16px; - background: none repeat scroll 0% 0% #FFF; + font-size: 14px; box-sizing: border-box; -webkit-overflow-scrolling: touch; + background-color: #fafafa; + color: #364149; } #page-wrapper { position: absolute; overflow-y: auto; - left: 260px; + left: 310px; right: 0px; top: 0px; bottom: 0px; @@ -47,7 +56,7 @@ body { } @media only print { - #toc, #nav { + #toc, #nav, #menu-bar { display: none; } } @@ -84,7 +93,7 @@ body { .section { list-style: none outside none; padding-left: 20px; - line-height: 30px; + line-height: 40px; } .section li { @@ -94,12 +103,17 @@ body { } .chapter li a { - color: #000000; + color: #333; + padding: 5px 0; } .chapter li a.active { - text-decoration: underline; - font-weight: bold; + color: #008cff; +} + +.chapter li a:hover { + color: #008cff; + text-decoration: none; } #toggle-nav { @@ -138,6 +152,20 @@ body { padding: 0; } +pre { + padding: 16px; + overflow: auto; + font-size: 85%; + line-height: 1.45; + background-color: #f7f7f7; + border: 0; + border-radius: 3px; +} + +.nav-previous-next { + margin-top: 60px; +} + .left { float: left; } @@ -145,4 +173,3 @@ body { .right { float: right; } -"#; diff --git a/src/rustbook/static/rustbook.js b/src/rustbook/static/rustbook.js new file mode 100644 index 0000000000000..ef96448a5d3a1 --- /dev/null +++ b/src/rustbook/static/rustbook.js @@ -0,0 +1,70 @@ +// 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. + +document.addEventListener("DOMContentLoaded", function(event) { + + document.getElementById("toggle-nav").onclick = toggleNav; + function toggleNav() { + var toc = document.getElementById("toc"); + var pagewrapper = document.getElementById("page-wrapper"); + toggleClass(toc, "mobile-hidden"); + toggleClass(pagewrapper, "mobile-hidden"); + } + + function toggleClass(el, className) { + // from http://youmightnotneedjquery.com/ + if (el.classList) { + el.classList.toggle(className); + } else { + var classes = el.className.split(' '); + var existingIndex = classes.indexOf(className); + + if (existingIndex >= 0) { + classes.splice(existingIndex, 1); + } else { + classes.push(className); + } + + el.className = classes.join(' '); + } + } + + // The below code is used to add prev and next navigation links to the bottom + // of each of the sections. + // It works by extracting the current page based on the url and iterates over + // the menu links until it finds the menu item for the current page. We then + // create a copy of the preceding and following menu links and add the + // correct css class and insert them into the bottom of the page. + var toc = document.getElementById('toc').getElementsByTagName('a'); + var href = document.location.pathname.split('/').pop(); + if (href === 'index.html' || href === '') { + href = 'README.html'; + } + + for (var i = 0; i < toc.length; i++) { + if (toc[i].attributes['href'].value.split('/').pop() === href) { + var nav = document.createElement('p'); + nav.className = 'nav-previous-next'; + if (i > 0) { + var prevNode = toc[i-1].cloneNode(true); + prevNode.className = 'left'; + nav.appendChild(prevNode); + } + if (i < toc.length - 1) { + var nextNode = toc[i+1].cloneNode(true); + nextNode.className = 'right'; + nav.appendChild(nextNode); + } + document.getElementById('page').appendChild(nav); + break; + } + } + +});