示例:
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,
}
您可以使用幻影类型参数在单个结构上实现所有函数。Customer
结构包含一些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
字段是私有的:
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) }
}
}
我们使用幻影类型创建了一个简单的状态机。这也被称为类型的状态模式。请注意,状态将被编译为零,因此没有运行时成本,只有编译时安全!
游乐场链接