gpt4 book ai didi

types - 在结构内调用 Boxed 函数时类型不匹配

转载 作者:行者123 更新时间:2023-11-29 08:12:50 24 4
gpt4 key购买 nike

我试图勾勒出一种运行根据 struct Problem 表示的“问题”的通用方法。以下。这些结构包括测试输入/输出和求解器函数。我的目标是通过与标准 I/O 交互或通过一些验证 solver 有效性的测试器函数来运行这些问题。针对给定的 test_input/test_output .此处的草图仅包含测试器功能。

当我尝试编译它时,出现类型不匹配:

problem.rs:43:22: 43:27 error: mismatched types:
expected `R`,
found `std::io::cursor::Cursor<&[u8]>`
(expected type parameter,
found struct `std::io::cursor::Cursor`) [E0308]
problem.rs:43 (problem.solver)(input, &mut output);
^~~~~
problem.rs:43:22: 43:27 help: run `rustc --explain E0308` to see a detailed explanation
problem.rs:43:29: 43:40 error: mismatched types:
expected `&mut W`,
found `&mut std::io::cursor::Cursor<collections::vec::Vec<u8>>`
(expected type parameter,
found struct `std::io::cursor::Cursor`) [E0308]
problem.rs:43 (problem.solver)(input, &mut output);
^~~~~~~~~~~
problem.rs:43:29: 43:40 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to 2 previous errors

我期待编译器接受 Cursor<&[u8]>作为BufRead呃,还有 Cursor<Vec<u8>>作为Write r,但是没用。我不知道对 R 的期望是什么或 W .

我试图通过直接调用求解器来运行类似的测试器函数,而不涉及 Problem完全没有,而且有效;所以我想这些错误可能与我调用 (problem.solver)(input, &mut output) 的方式有关.除此之外,我没有任何线索。有人可以告诉我这里发生了什么吗?

use std::io::prelude::*;
use problems::Problem;

mod problems {
use std::io::prelude::*;

pub struct Problem<'a, R, W>
where R: BufRead, W: Write
{
test_input: &'a str,
test_output: &'a str,
solver: Box<fn(R, &mut W)>,
}

mod a_problem {
use std::io::prelude::*;
use super::Problem;

pub fn getProblem<'a, R, W>() -> Problem<'a, R, W>
where R: BufRead, W: Write
{
Problem {
test_input: unimplemented!(),
test_output: unimplemented!(),
solver: Box::new(solver),
}
}

fn solver<R, W>(input: R, output: &mut W)
where R: BufRead, W: Write
{
unimplemented!();
}
}
}

fn test_solver<R, W>(problem: Problem<R, W>)
where R: BufRead, W: Write
{
let input = std::io::Cursor::new(problem.test_input.as_bytes());
let mut output = std::io::Cursor::new(Vec::<u8>::new());

(problem.solver)(input, &mut output);
assert_eq!(problem.test_output.as_bytes(), &*output.into_inner());
}

fn main() {
let problem = problems::a_problem::getProblem();
test_solver(problem);
}

最佳答案

您定义 Problem 结构的方式意味着 RWProblem 时是固定的被实例化。在 test_solver 中,您允许调用者指定他们想要的任何 R 和任何 W,但随后您尝试传递特定的 Cursor 类型改为 (problem.solver)

您可以通过两种方式解决此问题:

1) 不要将 test_solver 定义为通用函数。请注意,您不会为 RW 多次调用 (problem.solver)

fn test_solver<'a>(problem: Problem<'a, std::io::Cursor<&'a [u8]>, std::io::Cursor<Vec<u8>>>)
{
let input = std::io::Cursor::new(problem.test_input.as_bytes());
let mut output = std::io::Cursor::new(Vec::<u8>::new());

(problem.solver)(input, &mut output);
assert_eq!(problem.test_output.as_bytes(), &*output.into_inner());
}

2) 不要将solver 字段定义为通用的。让函数采用特征引用。

use std::io::prelude::*;
use problems::Problem;

mod problems {
use std::io::prelude::*;

pub struct Problem<'a>
{
pub test_input: &'a str,
pub test_output: &'a str,
pub solver: Box<fn(&mut BufRead, &mut Write)>,
}

pub mod a_problem {
use std::io::prelude::*;
use super::Problem;

pub fn getProblem<'a>() -> Problem<'a>
{
Problem {
test_input: unimplemented!(),
test_output: unimplemented!(),
solver: Box::new(solver),
}
}

fn solver(input: &mut BufRead, output: &mut Write)
{
unimplemented!();
}
}
}

fn test_solver(problem: Problem)
{
let mut input = std::io::Cursor::new(problem.test_input.as_bytes());
let mut output = std::io::Cursor::new(Vec::<u8>::new());

(problem.solver)(&mut input, &mut output);
assert_eq!(problem.test_output.as_bytes(), &*output.into_inner());
}

fn main() {
let problem = problems::a_problem::getProblem();
test_solver(problem);
}

关于types - 在结构内调用 Boxed 函数时类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33319755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com