From e4372c43a4624a225370e3ef303654bc595d8c2a Mon Sep 17 00:00:00 2001 From: P1start Date: Sat, 6 Jun 2015 13:58:37 +1200 Subject: [PATCH 1/2] =?UTF-8?q?Add=20some=20of=20`[T]`=E2=80=99s=20methods?= =?UTF-8?q?=20to=20`str`=20and=20vice=20versa?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- text/0000-slice-string-symmetry.md | 68 ++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 text/0000-slice-string-symmetry.md diff --git a/text/0000-slice-string-symmetry.md b/text/0000-slice-string-symmetry.md new file mode 100644 index 00000000000..e6cbf9490e5 --- /dev/null +++ b/text/0000-slice-string-symmetry.md @@ -0,0 +1,68 @@ +- Feature Name: `slice_string_symmetry` +- Start Date: 2015-06-06 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary + +Add some methods that already exist on slices to strings and vice versa. +Specifically, the following methods should be added: + +- `str::chunks` +- `str::windows` +- `str::into_string` +- `String::into_boxed_slice` +- `<[T]>::subslice_offset` + +# Motivation + +Conceptually, strings and slices are similar types. Many methods are already +shared between the two types due to their similarity. However, not all methods +are shared between the types, even though many could be. This is a little +unexpected and inconsistent. Because of that, this RFC proposes to remedy this +by adding a few methods to both strings and slices to even out these two types’ +available methods. + +# Detailed design + +Add the following methods to `str`, presumably as inherent methods: + +- `chunks(&self, n: usize) -> Chunks`: Returns an iterator that yields the + *characters* (not bytes) of the string in groups of `n` at a time. Iterator + element type: `&str`. + +- `windows(&self, n: usize) -> Windows`: Returns an iterator over all contiguous + windows of character length `n`. Iterator element type: `&str`. + +- `into_string(self: Box) -> String`: Returns `self` as a `String`. This is + equivalent to `[T]`’s `into_vec`. + +`split_at(&self, mid: usize) -> (&str, &str)` would also be on this list, but +there is [an existing RFC](https://github.com/rust-lang/rfcs/pull/1123) for it. + +Add the following method to `String` as an inherent method: + +- `into_boxed_slice(self) -> Box`: Returns `self` as a `Box`, + reallocating to cut off any excess capacity if needed. This is required to + provide a safe means of creating `Box`. + +Add the following method to `[T]` (for all `T`), presumably as an inherent +method: + +- `subslice_offset(&self, inner: &[T]) -> usize`: Returns the offset (in + elements) of an inner slice relative to an outer slice. Panics of `inner` is + not contained within `self`. + +# Drawbacks + +- `str::subslice_offset` is already unstable, so creating a similar method on + `[T]` is perhaps not such a good idea. + +# Alternatives + +- Do a subset of the proposal. For example, the `Box`-related methods could + be removed. + +# Unresolved questions + +None. From 566bb704942f68645d992cbaac36e4b7eb635f80 Mon Sep 17 00:00:00 2001 From: P1start Date: Sat, 27 Jun 2015 15:57:35 +1200 Subject: [PATCH 2/2] Remove `str::{windows,chunks}` and `<[T]>::subslice_offset` --- text/0000-slice-string-symmetry.md | 63 +++++++++++++++--------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/text/0000-slice-string-symmetry.md b/text/0000-slice-string-symmetry.md index e6cbf9490e5..d7cc405275a 100644 --- a/text/0000-slice-string-symmetry.md +++ b/text/0000-slice-string-symmetry.md @@ -5,14 +5,11 @@ # Summary -Add some methods that already exist on slices to strings and vice versa. -Specifically, the following methods should be added: +Add some methods that already exist on slices to strings. Specifically, the +following methods should be added: -- `str::chunks` -- `str::windows` - `str::into_string` -- `String::into_boxed_slice` -- `<[T]>::subslice_offset` +- `String::into_boxed_str` # Motivation @@ -20,48 +17,52 @@ Conceptually, strings and slices are similar types. Many methods are already shared between the two types due to their similarity. However, not all methods are shared between the types, even though many could be. This is a little unexpected and inconsistent. Because of that, this RFC proposes to remedy this -by adding a few methods to both strings and slices to even out these two types’ -available methods. +by adding a few methods to strings to even out these two types’ available +methods. -# Detailed design - -Add the following methods to `str`, presumably as inherent methods: +Specifically, it is currently very difficult to construct a `Box`, while it +is fairly simple to make a `Box<[T]>` by using `Vec::into_boxed_slice`. This RFC +proposes a means of creating a `Box` by converting a `String`. -- `chunks(&self, n: usize) -> Chunks`: Returns an iterator that yields the - *characters* (not bytes) of the string in groups of `n` at a time. Iterator - element type: `&str`. +# Detailed design -- `windows(&self, n: usize) -> Windows`: Returns an iterator over all contiguous - windows of character length `n`. Iterator element type: `&str`. +Add the following method to `str`, presumably as an inherent method: - `into_string(self: Box) -> String`: Returns `self` as a `String`. This is equivalent to `[T]`’s `into_vec`. -`split_at(&self, mid: usize) -> (&str, &str)` would also be on this list, but -there is [an existing RFC](https://github.com/rust-lang/rfcs/pull/1123) for it. - Add the following method to `String` as an inherent method: -- `into_boxed_slice(self) -> Box`: Returns `self` as a `Box`, +- `into_boxed_str(self) -> Box`: Returns `self` as a `Box`, reallocating to cut off any excess capacity if needed. This is required to - provide a safe means of creating `Box`. - -Add the following method to `[T]` (for all `T`), presumably as an inherent -method: + provide a safe means of creating `Box`. This is equivalent to `Vec`’s + `into_boxed_slice`. -- `subslice_offset(&self, inner: &[T]) -> usize`: Returns the offset (in - elements) of an inner slice relative to an outer slice. Panics of `inner` is - not contained within `self`. # Drawbacks -- `str::subslice_offset` is already unstable, so creating a similar method on - `[T]` is perhaps not such a good idea. +None, yet. # Alternatives -- Do a subset of the proposal. For example, the `Box`-related methods could - be removed. +- The original version of this RFC had a few extra methods: + - `str::chunks(&self, n: usize) -> Chunks`: Returns an iterator that yields + the *characters* (not bytes) of the string in groups of `n` at a time. + Iterator element type: `&str`. + + - `str::windows(&self, n: usize) -> Windows`: Returns an iterator over all + contiguous windows of character length `n`. Iterator element type: `&str`. + + This and `str::chunks` aren’t really useful without proper treatment of + graphemes, so they were removed from the RFC. + + - `<[T]>::subslice_offset(&self, inner: &[T]) -> usize`: Returns the offset + (in elements) of an inner slice relative to an outer slice. Panics of + `inner` is not contained within `self`. + + `str::subslice_offset` isn’t yet stable and its usefulness is dubious, so + this method was removed from the RFC. + # Unresolved questions