diff options
| author | Ben Blum <bblum@andrew.cmu.edu> | 2013-06-20 20:18:16 -0400 |
|---|---|---|
| committer | Ben Blum <bblum@andrew.cmu.edu> | 2013-06-26 18:14:43 -0400 |
| commit | 12e09afd6d7bb4ca30e572a5214c11284ea1965b (patch) | |
| tree | 2ae785a2f47d9c9b9c7e98ab5bff7a53748d2a21 | |
| parent | 3433851a37f04621e0c091ec2180aeddbdd5ada5 (diff) | |
| download | rust-12e09afd6d7bb4ca30e572a5214c11284ea1965b.tar.gz rust-12e09afd6d7bb4ca30e572a5214c11284ea1965b.zip | |
Work-around 'static bound requirement in io::with_bytes_reader (note: does not fix #5723, interface still unsafe)
| -rw-r--r-- | src/libextra/io_util.rs | 6 | ||||
| -rw-r--r-- | src/libstd/io.rs | 13 |
2 files changed, 15 insertions, 4 deletions
diff --git a/src/libextra/io_util.rs b/src/libextra/io_util.rs index 91424ae3ba2..11dea1c3a70 100644 --- a/src/libextra/io_util.rs +++ b/src/libextra/io_util.rs @@ -10,6 +10,7 @@ use core::io::{Reader, BytesReader}; use core::io; +use core::cast; /// An implementation of the io::Reader interface which reads a buffer of bytes pub struct BufReader { @@ -29,10 +30,13 @@ impl BufReader { } fn as_bytes_reader<A>(&self, f: &fn(&BytesReader) -> A) -> A { + // XXX FIXME(#5723) + let bytes = ::core::util::id::<&[u8]>(self.buf); + let bytes: &'static [u8] = unsafe { cast::transmute(bytes) }; // Recreating the BytesReader state every call since // I can't get the borrowing to work correctly let bytes_reader = BytesReader { - bytes: ::core::util::id::<&[u8]>(self.buf), + bytes: bytes, pos: @mut *self.pos }; diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 71a0dd6b9b2..a78be9c8b2b 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -1042,12 +1042,14 @@ pub fn file_reader(path: &Path) -> Result<@Reader, ~str> { // Byte readers -pub struct BytesReader<'self> { - bytes: &'self [u8], +pub struct BytesReader { + // FIXME(#5723) see other FIXME below + // FIXME(#7268) this should also be parameterized over <'self> + bytes: &'static [u8], pos: @mut uint } -impl<'self> Reader for BytesReader<'self> { +impl Reader for BytesReader { fn read(&self, bytes: &mut [u8], len: uint) -> uint { let count = uint::min(len, self.bytes.len() - *self.pos); @@ -1084,6 +1086,10 @@ impl<'self> Reader for BytesReader<'self> { } pub fn with_bytes_reader<T>(bytes: &[u8], f: &fn(@Reader) -> T) -> T { + // XXX XXX XXX this is glaringly unsound + // FIXME(#5723) Use a &Reader for the callback's argument. Should be: + // fn with_bytes_reader<'r, T>(bytes: &'r [u8], f: &fn(&'r Reader) -> T) -> T + let bytes: &'static [u8] = unsafe { cast::transmute(bytes) }; f(@BytesReader { bytes: bytes, pos: @mut 0 @@ -1091,6 +1097,7 @@ pub fn with_bytes_reader<T>(bytes: &[u8], f: &fn(@Reader) -> T) -> T { } pub fn with_str_reader<T>(s: &str, f: &fn(@Reader) -> T) -> T { + // FIXME(#5723): As above. with_bytes_reader(s.as_bytes(), f) } |
