Skip to content

Commit

Permalink
core::io::writer : add get_type() method
Browse files Browse the repository at this point in the history
   The get_type() method can hint to users what kind of item might be
   under the hood.
  • Loading branch information
dgryski authored and catamorphism committed Jul 27, 2012
1 parent 872ef0f commit cdd052f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/libcore/io.rs
Expand Up @@ -330,20 +330,25 @@ fn with_str_reader<T>(s: ~str, f: fn(reader) -> T) -> T {
// Writing
enum fileflag { append, create, truncate, no_flag, }

// What type of writer are we?
enum writer_type { screen, file }

// FIXME (#2004): Seekable really should be orthogonal.
// FIXME (#2004): eventually u64
iface writer {
fn write(v: &[const u8]);
fn seek(int, seek_style);
fn tell() -> uint;
fn flush() -> int;
fn get_type() -> writer_type;
}

impl <T: writer, C> of writer for {base: T, cleanup: C} {
fn write(bs: &[const u8]) { self.base.write(bs); }
fn seek(off: int, style: seek_style) { self.base.seek(off, style); }
fn tell() -> uint { self.base.tell() }
fn flush() -> int { self.base.flush() }
fn get_type() -> writer_type { file }
}

impl of writer for *libc::FILE {
Expand All @@ -364,6 +369,13 @@ impl of writer for *libc::FILE {
}
fn tell() -> uint { libc::ftell(self) as uint }
fn flush() -> int { libc::fflush(self) as int }
fn get_type() -> writer_type {
let fd = libc::fileno(self);
if libc::isatty(fd) == 0 {
ret file;
}
ret screen;
}
}

fn FILE_writer(f: *libc::FILE, cleanup: bool) -> writer {
Expand Down Expand Up @@ -399,6 +411,9 @@ impl of writer for fd_t {
fail;
}
fn flush() -> int { 0 }
fn get_type() -> writer_type {
if libc::isatty(self) == 0 { file } else { screen }
}
}

class fd_res {
Expand Down Expand Up @@ -646,6 +661,7 @@ impl of writer for mem_buffer {
}
fn tell() -> uint { self.pos }
fn flush() -> int { 0 }
fn get_type() -> writer_type { ret file }
}
fn mem_buffer() -> mem_buffer {
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/net_tcp.rs
Expand Up @@ -840,6 +840,9 @@ impl tcp_socket_buf of io::writer for @tcp_socket_buf {
fn flush() -> int {
0
}
fn get_type() -> io::writer_type {
io::file
}
}

// INTERNAL API
Expand Down

0 comments on commit cdd052f

Please sign in to comment.