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

We need an core::at_str library #3604

Closed
erickt opened this issue Sep 27, 2012 · 7 comments
Closed

We need an core::at_str library #3604

erickt opened this issue Sep 27, 2012 · 7 comments

Comments

@erickt
Copy link
Contributor

erickt commented Sep 27, 2012

There's no way to convert between a ~str and a @str. We need a library that provides this and other functionality.

@brson
Copy link
Contributor

brson commented Sep 27, 2012

Or alternately better integration of @Strs and @Vecs into core::str and vec. More abstraction is called for. The at_vec module hasn't been a huge success.

@nikomatsakis
Copy link
Contributor

So, I know that @pcwalton is opposed because it makes the signatures complex (which it does), but making use of static typeclass methods and the builder interface stuff, it is totally plausible to have all methods on iterables generate either ~[] or @[] or even user-defined impls, depending on what the inferencer decides is necessary.

Basically map() (here shown in its most generic form, for any iterable) would look like:

fn map<A, IA: Iterable<A>, B, C: Buildable<B>>(
    input: IA, conv: fn(&A) -> B) -> C
{
    do Buildable::build |push| {
        for input.each |a| {
            push(conv(a));
        }
    }
}

And the Buildable iface would look something like:

type PushFn<T> = &fn(+t: T);
trait Buildable<T> {
    pure fn build(pusher: &pure fn(PushFn<T>)) -> self;
}

And the impl for ~[] would look like:

impl<T> ~[T]: Buildable<T> {
    pure fn build(pusher: &pure fn(PushFn<T>)) -> ~[T] {
        let mut vec = ~[];
        do pusher |elem| {
            // unsafe is needed because this function should appear
            // pure from the outside.  We know it's safe because `vec`
            // does not escape, but the type system doesn't know it...

            unsafe { vec.push(elem); }
        }
        return vec;
    }
}

There are undoubtedly a lot of bugs standing between us and having this work, as well as (at least) one other enhancement that I think would be important. That enhancement is that we probably need some kind of way to help the type inferencer when the required type is underconstrained. Or else we just add type hints. I'm thinking of a situation like this:

for v.map(|e| ...).each |e| { ... }

Here, there is no way for the type inferencer to know what result type map() should have---it just needs to be something iterable. Alternatively, if you did this:

let mapped: ~[T] = v.map(|e| ...);
for mapped.each |e| { ... }

then the type inferencer would know what to do.

The downside of this scheme is that... well... it IS complex. But it'd be nice not to need to define higher-order things likemap() more than once.

Note: Here I've used the "receiver-less" fn notation that we intend to move to, rather than the keyword static that is currently required. I also invoked the "receiver-less" fn by prefixing it with the trait name, as I'd like to do. :)

@jruderman
Copy link
Contributor

Or library functions could generally build ~strs, and Rust could allow casting a ~str to @str.

@jruderman
Copy link
Contributor

Which is #3450

@catamorphism
Copy link
Contributor

I don't think this will break existing code, so clearing the milestone and nominating for milestone 3, feature-complete. I think we need some discussion about the str situation.

@pnkfelix
Copy link
Member

accepted for feature complete by analogy with #3450

@pnkfelix
Copy link
Member

wontfix, @str should go away.

bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
RalfJung pushed a commit to RalfJung/rust that referenced this issue May 19, 2024
intrinsics: just panic when they get used incorrectly

This is already what we do most of the time, so do it consistently.
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

No branches or pull requests

6 participants