如何借用结构的咖喱函数字段?

时间:2020-04-22 16:38:43

标签: rust traits currying borrow-checker parser-combinators

我有一个结构Parser,其中一个字段transformer用于保存函数。此函数返回一个闭包,该闭包处理ParserState并返回ParserState。我已经创建了一个函数str_parser,该函数正在用作Parser的新实例的转换器。

pub struct Parser<F> 
    where F: FnOnce(ParserState) -> ParserState {
    pub transformer: F
}

#[derive(Debug, PartialEq, Clone)]
pub struct ParserState {
    pub target: String,
    pub index: usize,
    pub result: Option<String>, // the container of eventual results from the parsing, with Some(result) or None
    pub error: bool,            // whether we've encountered an error; index -> 0, Some(err_msg)
    pub err_msg: Option<String> // Eventual error message
}

impl<F> Parser<F> 
    where F: FnOnce(ParserState) -> ParserState {
    pub fn new(f: F) -> Self {
        // creating a new Parser just means deciding on which closure it applies
        Parser {
            transformer: f,
        }
    }

    pub fn run(&self, corpus: String) -> ParserState {
        let state = ParserState {
            target: corpus,
            index: 0,
            result: None,
            error: false,
            err_msg: None
        };
        return (self.transformer)(state);
    }
}

pub fn str_parser(needle: String) -> impl FnOnce(ParserState) -> ParserState {
    let parser = move |state: ParserState| {
        let target_string = state.target;
        let index = state.index;
        if target_string[index..needle.len()] == needle {
            ParserState {
                target: target_string,
                index: index + needle.len(),
                result: Some(needle),
                error: false,
                err_msg: None
            }
        } else {
            ParserState {
                target: String::from(""),
                index: 0,
                result: None,
                error: true,
                err_msg: Some(String::from("Error"))
            }
        }
    };
    parser
}

由于出现此错误,借位检查器拒绝让我将此函数应用于提供的参数,

error[E0507]: cannot move out of `self.transformer` which is behind a shared reference
  --> src/parsers.rs:32:16
   |
32 |         return (self.transformer)(state);
   |                ^^^^^^^^^^^^^^^^^^ move occurs because `self.transformer` has type `F`, which does not implement the `Copy` trait

我该如何处理?在这种情况下实现Copy特质对我来说似乎是艰巨的。有没有办法使这一举措合法化,同时仍然允许我使用此str_parser函数?

1 个答案:

答案 0 :(得分:1)

在这种情况下,我有一个Parser,除了transformer之外没有其他任何状态。因此,我不需要使用FnOnceFn是我所需要的。从那里,我修改了str_parser函数中使用的闭包以克隆needle字符串:

pub struct Parser<F> 
    where F: Fn(ParserState) -> ParserState {
    pub transformer: F
}

#[derive(Debug, PartialEq, Clone)]
pub struct ParserState {
    pub target: String,
    pub index: usize,
    pub result: Option<String>, // the container of eventual results from the parsing, with Some(result) or None
    pub error: bool,            // whether we've encountered an error; index -> 0, Some(err_msg)
    pub err_msg: Option<String> // Eventual error message
}

impl<F> Parser<F> 
    where F: Fn(ParserState) -> ParserState {
    pub fn new(f: F) -> Self {
        // creating a new Parser just means deciding on which closure it applies
        Parser {
            transformer: f,
        }
    }

    pub fn run(&self, corpus: String) -> ParserState {
        let state = ParserState {
            target: corpus,
            index: 0,
            result: None,
            error: false,
            err_msg: None
        };
        return (self.transformer)(state);
    }
}

pub fn str_parser(needle: String) -> impl Fn(ParserState) -> ParserState {
    move |mut state: ParserState| {
        let target_string = state.target;
        let index = state.index;
        if target_string[index..needle.len()] == needle {
            state = ParserState {
                target: target_string,
                index: index + needle.len(),
                result: Some(needle.clone()),
                error: false,
                err_msg: None
            }
        } else {
            state = ParserState {
                target: String::from(""),
                index: 0,
                result: None,
                error: true,
                err_msg: Some(String::from("Error"))
            }
        }
        state
    }
}