gpt4 book ai didi

rust - 如何链接获取先前结果并满足严格顺序的函数?

转载 作者:行者123 更新时间:2023-12-03 11:27:52 24 4
gpt4 key购买 nike

例子:

let response = add_customer(InputCustomer)
.validate()?
.generate_code()
.create(DB::create(pool_conextion))?;
我尝试使用各种结构,但我不知道这是否是最好的方法:
struct InputCustomer {}

fn add_customer(i: InputCustomer) -> Validate {
Validate {
result: InputCustomer {},
}
}

struct Validate {
result: InputCustomer,
}

impl Validate {
fn do_validate() -> GenCode {
// valdiate struct customer
GenCode {
result: InputCustomer {},
}
}
}

struct GenCode {
result: InputCustomer,
}

impl GenCode {
fn generate_code() -> Create {
// generate customer code
Create { result: true }
}
}

struct Create {
result: bool,
}

最佳答案

您可以使用 phantom type parameters 在单个结构上实现所有功能. Customer struct 包含一些 state :

pub struct Customer<State> {
state: PhantomData<State>,
}
我们可以创建 Customer 的可能状态可以在:
pub struct CustomerStateNew;
pub struct CustomerStateValidated;
pub struct CustomerStateWithCode;
当您创建 Customer ,它的状态是 CustomerStateNew :
pub fn add_customer() -> Customer<CustomerStateNew> {
Customer { state: PhantomData }
}
验证 Customer它必须在 CustomerStateNew 中状态:
impl Customer<CustomerStateNew> {
pub fn validate(&self) -> Customer<CustomerStateValidated> {
Customer { state: PhantomData }
}
}
Customer必须经过验证 ( CustomerStateValidated ) 才能生成代码:
impl Customer<CustomerStateValidated> {
pub fn generate_code(&self) -> Customer<CustomerStateWithCode> {
Customer { state: PhantomData }
}
}
并且它必须有一个生成的代码 ( CustomerStateWithCode) 才能被创建。 create消耗 self ,因此客户在创建后无法使用(您可能不希望这种行为,但为了完整起见,我将其包含在此处):
impl Customer<CustomerStateWithCode> {
pub fn create(self) -> Result<(), ()> {
Ok(())
}
}
现在我们可以将创建用户的方法链接在一起:
let result = add_customer().validate().generate_code().create()?;
但是,我们尝试创建 Customer在验证之前,代码将无法编译:
let result = add_customer().create();

// error[E0599]: no method named `create` found for struct `Customer<CustomerStateNew>`
// --> src/main.rs:36:20
// 36 | add_customer().create();
// | ^^^^^^ method not found in `Customer<CustomerStateNew>`
此外,没有其他人可以创建 Customer具有任意状态,因为 state字段是私有(private)的:
mod somewhere_else {
fn bla() {
let customer: Customer<CustomerStateWithCode> = Customer { state: PhantomData };
customer.create();
}
}

// error[E0451]: field `state` of struct `Customer` is private
// --> src/main.rs:41:64
// |
// 41 | let customer: Customer<CustomerStateWithCode> = Customer { state: PhantomData };
// |
如果要存储特定于每个状态的数据,可以存储实际的 State里面 Customer而不是 PhantomData .然而,现在 state不仅仅是编译时安全,而且会在运行时存储:
pub struct CustomerStateWithCode(pub usize);

pub struct Customer<State> {
state: State,
}

impl Customer<CustomerStateValidated> {
pub fn generate_code(&self) -> Customer<CustomerStateWithCode> {
Customer { state: CustomerStateWithCode(1234) }
}
}
我们使用幻像类型创建了一个简单的状态机。这也称为类型状态模式。请注意,状态将被编译为空,因此没有运行时成本,只有编译时安全!
Playground link

关于rust - 如何链接获取先前结果并满足严格顺序的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65311492/

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