This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Drop for List { | |
fn drop(&mut self) { | |
let mut cur_link = mem::replace(&mut self.head, Link::Empty); | |
while let Link::More(mut boxed_node) = cur_link { | |
cur_link = mem::replace(&mut boxed_node.next, Link::Empty); | |
// boxed_node 在这里超出作用域并被 drop, | |
// 由于它的 `next` 字段拥有的 `Node` 被设置为 Link::Empty, | |
// 因此这里并不会有无边界的递归发生 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NodeVisitor(object): | |
def visit(self, node: AST) -> float: | |
method_name = 'visit_' + type(node).__name__ | |
visitor = getattr(self, method_name, self.generic_visit) | |
return visitor(node) | |
def generic_visit(self, node: AST) -> None: | |
raise Exception('No visit_{} method'.format(type(node).__name__)) | |