Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# リファレンス

- [std::any](./any/about.md)
- [TypeId](./any/TypeId.md)
- [std::vec](./vec/about.md)
- [Drain](./vec/Drain.md)

240 changes: 240 additions & 0 deletions src/any/TypeId.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
# 構造体 `std::any::TypeId`

```rust,ignore
pub struct TypeId {
// private fields
}
```
## バージョン

Rust 1.0.0 ~

## 解説

型に対するグローバルに一意な識別子を提供する構造体。
不透明なオブジェクトであるため内部を直接見ることはできないが、DebugやCloneといった基本的なトレイトは実装してある。

現在、`TypeId`は`'static`制約を満たすもののみ利用できるが、これは将来解除される可能性がある。

また、`Hash`や`Ord`、`PartialOrd`を実装しているが、ハッシュ値や順序はRustのリリースごとに違うため、これらに依存した設計は避けるべきである。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

意図が入っていますが,こちらの方がいいと思われます.


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Danger of Improper VarianceとExamplesが省略されています.
これは閲覧者が公式には追加の記述があることを一時的にでも書いておくことが親切かと思われます.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

見逃していました。追加します

## 実装

```rust,ignore
impl TypeId {
pub const fn of<T>() -> TypeId
where
T: 'static + ?Sized;
}
```

### `of`

ジェネリクスに渡された型のTypeIdを返す関数。

```rust
use std::any::TypeId;

fn is_i32<T: 'static + ?Sized>(val: T) -> bool {
TypeId::of::<T>() == TypeId::of::<i32>()
}

assert!(!is_i32(1.2));
```

## トレイト実装

### `Clone`

```rust,ignore
impl Clone for TypeId {
/* trait methods */
}
```

### `std::fmt::Debug`

```rust,ignore
impl std::fmt::Debug for TypeId {
/* trait methods */
}
```

### `std::hash::Hash`

```rust,ignore
impl std::hash::Hash for TypeId {
/* trait methods */
}
```

### `Ord`

```rust,ignore
impl Ord for TypeId {
/* trait methods */
}
```

### `PartialEq`

```rust,ignore
impl PartialEq for TypeId {
/* trait methods */
}
```

### `PartialOrd`

```rust,ignore
impl PartialOrd for TypeId {
/* trait methods */
}
```

### `Copy`

```rust,ignore
impl Copy for TypeId {}
```

### `Eq`

```rust,ignore
impl Eq for TypeId {}
```

### `Send`

```rust,ignore
impl Send for TypeId {}
```

### `Sync`

```rust,ignore
impl Sync for TypeId {}
```

### `std::marker::Freeze`

```rust,ignore
impl std::marker::Freeze for TypeId {}
```

### `std::panic::RefUnwindSafe`

```rust,ignore
impl std::panic::RefUnwindSafe for TypeId {}
```

### `Unpin`

```rust,ignore
impl Unpin for TypeId {}
```

### `std::panic::UnwindSafe`

```rust,ignore
impl std::panic::UnwindSafe for TypeId {}
```

---

### `std::any::Any`

```rust,ignore
impl<T> std::any::Any for T
where
T: 'static + ?Sized,
{
/* trait methods */
}
```

### `std::borrow::Borrow`

```rust,ignore
impl<T> std::borrow::Borrow<T> for T
where
T: ?Sized,
{
/* trait methods */
}
```

### `std::borrow::BorrowMut`

```rust,ignore
impl<T> std::borrow::BorrowMut<T> for T
where
T: ?Sized,
{
/* trait methods */
}
```

### `std::clone::CloneToUninit`

```rust,ignore
impl<T> std::clone::CloneToUninit for T
where
T: Clone,
{
/* trait methods */
}
```

### `From`

```rust,ignore
impl<T> From<T> for T {
/* trait methods */
}
```

### `Into`

```rust,ignore
impl<T, U> Into<U> for T
where
U: From<T>,
{
/* trait methods */
}
```

### `std::borrow::ToOwned`

```rust,ignore
impl<T> std::borrow::ToOwned for T
where
T: Clone,
{
/* trait methods */
}
```

### `TryFrom`

```rust,ignore
impl<T, U> TryFrom<U> for T
where
U: Into<T>,
{
/* trait methods */
}
```

### `TryInto`

```rust,ignore
impl<T, U> TryInto<U> for T
where
U: TryFrom<T>,
{
/* trait methods */
}
```

2 changes: 1 addition & 1 deletion src/any/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Rust 1.0.0 ~

| 名前 | 説明 |
| --- | --- |
| `TypeId` | グローバルに一意な型の識別子を提供する |
| [`TypeId`](./TypeId.md) | グローバルに一意な型の識別子を提供する |

## トレイト

Expand Down