Skip to content

Commit

Permalink
重构
Browse files Browse the repository at this point in the history
  • Loading branch information
mokurin000 committed Jan 11, 2022
1 parent cdcf42b commit 4f58345
Show file tree
Hide file tree
Showing 25 changed files with 38 additions and 111 deletions.
29 changes: 14 additions & 15 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,30 @@
- [确定性析构](./idioms/dtor-finally.md)
- [`mem::{take(_), replace(_)}`](./idioms/mem-replace.md)
- [栈上动态分发](./idioms/on-stack-dyn-dispatch.md)
- [外部语言接口](./idioms/ffi-intro.md)
- [常见错误处理](./idioms/ffi-errors.md)
- [接受字符串](./idioms/ffi-accepting-strings.md)
- [传递字符串](./idioms/ffi-passing-strings.md)
- [外部语言接口](idioms/ffi/ffi-intro.md)
- [常见错误处理](idioms/ffi/ffi-errors.md)
- [接受字符串](idioms/ffi/ffi-accepting-strings.md)
- [传递字符串](idioms/ffi/ffi-passing-strings.md)
- [关于 `Option`的迭代器](./idioms/option-iter.md)
- [向闭包传递变量](./idioms/pass-var-to-closure.md)
- [留隐私,为扩展](./idioms/priv-extend.md)
- [关于初始化的文档](./idioms/rustdoc-init.md)
- [临时可变性](./idioms/temporary-mutability.md)

- [设计模式](./patterns/index.md)
- [生成器](./patterns/builder.md)
- [分解结构体](./patterns/compose-structs.md)
- [Entry API](./patterns/entry.md)
- [生成器](patterns/creational/builder.md)
- [分解结构体](patterns/Structural/compose-structs.md)
- [外部语言接口使用](./patterns/ffi-intro.md)
- [基于对象的API](./patterns/ffi-export.md)
- [类型合并封装](./patterns/ffi-wrappers.md)
- [Fold](./patterns/fold.md)
- [解释器](./patterns/interpreter.md)
- [新类型](./patterns/newtype.md)
- [RAII 守卫](./patterns/RAII.md)
- [偏爱更小的库](./patterns/small-crates.md)
- [策略模式](./patterns/strategy.md)
- [将不安全置于小模块中](./patterns/unsafe-mods.md)
- [访问者模式](./patterns/visitor.md)
- [Fold](patterns/creational/fold.md)
- [解释器](patterns/behavioural/interpreter.md)
- [新类型](patterns/behavioural/newtype.md)
- [RAII 守卫](patterns/behavioural/RAII.md)
- [偏爱更小的库](patterns/Structural/small-crates.md)
- [策略模式](patterns/behavioural/strategy.md)
- [将不安全置于小模块中](patterns/Structural/unsafe-mods.md)
- [访问者模式](patterns/behavioural/visitor.md)

- [反模式](./anti_patterns/index.md)
- [clone过借用检查](./anti_patterns/borrow_clone.md)
Expand Down
2 changes: 1 addition & 1 deletion idioms/ctor.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ impl<T> Vec<T> {

## 参阅

[生成器模式](../patterns/builder.md)用于有多种构造对象方式的情况。
[生成器模式](../patterns/creational/builder.md)用于有多种构造对象方式的情况。
2 changes: 1 addition & 1 deletion idioms/dtor-finally.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ Rust对每个栈帧中的每个对象执行析构器代码。所以析构器即

## 参阅

[RAII](../patterns/RAII.md).
[RAII](../patterns/behavioural/RAII.md).
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions idioms/ffi-intro.md → idioms/ffi/ffi-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

这一章中包括下列能在做FFI时有用的习惯用法:

1. [常见错误处理](./ffi-errors.md) - 使用整型代表错误类型以及哨兵返回值(sentinel)。
2. [接受字符串](./ffi-accepting-strings.md) 同时使用最少的unsafe代码。
3. [传递字符串](./ffi-passing-strings.md) 给FFI函数。
1. [常见错误处理](ffi-errors.md) - 使用整型代表错误类型以及哨兵返回值(sentinel)。
2. [接受字符串](ffi-accepting-strings.md) 同时使用最少的unsafe代码。
3. [传递字符串](ffi-passing-strings.md) 给FFI函数。
File renamed without changes.
2 changes: 1 addition & 1 deletion idioms/on-stack-dyn-dispatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ let readable: Box<dyn io::Read> = if arg == "-" {
## 参阅

* [Finalisation in destructors](dtor-finally.md) and
[RAII guards](../patterns/RAII.md) can benefit from tight control over lifetimes.
[RAII guards](../patterns/behavioural/RAII.md) can benefit from tight control over lifetimes.
* For conditionally filled `Option<&T>`s of (mutable) references, one can
initialize an `Option<T>` directly and use its [`.as_ref()`] method to get an
optional reference.
Expand Down
6 changes: 3 additions & 3 deletions patterns/ffi-export.md → patterns/FFI/ffi-export.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ datum dbm_nextkey(DBM *);

其次,根据API各部分之间的关系,可能会涉及大量的设计工作。许多更简单的设计点都有与之相关的设计模式:

- [类型合并封装](./ffi-wrappers.md) 打包多个Rust类型为一个不透明的对象
- [类型合并封装](ffi-wrappers.md) 打包多个Rust类型为一个不透明的对象

- [常见错误处理](../idioms/ffi-errors.md) 讲述使用整型作为错误代码和返回值的哨兵(就像`NULL`指针一样)
- [常见错误处理](../../idioms/ffi/ffi-errors.md) 讲述使用整型作为错误代码和返回值的哨兵(就像`NULL`指针一样)

- [接受字符串](../idioms/ffi-accepting-strings.md) 代码的情况下接受字符串,并且更容易成功[传递字符串](../idioms/ffi-passing-strings.md)
- [接受字符串](../../idioms/ffi/ffi-accepting-strings.md) 代码的情况下接受字符串,并且更容易成功[传递字符串](../../idioms/ffi/ffi-passing-strings.md)

不过,也不是所有API都可以这样设计。具体情况具体分析。
4 changes: 2 additions & 2 deletions patterns/ffi-intro.md → patterns/FFI/ffi-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

这一章中包括下列能在做FFI时有用的设计模式:

1. [基于对象API](./ffi-export.md) 设计模式有内存安全的特性,同时有一个安全和非安全代码的清晰边界。
2. [类型合并到包装器](./ffi-wrappers.md)——将多个Rust类型合并到一个不透明的“对象”中。
1. [基于对象API](ffi-export.md) 设计模式有内存安全的特性,同时有一个安全和非安全代码的清晰边界。
2. [类型合并到包装器](ffi-wrappers.md)——将多个Rust类型合并到一个不透明的“对象”中。
2 changes: 1 addition & 1 deletion patterns/ffi-wrappers.md → patterns/FFI/ffi-wrappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl MySetWrapper {

## 优点

这使得API使用起来更安全,避免了在类型间交互时的生命周期问题。关于更多的优点和避免的陷阱请看 [基于对象的API](./ffi-export.md)
这使得API使用起来更安全,避免了在类型间交互时的生命周期问题。关于更多的优点和避免的陷阱请看 [基于对象的API](ffi-export.md)

## 缺点

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion patterns/RAII.md → patterns/behavioural/RAII.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn deref<'a>(&'a self) -> &'a T {

## 参阅

[Finalisation in destructors idiom](../idioms/dtor-finally.md)
[Finalisation in destructors idiom](../../idioms/dtor-finally.md)

RAII is a common pattern in C++: [cppreference.com](http://en.cppreference.com/w/cpp/language/raii),
[wikipedia][wikipedia].
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion patterns/visitor.md → patterns/behavioural/visitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ pub fn walk_expr(visitor: &mut Visitor, e: &Expr) {

[访问者模式](https://en.wikipedia.org/wiki/Visitor_pattern)

[fold](fold.md)模式与访问者模式很相似,区别在于生成了被访问数据结构的新版本。
[fold](../creational/fold.md)模式与访问者模式很相似,区别在于生成了被访问数据结构的新版本。
2 changes: 1 addition & 1 deletion patterns/builder.md → patterns/creational/builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ let f = fb.build();
- [Description in the style guide](https://web.archive.org/web/20210104103100/https://doc.rust-lang.org/1.12.0/style/ownership/builders.html)
- [derive_builder](https://crates.io/crates/derive_builder), a crate for automatically
implementing this pattern while avoiding the boilerplate.
- [Constructor pattern](../idioms/ctor.md) for when construction is simpler.
- [Constructor pattern](../../idioms/ctor.md) for when construction is simpler.
- [Builder pattern (wikipedia)](https://en.wikipedia.org/wiki/Builder_pattern)
- [Construction of complex values](https://web.archive.org/web/20210104103000/https://rust-lang.github.io/api-guidelines/type-safety.html#c-builder)
2 changes: 1 addition & 1 deletion patterns/fold.md → patterns/creational/fold.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ folder也可以定义为将一个数据结构映射到不同(但基本相似

在其他语言中,更常见的是Rust迭代器中的fold形式而不是这里说的fold模式。一些函数式语言中有对数据结构进行复杂转换的支持。

[访问者模式](visitor.md)和fold高度相关。 它们共享遍历数据结构的概念——在每个节点上执行操作。不过访问者模式不创建新的数据结构也不消耗原来的数据。
[访问者模式](../behavioural/visitor.md)和fold高度相关。 它们共享遍历数据结构的概念——在每个节点上执行操作。不过访问者模式不创建新的数据结构也不消耗原来的数据。
34 changes: 0 additions & 34 deletions patterns/entry.md

This file was deleted.

35 changes: 0 additions & 35 deletions patterns/late-bounds.md

This file was deleted.

19 changes: 8 additions & 11 deletions refactoring/index.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
# Refactoring
# 重构

Refactoring is very important in relation to these topics.
Just as important as the other topics covered here, is how to take good code and
turn it into great code.
重构对于这些主题来说是非常重要的。
与这里涉及的其他主题同样重要的是,如何将好的代码变成伟大的代码。

We can use [design patterns](../patterns/index.md) to [DRY] up code and generalize
abstractions. We must avoid [anti-patterns](../anti_patterns/index.md) while we
do this. While they may be tempting to employ, their costs outweigh their benefits.
我们可以用[设计模式](../patterns/index.md)[DRY]代码,以及泛化抽象。同时我们还必须避免[反模式](../anti_patterns/index.md)
While they may be tempting to employ, their costs outweigh their benefits.

> Shortcuts make for long days.
We can also use [idioms](../idioms/index.md) to structure our code in a way that
is understandable.
我们也可以用[典型用例](../idioms/index.md)组织我们的代码,让它更好理解。

## Tests
## 测试

Tests are of vital importance during refactoring.
测试在重构中极其重要。

## Small changes

Expand Down

0 comments on commit 4f58345

Please sign in to comment.