3.5.4 PhantomData 与类型
04-PhantomData 与类型 — Comprehensive Rust
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/idiomatic/leveraging-the-type-system/borrow-checker-invariants/phantomdata-01-types.html
3.5.4 PhantomData 与类型
Newtype 模式有时会与 DRY 原则冲突,我们该如何解决?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| // Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0
pub struct UserId(u64);
impl ChatUser for UserId { /* ... */ }
pub struct PatronId(u64);
impl ChatUser for PatronId { /* ... */ }
pub struct ModeratorId(u64);
impl ChatUser for ModeratorId { /* ... */ }
impl ChatModerator for ModeratorId { /* ... */ }
pub struct AdminId(u64);
impl ChatUser for AdminId { /* ... */ }
impl ChatModerator for AdminId { /* ... */ }
impl ChatAdmin for AdminId { /* ... */ }
// 以此类推 ...
fn main() {}
|