Skip to content

Add StringReader, StringWriter, and StringStream #10564

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
emberian opened this issue Nov 19, 2013 · 6 comments
Closed

Add StringReader, StringWriter, and StringStream #10564

emberian opened this issue Nov 19, 2013 · 6 comments
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@emberian
Copy link
Member

The docs for std::io::mem mention that there should be some sort of implementation for strings.

The Reader is straightforward.

For Writer, this is tricky, since one cannot just write arbitrary data to a string, it must be UTF-8. Where should the boundary be drawn? Should individual writes be checked for UTF-8ness? Should the writer instead check validity of the underlying string when it is destructed? Should these operate on a &str (and thus not be growable), or should they make their own ~str?

I'm leaning towards a StringWriter that writes to an internal ~[u8], with an explicit fn get_string(self) -> Option<~str> that destroys the writer and returns the string if it is valid, None otherwise. Or, maybe it should be Result<~str, ~[u8]>, so the buffer could still be handled if desired.

@huonw
Copy link
Member

huonw commented Nov 19, 2013

Assuming we're not checking each byte (which seems reasonable, one could feasibly be streaming 1000 bytes at a time from something and so get split across a codepoint), couldn't the StringWriter be implemented as the get_string method on MemWriter? (Although, on naming, I'd say into_str() -> ....)

(Similarly, isn't StringReader just BufReader::new(s.as_bytes()), i.e. it could be left as that, or there could be from_utf8(s: &str) creators.)

@emberian
Copy link
Member Author

Yeah, adding a method to MemWriter sounds reasonable! The BufReader makes sense too.

@pongad
Copy link
Contributor

pongad commented Mar 12, 2014

I'm on it. Since it hasn't been updated in 4 months, if this is no longer relevant please let me know :D

@sfackler
Copy link
Member

I'm not sure this makes a ton of sense to add. Doing the conversion manually is pretty painless: str::from_utf8_owned(w.unwrap()).

@pongad
Copy link
Contributor

pongad commented Mar 12, 2014

@sfackler I see your point. If there is a consensus, we can remove this issue.

@alexcrichton
Copy link
Member

A very similar rfc was postponed, so I'm going to close this for now.

These operations are all possible with MemWriter and friends with the utf8 -> str conversion functions.

flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 30, 2023
…affate

make cast_possible_wrap work correctly for 16 bit {u,i}size

These changes make `cast_possible_wrap` aware of the different pointer widths and fixes the implementation to print the correct pointer widths.

Fixes rust-lang#9337

changelog: `cast_possible_wrap` does not lint on `u8 as isize` or `usize as i8`, since these can never wrap.
`cast_possible_wrap` now properly considers 16 bit pointer size and prints the correct bit widths.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

5 participants