Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[ucd/category] Add all_values() static fn #70

Merged
merged 1 commit into from
Aug 4, 2017
Merged

[ucd/category] Add all_values() static fn #70

merged 1 commit into from
Aug 4, 2017

Conversation

behnam
Copy link
Member

@behnam behnam commented Aug 4, 2017

The all_values() static function is a new API for character
properties implemented as flat enum types in Rust, like some of
Unicode properties of type Catalog or Enumeration, to return an
exhaustive list of all possible values.

The function is useful mainly in writing integration tests, but since is
efficient in implementation, can be used in application logic, as well.

The current list is manually created and maintained. With migration to
char_property macros, the list will be auto-generated. (Possibly
stored in a private module and only exposed via all_values() API.)

See #66

@behnam
Copy link
Member Author

behnam commented Aug 4, 2017

@CAD97, I don't think we want to wait until rustc:1.20 becomes stable for these kinds of API additions, so, instead of the approach taken in 96c0d81, I think we can go ahead with a static function for now.

We can deprecate these and use associated_consts when they become available, but don't think it can happen in time for unic:1.0.

What do you think?

@CAD97
Copy link
Collaborator

CAD97 commented Aug 4, 2017

@behnam what should the return type be? The simple case &'static [$name] doesn't seem to work: https://play.rust-lang.org/?gist=556164442d8689d67e37b043cedda9f3&version=stable

#[derive(Copy, Clone, Debug)]
enum E {
    A,
    B,
    C,
    D,
}

impl E {
    fn all_values() -> &'static [E] {
        &[E::A, E::B, E::C, E::D]
    }
}

fn main() {
    for value in E::all_values() {
        println!("{:?}", value);
    }
}
   Compiling playground v0.0.1 (file:///playground)
error[E0597]: borrowed value does not live long enough
  --> src/main.rs:11:10
   |
11 |         &[E::A, E::B, E::C, E::D]
   |          ^^^^^^^^^^^^^^^^^^^^^^^^ does not live long enough
12 |     }
   |     - temporary value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...

error: aborting due to previous error(s)

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

(I seem to recall reading somewhere that even Debug optimizes this to a static array slice, but it takes some finagling to convince the borrow checker.)


EDIT: Or, you know, this is a PR that I could read...

@CAD97
Copy link
Collaborator

CAD97 commented Aug 4, 2017

Yes, this looks good to me. I'll modify my char_property PR to use this form as well.

The `all_values()` static function is a new API for character
properties implemented as *flat* `enum` types in Rust, like some of
Unicode properties of type *Catalog* or *Enumeration*, to return an
exhaustive list of all possible values.

The function is useful mainly in writing integration tests, but since is
efficient in implementation, can be used in application logic, as well.

The current list is manually created and maintained. With migration to
`char_property` macros, the list will be auto-generated. (Possibly
stored in a private module and only exposed via `all_values()` API.)

See <#66>
@behnam behnam merged commit efa6f03 into master Aug 4, 2017
@behnam behnam deleted the allvalues branch August 4, 2017 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants