@@ -532,11 +532,9 @@ pub trait Read {
532532
533533/// A trait for objects which are byte-oriented sinks.
534534///
535- /// The `write` method will attempt to write some data into the object,
536- /// returning how many bytes were successfully written.
535+ /// Implementors of the `Write` trait are sometimes called 'writers'.
537536///
538- /// The `flush` method is useful for adaptors and explicit buffers themselves
539- /// for ensuring that all buffered data has been pushed out to the "true sink".
537+ /// Writers are defined by two required methods, `write()` and `flush()`:
540538///
541539/// * The `write()` method will attempt to write some data into the object,
542540/// returning how many bytes were successfully written.
@@ -588,6 +586,20 @@ pub trait Write {
588586 ///
589587 /// It is **not** considered an error if the entire buffer could not be
590588 /// written to this writer.
589+ ///
590+ /// # Examples
591+ ///
592+ /// ```
593+ /// use std::io::prelude::*;
594+ /// use std::fs::File;
595+ ///
596+ /// # fn foo() -> std::io::Result<()> {
597+ /// let mut buffer = try!(File::create("foo.txt"));
598+ ///
599+ /// try!(buffer.write(b"some bytes"));
600+ /// # Ok(())
601+ /// # }
602+ /// ```
591603 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
592604 fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize > ;
593605
@@ -598,6 +610,22 @@ pub trait Write {
598610 ///
599611 /// It is considered an error if not all bytes could be written due to
600612 /// I/O errors or EOF being reached.
613+ ///
614+ /// # Examples
615+ ///
616+ /// ```
617+ /// use std::io::prelude::*;
618+ /// use std::io::BufWriter;
619+ /// use std::fs::File;
620+ ///
621+ /// # fn foo() -> std::io::Result<()> {
622+ /// let mut buffer = BufWriter::new(try!(File::create("foo.txt")));
623+ ///
624+ /// try!(buffer.write(b"some bytes"));
625+ /// try!(buffer.flush());
626+ /// # Ok(())
627+ /// # }
628+ /// ```
601629 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
602630 fn flush ( & mut self ) -> Result < ( ) > ;
603631
@@ -611,6 +639,20 @@ pub trait Write {
611639 /// # Errors
612640 ///
613641 /// This function will return the first error that `write` returns.
642+ ///
643+ /// # Examples
644+ ///
645+ /// ```
646+ /// use std::io::prelude::*;
647+ /// use std::fs::File;
648+ ///
649+ /// # fn foo() -> std::io::Result<()> {
650+ /// let mut buffer = try!(File::create("foo.txt"));
651+ ///
652+ /// try!(buffer.write_all(b"some bytes"));
653+ /// # Ok(())
654+ /// # }
655+ /// ```
614656 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
615657 fn write_all ( & mut self , mut buf : & [ u8 ] ) -> Result < ( ) > {
616658 while !buf. is_empty ( ) {
@@ -628,17 +670,41 @@ pub trait Write {
628670 /// Writes a formatted string into this writer, returning any error
629671 /// encountered.
630672 ///
631- /// This method is primarily used to interface with the `format_args!`
632- /// macro, but it is rare that this should explicitly be called. The
633- /// `write!` macro should be favored to invoke this method instead.
673+ /// This method is primarily used to interface with the
674+ /// [`format_args!`][formatargs] macro, but it is rare that this should
675+ /// explicitly be called. The [`write!`][write] macro should be favored to
676+ /// invoke this method instead.
677+ ///
678+ /// [formatargs]: ../std/macro.format_args!.html
679+ /// [write]: ../std/macro.write!.html
634680 ///
635- /// This function internally uses the `write_all` method on this trait and
636- /// hence will continuously write data so long as no errors are received.
637- /// This also means that partial writes are not indicated in this signature.
681+ /// This function internally uses the [`write_all`][writeall] method on
682+ /// this trait and hence will continuously write data so long as no errors
683+ /// are received. This also means that partial writes are not indicated in
684+ /// this signature.
685+ ///
686+ /// [writeall]: #method.write_all
638687 ///
639688 /// # Errors
640689 ///
641690 /// This function will return any I/O error reported while formatting.
691+ ///
692+ /// # Examples
693+ ///
694+ /// ```
695+ /// use std::io::prelude::*;
696+ /// use std::fs::File;
697+ ///
698+ /// # fn foo() -> std::io::Result<()> {
699+ /// let mut buffer = try!(File::create("foo.txt"));
700+ ///
701+ /// // this call
702+ /// try!(write!(buffer, "{:.*}", 2, 1.234567));
703+ /// // turns into this:
704+ /// try!(buffer.write_fmt(format_args!("{:.*}", 2, 1.234567)));
705+ /// # Ok(())
706+ /// # }
707+ /// ```
642708 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
643709 fn write_fmt ( & mut self , fmt : fmt:: Arguments ) -> Result < ( ) > {
644710 // Create a shim which translates a Write to a fmt::Write and saves
@@ -671,6 +737,23 @@ pub trait Write {
671737 ///
672738 /// The returned adaptor also implements `Write` and will simply borrow this
673739 /// current writer.
740+ ///
741+ /// # Examples
742+ ///
743+ /// ```
744+ /// use std::io::Write;
745+ /// use std::fs::File;
746+ ///
747+ /// # fn foo() -> std::io::Result<()> {
748+ /// let mut buffer = try!(File::create("foo.txt"));
749+ ///
750+ /// let reference = buffer.by_ref();
751+ ///
752+ /// // we can use reference just like our original buffer
753+ /// try!(reference.write_all(b"some bytes"));
754+ /// # Ok(())
755+ /// # }
756+ /// ```
674757 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
675758 fn by_ref ( & mut self ) -> & mut Self where Self : Sized { self }
676759
@@ -682,6 +765,25 @@ pub trait Write {
682765 /// implementation do not precisely track where errors happen. For example
683766 /// an error on the second call to `write` will not report that the first
684767 /// call to `write` succeeded.
768+ ///
769+ /// # Examples
770+ ///
771+ /// ```
772+ /// #![feature(io)]
773+ /// use std::io::prelude::*;
774+ /// use std::fs::File;
775+ ///
776+ /// # fn foo() -> std::io::Result<()> {
777+ /// let mut buffer1 = try!(File::create("foo.txt"));
778+ /// let mut buffer2 = Vec::new();
779+ ///
780+ /// // write the output to buffer1 as we read
781+ /// let mut handle = buffer1.broadcast(&mut buffer2);
782+ ///
783+ /// try!(handle.write(b"some bytes"));
784+ /// # Ok(())
785+ /// # }
786+ /// ```
685787 #[ unstable( feature = "io" , reason = "the semantics of a partial read/write \
686788 of where errors happen is currently \
687789 unclear and may change") ]
@@ -706,15 +808,15 @@ pub trait Write {
706808///
707809/// ```
708810/// use std::io;
811+ /// use std::io::prelude::*;
709812/// use std::fs::File;
710- /// use std::io::Seek;
711813/// use std::io::SeekFrom;
712814///
713815/// # fn foo() -> io::Result<()> {
714816/// let mut f = try!(File::open("foo.txt"));
715817///
716818/// // move the cursor 42 bytes from the start of the file
717- /// f.seek(SeekFrom::Start(42)).unwrap( );
819+ /// try!( f.seek(SeekFrom::Start(42)));
718820/// # Ok(())
719821/// # }
720822/// ```
0 commit comments