Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate vec-final.md #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
* [RawVec](vec-raw.md)
* [Drain](vec-drain.md)
* [Handling Zero-Sized Types](vec-zsts.md)
* [Final Code](vec-final.md)
* [最終コード](vec-final.md)
* [Implementing Arc and Mutex](arc-and-mutex.md)
32 changes: 19 additions & 13 deletions src/vec-final.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<!--
# The Final Code
-->

# 最終コード

```rust
#![feature(unique)]
Expand All @@ -21,10 +25,11 @@ struct RawVec<T> {
impl<T> RawVec<T> {
fn new() -> Self {
unsafe {
// !0 is usize::MAX. This branch should be stripped at compile time.
// !0 usize::MAX です。この分岐はコンパイル時に取り除かれるはずです。
let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };

// heap::EMPTY doubles as "unallocated" and "zero-sized allocation"
// heap::EMPTY は "アロケートされていない" と "サイズが 0 の型のアロケーション" の
// 2 つの意味を兼ねることになります。
RawVec { ptr: Unique::new(heap::EMPTY as *mut T), cap: cap }
}
}
Expand All @@ -33,8 +38,9 @@ impl<T> RawVec<T> {
unsafe {
let elem_size = mem::size_of::<T>();

// since we set the capacity to usize::MAX when elem_size is
// 0, getting to here necessarily means the Vec is overfull.
// elem_size が 0 の時にキャパシティを usize::MAX にしたので、
// ここにたどり着いてしまうということは、 Vec が満杯であることを必然的に
// 意味します。
assert!(elem_size != 0, "capacity overflow");

let align = mem::align_of::<T>();
Expand All @@ -51,7 +57,7 @@ impl<T> RawVec<T> {
(new_cap, ptr)
};

// If allocate or reallocate fail, we'll get `null` back
// もしアロケートや、リアロケートに失敗すると、 `null` が返ってきます
if ptr.is_null() { oom() }

self.ptr = Unique::new(ptr as *mut _);
Expand Down Expand Up @@ -98,7 +104,7 @@ impl<T> Vec<T> {
ptr::write(self.ptr().offset(self.len as isize), elem);
}

// Can't fail, we'll OOM first.
// 絶対成功します。 OOM はこの前に起きるからです。
self.len += 1;
}

Expand Down Expand Up @@ -157,9 +163,9 @@ impl<T> Vec<T> {
unsafe {
let iter = RawValIter::new(&self);

// this is a mem::forget safety thing. If Drain is forgotten, we just
// leak the whole Vec's contents. Also we need to do this *eventually*
// anyway, so why not do it now?
// これは mem::forget の安全版です。もし Drain が forget されたら、
// 単に Vec の内容全体をリークします。そして*結局*これをしなければ
// なりません。なら今やっちゃいましょう。
self.len = 0;

Drain {
Expand All @@ -173,7 +179,7 @@ impl<T> Vec<T> {
impl<T> Drop for Vec<T> {
fn drop(&mut self) {
while let Some(_) = self.pop() {}
// allocation is handled by RawVec
// デアロケートは RawVec が対処します
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this is a just typo.

}
}

Expand Down Expand Up @@ -265,7 +271,7 @@ impl<T> DoubleEndedIterator for RawValIter<T> {


pub struct IntoIter<T> {
_buf: RawVec<T>, // we don't actually care about this. Just need it to live.
_buf: RawVec<T>, // これを扱うことはないのですが、その存在は必要です。
iter: RawValIter<T>,
}

Expand Down Expand Up @@ -310,9 +316,9 @@ impl<'a, T> Drop for Drain<'a, T> {
}
}

/// Abort the process, we're out of memory!
/// プロセスをアボートします。メモリ不足だ!
///
/// In practice this is probably dead code on most OSes
/// 実際にはこのコードは、多分ほとんどの OS においてデッドコードとなるでしょう
fn oom() {
::std::process::exit(-9999);
}
Expand Down