|
| 1 | +- Feature Name: osstring_simple_functions |
| 2 | +- Start Date: 2015-10-04 |
| 3 | +- RFC PR: (leave this empty) |
| 4 | +- Rust Issue: (leave this empty) |
| 5 | + |
| 6 | +# Summary |
| 7 | + |
| 8 | +Add some additional utility methods to OsString and OsStr. |
| 9 | + |
| 10 | +# Motivation |
| 11 | + |
| 12 | +OsString and OsStr are extremely bare at the moment; some utilities would make them |
| 13 | +easier to work with. The given set of utilities is taken from String, and don't add |
| 14 | +any additional restrictions to the implementation. |
| 15 | + |
| 16 | +I don't think any of the proposed methods are controversial. |
| 17 | + |
| 18 | +# Detailed design |
| 19 | + |
| 20 | +Add the following methods to OsString: |
| 21 | + |
| 22 | +```rust |
| 23 | +/// Creates a new `OsString` with the given capacity. The string will be able |
| 24 | +/// to hold exactly `capacity` bytes without reallocating. If `capacity` is 0, |
| 25 | +/// the string will not allocate. |
| 26 | +/// |
| 27 | +/// See main `OsString` documentation information about encoding. |
| 28 | +fn with_capacity(capacity: usize) -> OsString; |
| 29 | + |
| 30 | +/// Truncates `self` to zero length. |
| 31 | +fn clear(&mut self); |
| 32 | + |
| 33 | +/// Returns the number of bytes this `OsString` can hold without reallocating. |
| 34 | +/// |
| 35 | +/// See `OsString` introduction for information about encoding. |
| 36 | +fn capacity(&self) -> usize; |
| 37 | + |
| 38 | +/// Reserves capacity for at least `additional` more bytes to be inserted in the |
| 39 | +/// given `OsString`. The collection may reserve more space to avoid frequent |
| 40 | +/// reallocations. |
| 41 | +fn reserve(&mut self, additional: usize); |
| 42 | + |
| 43 | +/// Reserves the minimum capacity for exactly `additional` more bytes to be |
| 44 | +/// inserted in the given `OsString`. Does nothing if the capacity is already |
| 45 | +/// sufficient. |
| 46 | +/// |
| 47 | +/// Note that the allocator may give the collection more space than it |
| 48 | +/// requests. Therefore capacity can not be relied upon to be precisely |
| 49 | +/// minimal. Prefer reserve if future insertions are expected. |
| 50 | +fn reserve_exact(&mut self, additional: usize); |
| 51 | +``` |
| 52 | + |
| 53 | +Add the following methods to OsStr: |
| 54 | + |
| 55 | +```rust |
| 56 | +/// Checks whether `self` is empty. |
| 57 | +fn is_empty(&self) -> bool; |
| 58 | + |
| 59 | +/// Returns the number of bytes in this string. |
| 60 | +/// |
| 61 | +/// See `OsStr` introduction for information about encoding. |
| 62 | +fn len(&self) -> usize; |
| 63 | +``` |
| 64 | + |
| 65 | +# Drawbacks |
| 66 | + |
| 67 | +The meaning of `len()` might be a bit confusing because it's the size of |
| 68 | +the internal representation on Windows, which isn't otherwise visible to the |
| 69 | +user. |
| 70 | + |
| 71 | +# Alternatives |
| 72 | + |
| 73 | +None. |
| 74 | + |
| 75 | +# Unresolved questions |
| 76 | + |
| 77 | +None. |
0 commit comments