我想将 OpenSSL 绑定到我的 Swift 客户端代码。我现在使用的是在模块映射中引用的伞头:
module COpenSSL [system] {
header "copenssl.h"
export *
}
Run Code Online (Sandbox Code Playgroud)
它copenssl.h位于同一个目录中,它只是一系列包含#include <openssl/conf.h>. 有了所有需要的编译器标志,它可以完美地工作:我能够从中导入COpenSSL和使用符号。
但是,我担心代码中的另一个间接级别。我怀疑是否真的有必要。如果我放下伞头,那copenssl.h,然后将包含的内容粘贴到模块映射中,如下所示:
module COpenSSL [system] {
header "openssl/conf.h"
/* ...and more */
export *
}
Run Code Online (Sandbox Code Playgroud)
然后它再也找不到标题,即使所有标志都相同。感觉编译器只搜索模块映射目录,不尊重项目的搜索路径设置。
该文件说:
可以通过绝对路径或相对于当前地图文件的路径来引用该文件。
— http://clang.llvm.org/docs/Modules.html#module-declaration
但我对硬编码路径犹豫不决,因为它阻碍了跨平台性。
我应该只使用伞头,还是有什么方法可以让编译器查看-I路径?
在Swift中,是否可以将某些文字自动转换为其他类型?
let _: Double = 1 // Int literal to Double variable
Run Code Online (Sandbox Code Playgroud)
但是,对于结构,编译器拒绝执行类似的转换:
struct User {
var id: Int
var name: String
}
let _: User = (id: 778, name: "Pete") // error: cannot convert value of type '(id: Int, name: String)' to specified type 'User'
Run Code Online (Sandbox Code Playgroud)
用相应的字段定义一个初始化程序也无济于事。
当从上下文中清除类型时,是否可以忽略结构的显式初始化器?
考虑具有工厂方法的协议:
public protocol Frobnicator {
func frobnicate()
static func makeRightFrobnicator() -> Frobnicator
}
private class SomeFrobnicatorImplementation: Frobnicator { ... }
private class AnotherFrobnicatorImplementation: Frobnicator { ... }
public extension Frobnicator {
static func makeRightFrobnicator() -> Frobnicator {
if something {
return SomeFrobnicatorImplementation()
} else {
return AnotherFrobnicatorImplementation()
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够在不同的时间构建不同的实现者.实现者本身对模块是私有的,而协议是公共的在客户端代码中使用.
当我尝试类似于上面的代码时,我得到"静态成员makeRightFrobnicator不能用于协议元类型Frobnicator.Protocol."
有没有办法,或者我应该使用免费功能?
正如在这个存储库中看到的:
https://github.com/ReactiveX/RxRust/blob/master/src/lib.rs#L110
let gen = move |:| {
let it = range(0is, 20is);
// ~~~ ~~~~
let q = Box::new(Decoupler::new(dtx.clone()));
let mut map1 = Box::new(Map::new(|i : isize| {i * 10}));
let mut map2 = Box::new(Map::new(|i : isize| {i + 2}));
let mut iter = Box::new(IterPublisher::new(it));
map2.subscribe(q);
map1.subscribe(map2);
iter.subscribe(map1);
};
Run Code Online (Sandbox Code Playgroud)
(弯弯曲曲地强调我的)
我想弄清楚数字is后面是什么。这本书仅简要介绍了字面后缀:
请注意,除字节字面量之外的所有数字字面量都允许类型后缀,例如 57u8,以及 _ 作为视觉分隔符,例如 1_000。
— https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-types
并且编译器 (1.53) 只能理解一组特定的后缀,所以我什至无法在我的机器上构建原始的 crate:
invalid suffix `is`
help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, …Run Code Online (Sandbox Code Playgroud)