Skip to content

Commit 12e09af

Browse files
committed
Work-around 'static bound requirement in io::with_bytes_reader (note: does not fix #5723, interface still unsafe)
1 parent 3433851 commit 12e09af

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/libextra/io_util.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use core::io::{Reader, BytesReader};
1212
use core::io;
13+
use core::cast;
1314

1415
/// An implementation of the io::Reader interface which reads a buffer of bytes
1516
pub struct BufReader {
@@ -29,10 +30,13 @@ impl BufReader {
2930
}
3031

3132
fn as_bytes_reader<A>(&self, f: &fn(&BytesReader) -> A) -> A {
33+
// XXX FIXME(#5723)
34+
let bytes = ::core::util::id::<&[u8]>(self.buf);
35+
let bytes: &'static [u8] = unsafe { cast::transmute(bytes) };
3236
// Recreating the BytesReader state every call since
3337
// I can't get the borrowing to work correctly
3438
let bytes_reader = BytesReader {
35-
bytes: ::core::util::id::<&[u8]>(self.buf),
39+
bytes: bytes,
3640
pos: @mut *self.pos
3741
};
3842

src/libstd/io.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1042,12 +1042,14 @@ pub fn file_reader(path: &Path) -> Result<@Reader, ~str> {
10421042
10431043
10441044
// Byte readers
1045-
pub struct BytesReader<'self> {
1046-
bytes: &'self [u8],
1045+
pub struct BytesReader {
1046+
// FIXME(#5723) see other FIXME below
1047+
// FIXME(#7268) this should also be parameterized over <'self>
1048+
bytes: &'static [u8],
10471049
pos: @mut uint
10481050
}
10491051
1050-
impl<'self> Reader for BytesReader<'self> {
1052+
impl Reader for BytesReader {
10511053
fn read(&self, bytes: &mut [u8], len: uint) -> uint {
10521054
let count = uint::min(len, self.bytes.len() - *self.pos);
10531055
@@ -1084,13 +1086,18 @@ impl<'self> Reader for BytesReader<'self> {
10841086
}
10851087
10861088
pub fn with_bytes_reader<T>(bytes: &[u8], f: &fn(@Reader) -> T) -> T {
1089+
// XXX XXX XXX this is glaringly unsound
1090+
// FIXME(#5723) Use a &Reader for the callback's argument. Should be:
1091+
// fn with_bytes_reader<'r, T>(bytes: &'r [u8], f: &fn(&'r Reader) -> T) -> T
1092+
let bytes: &'static [u8] = unsafe { cast::transmute(bytes) };
10871093
f(@BytesReader {
10881094
bytes: bytes,
10891095
pos: @mut 0
10901096
} as @Reader)
10911097
}
10921098
10931099
pub fn with_str_reader<T>(s: &str, f: &fn(@Reader) -> T) -> T {
1100+
// FIXME(#5723): As above.
10941101
with_bytes_reader(s.as_bytes(), f)
10951102
}
10961103

0 commit comments

Comments
 (0)