gpt4 book ai didi

rust - 我对泛型函数和枚举有问题

转载 作者:行者123 更新时间:2023-12-03 11:44:50 25 4
gpt4 key购买 nike

我对通用函数(Rust Playground)有问题:

use core::any::Any;
use std::fmt;
use std::fmt::Debug;

type BoolPtr = Box<dyn Fn(String, bool) -> Result<(), String>>;
type U8Ptr = Box<dyn Fn(String, u8) -> Result<(), String>>;
type U16Ptr = Box<dyn Fn(String, u8) -> Result<(), String>>;

pub enum WriteCallbackClosure {
BOOL(BoolPtr),
U8(U8Ptr),
U16(U16Ptr),
}

pub fn create_cb_enum<T, Closure>(
var_name: &String,
var_type: &String,
callback: Closure,
) -> Option<WriteCallbackClosure>
where
Closure: 'static + Sized + std::panic::UnwindSafe + std::panic::RefUnwindSafe + Send + Sync,
T: Any,
Closure: Fn(String, T) -> Result<(), String>,
{
let box_cb = Box::new(callback);

if var_name == "BOOL" {
return Some(WriteCallbackClosure::BOOL(box_cb));
} else if var_name == "U8" {
return Some(WriteCallbackClosure::U8(box_cb));
}

return None;
}

fn main() {
let f1 = move |name, state: bool| {
println!("name: {}, state: {}", name, state);
return Ok(());
};

let v1 = create_cb_enum(&"v1_bool".to_string(), &"BOOL".to_string(), f1);

let f2 = move |name, state: u8| {
println!("name: {}, state: {}", name, state);
return Ok(());
};

let v2 = create_cb_enum(&"v2_u8".to_string(), &"U8".to_string(), f2);
}
编译器建议:
error[E0277]: expected a `std::ops::Fn<(std::string::String, bool)>` closure, found `Closure`
--> src/main.rs:28:48
|
28 | return Some(WriteCallbackClosure::BOOL(box_cb));
| ^^^^^^ expected an `Fn<(std::string::String, bool)>` closure, found `Closure`
|
= note: required for the cast to the object type `dyn std::ops::Fn(std::string::String, bool) -> std::result::Result<(), std::string::String>`
help: consider further restricting type parameter `Closure`
|
23 | Closure: Fn(String, T) -> Result<(), String>, Closure: std::ops::Fn<(std::string::String, bool)>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: expected a `std::ops::Fn<(std::string::String, u8)>` closure, found `Closure`
--> src/main.rs:30:46
|
30 | return Some(WriteCallbackClosure::U8(box_cb));
| ^^^^^^ expected an `Fn<(std::string::String, u8)>` closure, found `Closure`
|
= note: required for the cast to the object type `dyn std::ops::Fn(std::string::String, u8) -> std::result::Result<(), std::string::String>`
help: consider further restricting type parameter `Closure`
|
23 | Closure: Fn(String, T) -> Result<(), String>, Closure: std::ops::Fn<(std::string::String, u8)>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
这很奇怪,因为 function是一个称为 Closure的泛型: Fn(String, T) -> Result<(), String>

最佳答案

已更新,以反射(reflect)非静态Fn (请参见下面的原始答案)
Playground

type BoolPtr<'a> = Box<dyn 'a + FnMut(String, bool) -> Result<(), String>>;
type U8Ptr<'a> = Box<dyn 'a + FnMut(String, u8) -> Result<(), String>>;
type U16Ptr<'a> = Box<dyn 'a + FnMut(String, u16) -> Result<(), String>>;

pub enum WriteCallbackClosure<'a>
{
BOOL(BoolPtr<'a>),
U8(U8Ptr<'a>),
U16(U16Ptr<'a>),
}

trait WriteCallbackCreatorTrait<T> {
fn from_fn<'a, P> (v: P) -> Option<WriteCallbackClosure<'a>>
where P: 'a + Sized + FnMut(String, T) -> Result<(), String>;
}

impl WriteCallbackCreatorTrait<bool> for WriteCallbackClosure<'_> {
fn from_fn<'a, P>(v: P) -> Option<WriteCallbackClosure<'a>>
where P: 'a + Sized + FnMut(String, bool) -> Result<(), String>
{

let boxed = Box::new(v);
let closure = WriteCallbackClosure::BOOL(boxed);
return Some(closure);
}
}


impl WriteCallbackCreatorTrait<u8> for WriteCallbackClosure<'_> {
fn from_fn<'a, P>(v: P) -> Option<WriteCallbackClosure<'a>>
where P: 'a + Sized + FnMut(String, u8) -> Result<(), String>
{
let boxed = Box::new(v);
let closure = WriteCallbackClosure::U8(boxed);
return Some(closure);
}
}

impl WriteCallbackCreatorTrait<u16> for WriteCallbackClosure<'_> {
fn from_fn<'a, P>(v: P) -> Option<WriteCallbackClosure<'a>>
where P: 'a + Sized + FnMut(String, u16) -> Result<(), String>
{
let boxed = Box::new(v);
let closure = WriteCallbackClosure::U16(boxed);
return Some(closure);
}
}

fn main() {
let mut my_state = false;

let f1 = |name, state: bool| {
println!("name: {}, state: {}", name, state);
my_state = state;
return Ok(());
};

let _v1 = WriteCallbackClosure::from_fn(f1);


}
原始答案
看起来您在寻找类似这样的东西( Playground)
type BoolPtr = Box<dyn Fn(String, bool) -> Result<(), String>>;
type U8Ptr = Box<dyn Fn(String, u8) -> Result<(), String>>;
type U16Ptr = Box<dyn Fn(String, u16) -> Result<(), String>>;

pub enum WriteCallbackClosure
{
BOOL(BoolPtr),
U8(U8Ptr),
U16(U16Ptr),
}

trait WriteCallbackCreatorTrait<T> {
fn from_fn<P> (v: P) -> Option<WriteCallbackClosure>
where P: 'static + Sized + Fn(String, T) -> Result<(), String>;
}

impl WriteCallbackCreatorTrait<bool> for WriteCallbackClosure {
fn from_fn<P>(v: P) -> Option<WriteCallbackClosure>
where P: 'static + Sized + Fn(String, bool) -> Result<(), String>
{
let boxed = Box::new(v);
let closure = WriteCallbackClosure::BOOL(boxed);
return Some(closure);
}
}

impl WriteCallbackCreatorTrait<u8> for WriteCallbackClosure {
fn from_fn<P>(v: P) -> Option<WriteCallbackClosure>
where P: 'static + Sized + Fn(String, u8) -> Result<(), String>
{
let boxed = Box::new(v);
let closure = WriteCallbackClosure::U8(boxed);
return Some(closure);
}
}

impl WriteCallbackCreatorTrait<u16> for WriteCallbackClosure {
fn from_fn<P>(v: P) -> Option<WriteCallbackClosure>
where P: 'static + Sized + Fn(String, u16) -> Result<(), String>
{
let boxed = Box::new(v);
let closure = WriteCallbackClosure::U16(boxed);
return Some(closure);
}
}

fn main() {
let f1 = move |name, state: bool| {
println!("name: {}, state: {}", name, state);
return Ok(());
};

let _v1 = WriteCallbackClosure::from_fn(f1);

let f2 = move |name, state: u8| {
println!("name: {}, state: {}", name, state);
return Ok(());
};

let _v2 = WriteCallbackClosure::from_fn(f2);
}
原始的 create_cb_enum没有为Closure的第二个参数指定特定的类型。

关于rust - 我对泛型函数和枚举有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63565914/

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