tune explanation of PhantomData ownership annotation
[rust-101.git] / src / part16.rs
index fefccdf9d369ea514821065b3ea09539c46dafb2..eef6f0b40d90c12a27f31a027c6498fd8da03d53 100644 (file)
@@ -43,9 +43,9 @@ type NodePtr<T> = *mut Node<T>;
 // will own data of type `T`.
 //@ The type `PhantomData<T>` does not actually store anything in memory - it has size zero. However, logically,
 //@ Rust will consider a `T` to be present. In this case, Rust knows that data of type `T` may be dropped
-//@ whenever a `LinkedList<T>` is dropped. The checks involving destructors are pretty subtle, so it's always
-//@ a good idea to provide such extra information. In safe Rust, this can all be done automatically, but here,
-//@ we just have a `*mut Node<T>`, which Rust does not consider as actually owning the data it points to.
+//@ whenever a `LinkedList<T>` is dropped. Dropping has a lot of subtle checks to it, making sure that things can't go
+//@ wrong. For this to work, Rust needs to know which types could potentially be dropped. In safe Rust, this can all be inferred
+//@ automatically, but here, we just have a `*mut Node<T>`, and we need to tell Rust that we actually own such data.
 pub struct LinkedList<T> {
     first: NodePtr<T>,
     last:  NodePtr<T>,
@@ -166,7 +166,7 @@ impl<'a, T> Iterator for IterMut<'a, T> {
 //@ The linked list we wrote is already working quite nicely, but there is one problem: When the list is dropped,
 //@ nobody bothers to deallocate the remaining nodes. Even worse, if `T` itself has a destructor that needs to
 //@ clean up, it is not called for the element remaining in the list. We need to take care of that ourselves.
-//@ 
+
 //@ In Rust, adding a destructor for a type is done by implementing the `Drop` trait. This is a very special trait.
 //@ It can only be implemented for *nominal types*, i.e., you cannot implement `Drop` for `&mut T`. You also cannot
 //@ restrict the type and lifetime parameters further than the type does - the `Drop` implementation has to apply to *all* instances