Skip to content

Implement std::ascii::AsciiExt and std::ascii::OwnedAsciiExt for Ascii #3

Closed
@tomprogrammer

Description

@tomprogrammer

Motivation

Preliminary
While str guarantees statically that data of its type is valid UTF-8, the type Ascii guarantees ASCII-conformance. Therefore the types [Ascii] and str and their owned counterparts Vec<Ascii> and String should behave similar.

Topic of this Issue
Ascii provides functions like to_uppercase() and to_lowercase() which can be applied to single ascii-characters. Currently such operations are not implemented on owned or borrowed strings of ascii-characters. As the types Vec<Ascii> and [Ascii] should be opaque manually implementing the iteration isn't recommended because it is a implementation detail of these types.

Example:

error: type `&[Ascii]` does not implement any method in scope named `to_uppercase`
let _ = "abcXYZ".to_ascii().unwrap().to_uppercase();
                                     ^~~~~~~~~~~~~~~~~~~~

Design

The types String and str provide functionality for converting to uppercase and lowercase with their implementations of the traits std::ascii::{AsciiExt, OwnedAsciiExt}. These traits are intended for "[…] ASCII-subset only operations on string slices" and owned strings. Of course Vec<Ascii> and [Ascii] are subsets of ascii, they are equivalent so it's valid to implement them for the ascii only string types.

Implement the traits:

impl AsciiExt<Vec<Ascii>> for [Ascii]
impl AsciiExt for Ascii
impl OwnedAsciiExt for Vec<Ascii>

The implementations use functionality present in Ascii if possible.

Design flaws

  • The traits std::ascii::{AsciiExt, OwnedAsciiExt} are marked experimental. This shouldn't be a real issue as I expect this crate to follow the way conversions are done in the standard library.
  • All functions declared by the traits std::ascii::{AsciiExt, OwnedAsciiExt} carry the infix ascii which is redundant in the case the traits are implemented on Vec<Ascii>, [Ascii] and Ascii. This redundancy must be tolerated to achieve the goals described above.

Drawbacks

  • Duplicate implementations arise because the type Ascii implements the same functionality in the functions to_uppercase() / to_ascii_uppercase() and to_lowercase() and to_ascii_lowercase().

Further considerations after discussion

  • Deprecate and remove the functions to_uppercase() and to_lowercase() on Ascii in favour of their equivalents in AsciiExt. That removes the duplication mentioned in the drawbacks.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions