Skip to content

Commit

Permalink
std::io: Modernize some constructors
Browse files Browse the repository at this point in the history
Part of #3853
  • Loading branch information
Blei committed Jun 4, 2013
1 parent 34ee63e commit e1c1c05
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/libextra/flatpipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ mod test {
#[test]
#[ignore(reason = "ebml failure")]
fn test_serializing_memory_stream() {
let writer = BytesWriter();
let writer = BytesWriter::new();
let chan = serial::writer_chan(writer);

chan.send(10);
Expand Down Expand Up @@ -708,7 +708,7 @@ mod test {

#[test]
fn test_pod_memory_stream() {
let writer = BytesWriter();
let writer = BytesWriter::new();
let chan = pod::writer_chan(writer);

chan.send(10);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ pub static metadata_encoding_version : &'static [u8] =
0, 0, 0, 1 ];

pub fn encode_metadata(parms: EncodeParams, crate: &crate) -> ~[u8] {
let wr = @io::BytesWriter();
let wr = @io::BytesWriter::new();
let stats = Stats {
inline_bytes: 0,
attr_bytes: 0,
Expand Down
68 changes: 35 additions & 33 deletions src/libstd/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,12 @@ pub struct FILERes {
f: *libc::FILE,
}

impl FILERes {
pub fn new(f: *libc::FILE) -> FILERes {
FILERes { f: f }
}
}

impl Drop for FILERes {
fn finalize(&self) {
unsafe {
Expand All @@ -990,15 +996,9 @@ impl Drop for FILERes {
}
}

pub fn FILERes(f: *libc::FILE) -> FILERes {
FILERes {
f: f
}
}

pub fn FILE_reader(f: *libc::FILE, cleanup: bool) -> @Reader {
if cleanup {
@Wrapper { base: f, cleanup: FILERes(f) } as @Reader
@Wrapper { base: f, cleanup: FILERes::new(f) } as @Reader
} else {
@f as @Reader
}
Expand Down Expand Up @@ -1183,7 +1183,7 @@ impl Writer for *libc::FILE {

pub fn FILE_writer(f: *libc::FILE, cleanup: bool) -> @Writer {
if cleanup {
@Wrapper { base: f, cleanup: FILERes(f) } as @Writer
@Wrapper { base: f, cleanup: FILERes::new(f) } as @Writer
} else {
@f as @Writer
}
Expand Down Expand Up @@ -1227,6 +1227,12 @@ pub struct FdRes {
fd: fd_t,
}

impl FdRes {
pub fn new(fd: fd_t) -> FdRes {
FdRes { fd: fd }
}
}

impl Drop for FdRes {
fn finalize(&self) {
unsafe {
Expand All @@ -1235,15 +1241,9 @@ impl Drop for FdRes {
}
}

pub fn FdRes(fd: fd_t) -> FdRes {
FdRes {
fd: fd
}
}

pub fn fd_writer(fd: fd_t, cleanup: bool) -> @Writer {
if cleanup {
@Wrapper { base: fd, cleanup: FdRes(fd) } as @Writer
@Wrapper { base: fd, cleanup: FdRes::new(fd) } as @Writer
} else {
@fd as @Writer
}
Expand Down Expand Up @@ -1634,6 +1634,15 @@ pub struct BytesWriter {
pos: @mut uint,
}

impl BytesWriter {
pub fn new() -> BytesWriter {
BytesWriter {
bytes: @mut ~[],
pos: @mut 0
}
}
}

impl Writer for BytesWriter {
fn write(&self, v: &[u8]) {
let v_len = v.len();
Expand Down Expand Up @@ -1673,15 +1682,8 @@ impl Writer for BytesWriter {
}
}

pub fn BytesWriter() -> BytesWriter {
BytesWriter {
bytes: @mut ~[],
pos: @mut 0
}
}

pub fn with_bytes_writer(f: &fn(@Writer)) -> ~[u8] {
let wr = @BytesWriter();
let wr = @BytesWriter::new();
f(wr as @Writer);
let @BytesWriter { bytes, _ } = wr;
copy *bytes
Expand Down Expand Up @@ -1762,6 +1764,12 @@ pub mod fsync {
arg: Arg<t>,
}

impl <t: Copy> Res<t> {
pub fn new(arg: Arg<t>) -> Res<t> {
Res { arg: arg }
}
}

#[unsafe_destructor]
impl<T:Copy> Drop for Res<T> {
fn finalize(&self) {
Expand All @@ -1776,12 +1784,6 @@ pub mod fsync {
}
}

pub fn Res<t: Copy>(arg: Arg<t>) -> Res<t>{
Res {
arg: arg
}
}

pub struct Arg<t> {
val: t,
opt_level: Option<Level>,
Expand All @@ -1793,7 +1795,7 @@ pub mod fsync {
// outer res
pub fn FILE_res_sync(file: &FILERes, opt_level: Option<Level>,
blk: &fn(v: Res<*libc::FILE>)) {
blk(Res(Arg {
blk(Res::new(Arg {
val: file.f, opt_level: opt_level,
fsync_fn: |file, l| {
unsafe {
Expand All @@ -1806,7 +1808,7 @@ pub mod fsync {
// fsync fd after executing blk
pub fn fd_res_sync(fd: &FdRes, opt_level: Option<Level>,
blk: &fn(v: Res<fd_t>)) {
blk(Res(Arg {
blk(Res::new(Arg {
val: fd.fd, opt_level: opt_level,
fsync_fn: |fd, l| os::fsync_fd(fd, l) as int
}));
Expand All @@ -1818,7 +1820,7 @@ pub mod fsync {
// Call o.fsync after executing blk
pub fn obj_sync(o: @FSyncable, opt_level: Option<Level>,
blk: &fn(v: Res<@FSyncable>)) {
blk(Res(Arg {
blk(Res::new(Arg {
val: o, opt_level: opt_level,
fsync_fn: |o, l| o.fsync(l)
}));
Expand Down Expand Up @@ -1993,7 +1995,7 @@ mod tests {
#[test]
fn bytes_buffer_overwrite() {
let wr = BytesWriter();
let wr = BytesWriter::new();
wr.write([0u8, 1u8, 2u8, 3u8]);
assert!(*wr.bytes == ~[0u8, 1u8, 2u8, 3u8]);
wr.seek(-2, SeekCur);
Expand Down

0 comments on commit e1c1c05

Please sign in to comment.