Cursor
struct Cursor
The Cursor
use for make a Bytes
to Read
interface.
For example we have Bytes
and we want to read it as Read
interface.
Use Cursor
to wrap the Bytes
and then we can read it as Read
interface.
nv
use std.io.{self, Cursor};
let bytes = "Hello World".bytes();
let cursor = Cursor.new(bytes);
fn some_method(reader: io.Read) {
// do something
}
// Now we can use `cursor` as `io.Read` interface.
some_method(cursor);
Implementions
Methods
[src]
pub fn read(self, bytes: Bytes): int throws IoError {
let n = self.bytes.len() - self.pos;
if (n < 0) {
return 0;
}
let n = n.min(bytes.len());
bytes.copy_from(self.bytes.slice(self.pos, self.pos + n));
self.pos += n;
return n;
}
[src]
pub fn read(self, bytes: Bytes): int throws IoError {
let n = self.bytes.len() - self.pos;
if (n < 0) {
return 0;
}
let n = n.min(bytes.len());
bytes.copy_from(self.bytes.slice(self.pos, self.pos + n));
self.pos += n;
return n;
}
[src]
pub fn seek(self, offset: int, whence: SeekFrom): int throws IoError {
let n: int;
switch (whence) {
case .Start:
n = offset;
case .End:
n = self.bytes.len() + offset;
case .Current:
n = self.pos + offset;
}
if (n < 0) {
throw IoError.other("invalid seek to a negative or overflowing position");
}
self.pos = n;
return n;
}
[src]
pub fn seek(self, offset: int, whence: SeekFrom): int throws IoError {
let n: int;
switch (whence) {
case .Start:
n = offset;
case .End:
n = self.bytes.len() + offset;
case .Current:
n = self.pos + offset;
}
if (n < 0) {
throw IoError.other("invalid seek to a negative or overflowing position");
}
self.pos = n;
return n;
}
stream_len
pub fn stream_len(self): int throws IoError
[src]
pub fn stream_len(self): int throws IoError {
return self.bytes.len();
}
stream_len
pub fn stream_len(self): int throws IoError
[src]
pub fn stream_len(self): int throws IoError {
return self.bytes.len();
}
stream_position
pub fn stream_position(self): int throws IoError
[src]
pub fn stream_position(self): int throws IoError {
return self.pos;
}
stream_position
pub fn stream_position(self): int throws IoError
[src]
pub fn stream_position(self): int throws IoError {
return self.pos;
}
[src]
pub fn new(value: Bytes | string): Cursor {
switch (let value = value.(type)) {
case string:
return Cursor { bytes: value.bytes(), pos: 0 };
case Bytes:
return Cursor { bytes: value, pos: 0 };
}
}
Creates a new Cursor
wrapping the given string
| std.io.Bytes
.
[src]
pub fn new(value: Bytes | string): Cursor {
switch (let value = value.(type)) {
case string:
return Cursor { bytes: value.bytes(), pos: 0 };
case Bytes:
return Cursor { bytes: value, pos: 0 };
}
}
Creates a new Cursor
wrapping the given string
| std.io.Bytes
.
Creates a new Cursor
wrapping the given string
| std.io.Bytes
.
nv
use std.io.{Read, Cursor};
let reader: Read = Cursor.new("hello".bytes());
assert_eq try! reader.read_to_string(), "hello";
let reader: Read = Cursor.new("hello");
assert_eq try! reader.read_to_string(), "hello";