Skip to content

borrowed pointers vs generics #2991

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

Closed
jesse99 opened this issue Jul 22, 2012 · 1 comment
Closed

borrowed pointers vs generics #2991

jesse99 opened this issue Jul 22, 2012 · 1 comment

Comments

@jesse99
Copy link
Contributor

jesse99 commented Jul 22, 2012

Been trying to get my code working with the latest rust (master from July 22) and am stymied by the code below:

// rustc --lib owned.rs
type parser<T: copy> = fn@ (state) -> status<T>;

type state = {file: ~str, text: @[char], index: uint, line: int};

type status<T: copy> = result::result<succeeded<T>, failed>;

type succeeded<T: copy> = {new_state: state, value: T};

type failed = {old_state: state, err_state: state, mesg: ~str};

fn return<T: copy>(value: T) -> parser<T>
{
    |input: state|
    {
        result::ok({new_state: input, value: value})
    }
}

fn thene<T: copy, U: copy>(parser: parser<T>, eval: fn@ (T) -> parser<U>) -> parser<U>
{
    |input: state|
    {
        do result::chain(parser(input))
        |pass|
        {
            do result::chain_err(eval(pass.value)(pass.new_state))
            |failure|
            {
                result::err({old_state: input with failure})
            }
        }
    }
}

fn compose<T: copy, U: copy>(parser: parser<T>, op: parser<U>) -> parser<(U, T)>
{
    do thene(op)
    |operator|
    {
        do thene(parser)
        |value|
        {
            return((operator, value))
        }
    }
}

// owned.rs:16:39: 16:44 error: value may contain borrowed pointers; use `owned` bound
// owned.rs:16         result::ok({new_state: input, value: value})
//                                                         ^~~~~

// Note that I can't use an owned bound because owned does not imply copy.

Barring a complete rewrite, I don't see any way I can get the compiler to accept the code.

@nikomatsakis
Copy link
Contributor

You can have more than one bound, simply separate them with spaces (e.g. T: owned copy).

That said, I do not think an owned bound should be required here, as the type in question (T) appears within the fn type. This rule is not correctly enforced for functions (it is correctly enforced for ifaces). I will open a separate question on this matter.

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

2 participants