編輯已恢復(fù)
我們已與您斷開連接。嘗試重連時(shí)會(huì)保存您所做的變更。嘗試重連
標(biāo)題 1
已保存
Bin Song
B
要發(fā)布此內(nèi)容,請(qǐng)選擇鍵盤上的 ?Enter。
發(fā)布
關(guān)閉
Rust技術(shù)空間
…
跨平臺(tái)使用調(diào)研
iOS使用Rust調(diào)研
添加表情符號(hào)
添加標(biāo)題圖像
添加狀態(tài)
一、iOS 項(xiàng)目接入 Rust Library
約定:
iOS 項(xiàng)目目錄 DemoApp
Rust Library:demo-library-ffi
1.1、Rust Library
將 Rust Library 代碼 Clone 到 iOS 項(xiàng)目根目錄下: DemoApp/demo-library-ffi;
修改 DemoApp/rust-library/Cargo.toml,將 Rust Library 修改為靜態(tài)庫
[lib]
crate-type = [“staticlib”]
在 DemoApp 目錄下創(chuàng)建一個(gè)新的頂層 Cargo.toml 文件:
[workspace]
members = [
“demo-library-ffi”
]
現(xiàn)在可以直接使用 cargo build 來生成 rust 靜態(tài)庫了:
DemoApp/target/debug/demo-library-ffi.a
1.2、iOS Project
1.2.1
打開 Xcode target configuration → Build Phases → Link Binary with Libraries 選項(xiàng)卡,選擇 Add files,將前面生成的 DemoApp/target/debug/demo-library-ffi.a 文件添加進(jìn)去;
是的,這個(gè)不是最終我們需要的正確靜態(tài)庫文件,因?yàn)樗挥挟?dāng)前打包機(jī)器的架構(gòu),不要緊,我們會(huì)在下面步驟中修復(fù)它;
1.2.2
用文本編輯器打開 project.pbxproj,搜索并替換 Debug 和 Release 兩處 LIBRARY_SEARCH_PATHS
LIBRARY_SEARCH_PATHS = (
“
(
i
n
h
e
r
i
t
e
d
)
"
,
"
(inherited)", "
(inherited)","(PROJECT_DIR)/target/debug”,
);
Debug 替換為:
“LIBRARY_SEARCH_PATHS[sdk=iphoneos*][arch=arm64]” = “
(
P
R
O
J
E
C
T
D
I
R
)
/
t
a
r
g
e
t
/
a
a
r
c
h
64
?
a
p
p
l
e
?
i
o
s
/
d
e
b
u
g
"
;
"
L
I
B
R
A
R
Y
S
E
A
R
C
H
P
A
T
H
S
[
s
d
k
=
i
p
h
o
n
e
s
i
m
u
l
a
t
o
r
?
]
[
a
r
c
h
=
a
r
m
64
]
"
=
"
(PROJECT_DIR)/target/aarch64-apple-ios/debug"; "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=arm64]" = "
(PROJECTD?IR)/target/aarch64?apple?ios/debug";"LIBRARYS?EARCHP?ATHS[sdk=iphonesimulator?][arch=arm64]"="(PROJECT_DIR)/target/aarch64-apple-ios-sim/debug”;
“LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=x86_64]” = “
(
P
R
O
J
E
C
T
D
I
R
)
/
t
a
r
g
e
t
/
x
8
6
6
4
?
a
p
p
l
e
?
i
o
s
/
d
e
b
u
g
"
;
R
e
l
e
a
s
e
替換為:
"
L
I
B
R
A
R
Y
S
E
A
R
C
H
P
A
T
H
S
[
s
d
k
=
i
p
h
o
n
e
o
s
?
]
[
a
r
c
h
=
a
r
m
64
]
"
=
"
(PROJECT_DIR)/target/x86_64-apple-ios/debug"; Release 替換為: "LIBRARY_SEARCH_PATHS[sdk=iphoneos*][arch=arm64]" = "
(PROJECTD?IR)/target/x866?4?apple?ios/debug";Release替換為:"LIBRARYS?EARCHP?ATHS[sdk=iphoneos?][arch=arm64]"="(PROJECT_DIR)/target/aarch64-apple-ios/release”;
“LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=arm64]” = “
(
P
R
O
J
E
C
T
D
I
R
)
/
t
a
r
g
e
t
/
a
a
r
c
h
64
?
a
p
p
l
e
?
i
o
s
?
s
i
m
/
r
e
l
e
a
s
e
"
;
"
L
I
B
R
A
R
Y
S
E
A
R
C
H
P
A
T
H
S
[
s
d
k
=
i
p
h
o
n
e
s
i
m
u
l
a
t
o
r
?
]
[
a
r
c
h
=
x
8
6
6
4
]
"
=
"
(PROJECT_DIR)/target/aarch64-apple-ios-sim/release"; "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=x86_64]" = "
(PROJECTD?IR)/target/aarch64?apple?ios?sim/release";"LIBRARYS?EARCHP?ATHS[sdk=iphonesimulator?][arch=x866?4]"="(PROJECT_DIR)/target/x86_64-apple-ios/release”;
1.2.3
再次打開 Build Phases 選項(xiàng)卡,添加一個(gè) Script,并移動(dòng)到 Compile Sources 上方,可以命名為: Build Rust library :
bash ${PROJECT_DIR}/bin/compile-rust-lib.sh demo-library-ffi $RUSTBUILDCONF
第二個(gè)參數(shù) RUSTBUILDCONF是傳遞給腳本的環(huán)境變量,我們先定義它們:
打開 Build Settings 選項(xiàng)卡,點(diǎn)擊 + 添加 User-Defined Setting,為其命名 RUSTBUILDCONF 并根據(jù)構(gòu)建變量選擇一個(gè)值:Debug 中填寫 DEBUG,Release 中填寫 RELEASE。
1.2.4
構(gòu)建腳本 bin/compile-rust-lib.sh
#!/usr/bin/env bash
if [ “$#” -ne 2 ]
then
echo “Usage (note: only call inside xcode!):”
echo “compile-rust-lib.sh <FFI_TARGET> ”
exit 1
fi
what to pass to cargo build -p, e.g. rust_lib_ffi
FFI_TARGET=$1
RUSTBUILDCONF from our xcconfigs
RUSTBUILDCONF=$2
RELFLAG=
if [[ “$RUSTBUILDCONF” != “DEBUG” ]]; then
RELFLAG=–release
fi
set -euvx
if [[ -n “${DEVELOPER_SDK_DIR:-}” ]]; then
Assume we’re in Xcode, which means we’re probably cross-compiling.
In this case, we need to add an extra library search path for build scripts and proc-macros,
which run on the host instead of the target.
(macOS Big Sur does not have linkable libraries in /usr/lib/.)
export LIBRARY_PATH=“
D
E
V
E
L
O
P
E
R
S
D
K
D
I
R
/
M
a
c
O
S
X
.
s
d
k
/
u
s
r
/
l
i
b
:
{DEVELOPER_SDK_DIR}/MacOSX.sdk/usr/lib:
DEVELOPERS?DKD?IR/MacOSX.sdk/usr/lib:{LIBRARY_PATH:-}”
fi
IS_SIMULATOR=0
if [ “${LLVM_TARGET_TRIPLE_SUFFIX-}” = “-simulator” ]; then
IS_SIMULATOR=1
fi
for arch in
A
R
C
H
S
;
d
o
c
a
s
e
"
ARCHS; do case "
ARCHS;docase"arch" in
x86_64)
if [ $IS_SIMULATOR -eq 0 ]; then
echo “Building for x86_64, but not a simulator build. What’s going on?” >&2
exit 2
fi
# Intel iOS simulator
export CFLAGS_x86_64_apple_ios="-target x86_64-apple-ios"
$HOME/.cargo/bin/cargo build -p $FFI_TARGET --lib $RELFLAG --target x86_64-apple-ios
;;
arm64)
if [ $IS_SIMULATOR -eq 0 ]; then
# Hardware iOS targets
$HOME/.cargo/bin/cargo build -p $FFI_TARGET --lib $RELFLAG --target aarch64-apple-ios
else
$HOME/.cargo/bin/cargo build -p $FFI_TARGET --lib $RELFLAG --target aarch64-apple-ios-sim
fi
esac
done
二、問題調(diào)研文章來源:http://www.zghlxwxcb.cn/news/detail-621448.html
- Rust訪問網(wǎng)絡(luò)是否需要原生語言的干預(yù)?需支持協(xié)議如:gRPC/RESTful/WebSocket/UDP
Rust 可以直接訪問網(wǎng)絡(luò),并不需要原生語言的干預(yù) - Rust與原生語言的協(xié)作方式,F(xiàn)FI/JNI or 其他?
FFI
cbindgen
rsbind - 如何接收Rust發(fā)起的回調(diào)?
使用函數(shù)指針,可以參考這個(gè)人的代碼:?https://github.com/thombles/dw2019rust/blob/master/modules/07%20-%20Swift%20callbacks.md
或者使用 rsbind - Rust是否可以直接訪問本地(圖片/文件)/沙箱文件?
?https://docs.rs/cacao/latest/cacao/index.html - Rust是否可以直接訪問本地?cái)?shù)據(jù)庫?本地支持哪些常用數(shù)據(jù)庫并且Rust有相應(yīng)的封裝?
可以直接使用 sqlite,如果要用 CoreData,需要通過 FFI - 構(gòu)建時(shí)各平臺(tái)的構(gòu)建步驟以及命令
參考 1.2.4
或者使用 rsbind - Android/iOS端異常調(diào)用棧的分析方法,是否有工具/框架可以配合
- 打包后包大小的變化
1.網(wǎng)絡(luò)
方案1: reqwest, 關(guān)閉bitcode的情況下,包增加7.6MB
2.數(shù)據(jù)庫
方案1:rusqlite, 關(guān)閉bitcode的情況下,包增加3.6MB文章來源地址http://www.zghlxwxcb.cn/news/detail-621448.html
到了這里,關(guān)于iOS使用Rust調(diào)研的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!