Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
RoboPLC
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄新宇
RoboPLC
Commits
84568469
提交
84568469
authored
12月 28, 2024
作者:
Serhij S
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
state functions moved to features
上级
9e65ff31
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
29 行增加
和
12 行删除
+29
-12
Cargo.toml
Cargo.toml
+6
-3
lib.rs
src/lib.rs
+1
-0
state.rs
src/state.rs
+22
-9
没有找到文件。
Cargo.toml
浏览文件 @
84568469
...
@@ -54,8 +54,8 @@ once_cell = { version = "1.19.0", optional = true }
...
@@ -54,8 +54,8 @@ once_cell = { version = "1.19.0", optional = true }
parking_lot
=
{
version
=
"0.12.3"
,
optional
=
true
}
parking_lot
=
{
version
=
"0.12.3"
,
optional
=
true
}
parking_lot_rt
=
{
version
=
"0.12.1"
,
optional
=
true
}
parking_lot_rt
=
{
version
=
"0.12.1"
,
optional
=
true
}
quanta
=
{
version
=
"=0.12.3"
,
optional
=
true
}
quanta
=
{
version
=
"=0.12.3"
,
optional
=
true
}
serde_json
=
"1.0.134"
serde_json
=
{
version
=
"1.0.134"
,
optional
=
true
}
rmp-serde
=
"1.3.0"
rmp-serde
=
{
version
=
"1.3.0"
,
optional
=
true
}
[target.'cfg(windows)'.dependencies]
[target.'cfg(windows)'.dependencies]
parking_lot_rt
=
{
version
=
"0.12.1"
}
parking_lot_rt
=
{
version
=
"0.12.1"
}
...
@@ -69,12 +69,15 @@ modbus = ["rmodbus"]
...
@@ -69,12 +69,15 @@ modbus = ["rmodbus"]
openssl-vendored
=
[
"busrt/openssl-vendored"
,
"eva-common/openssl-vendored"
]
openssl-vendored
=
[
"busrt/openssl-vendored"
,
"eva-common/openssl-vendored"
]
metrics
=
[
"dep:metrics"
,
"metrics-exporter-prometheus"
,
"metrics-exporter-scope"
,
"tokio"
,
"quanta"
]
metrics
=
[
"dep:metrics"
,
"metrics-exporter-prometheus"
,
"metrics-exporter-scope"
,
"tokio"
,
"quanta"
]
async
=
["dep:parking_lot_rt"]
async
=
["dep:parking_lot_rt"]
full
=
[
"eapi"
,
"modbus"
,
"metrics"
,
"pipe"
,
"rvideo"
,
"rflow"
,
"async"
]
full
=
[
"eapi"
,
"modbus"
,
"metrics"
,
"pipe"
,
"rvideo"
,
"rflow"
,
"async"
,
"json"
,
"msgpack"
]
locking-default
=
[
"dep:parking_lot"
,
"rtsc/parking_lot"
,
"rvideo?/locking-default"
,
"rflow?/locking-default"
]
locking-default
=
[
"dep:parking_lot"
,
"rtsc/parking_lot"
,
"rvideo?/locking-default"
,
"rflow?/locking-default"
]
locking-rt
=
[
"dep:parking_lot_rt"
,
"rvideo?/locking-rt"
,
"rflow?/locking-rt"
]
locking-rt
=
[
"dep:parking_lot_rt"
,
"rvideo?/locking-rt"
,
"rflow?/locking-rt"
]
locking-rt-safe
=
[
"rvideo?/locking-rt-safe"
,
"rflow?/locking-rt-safe"
]
locking-rt-safe
=
[
"rvideo?/locking-rt-safe"
,
"rflow?/locking-rt-safe"
]
json
=
["serde_json"]
msgpack
=
["rmp-serde"]
default
=
["locking-default"]
default
=
["locking-default"]
[dev-dependencies]
[dev-dependencies]
...
...
src/lib.rs
浏览文件 @
84568469
...
@@ -108,6 +108,7 @@ pub mod supervisor;
...
@@ -108,6 +108,7 @@ pub mod supervisor;
pub
mod
thread_rt
;
pub
mod
thread_rt
;
/// State helper functions
/// State helper functions
#[cfg(any(feature
=
"json"
,
feature
=
"msgpack"
))]
pub
mod
state
;
pub
mod
state
;
/// The crate result type
/// The crate result type
...
...
src/state.rs
浏览文件 @
84568469
...
@@ -5,42 +5,55 @@ use serde::{de::DeserializeOwned, Serialize};
...
@@ -5,42 +5,55 @@ use serde::{de::DeserializeOwned, Serialize};
use
crate
::{
Error
,
Result
};
use
crate
::{
Error
,
Result
};
enum
Format
{
enum
Format
{
#[cfg(feature
=
"json"
)]
Json
,
Json
,
#[cfg(feature
=
"msgpack"
)]
Msgpack
,
Msgpack
,
}
}
impl
Format
{
impl
Format
{
fn
from_path
<
P
:
AsRef
<
Path
>>
(
path
:
P
)
->
Self
{
fn
from_path
<
P
:
AsRef
<
Path
>>
(
path
:
P
)
->
Result
<
Self
>
{
match
path
match
path
.as_ref
()
.as_ref
()
.extension
()
.extension
()
.map_or
(
""
,
|
ext
|
ext
.to_str
()
.unwrap
())
.map_or
(
""
,
|
ext
|
ext
.to_str
()
.unwrap
())
{
{
"json"
=>
Self
::
Json
,
#[cfg(feature
=
"json"
)]
_
=>
Self
::
Msgpack
,
"json"
=>
Ok
(
Self
::
Json
),
#[cfg(not(feature
=
"json"
))]
"json"
=>
Err
(
Error
::
Unimplemented
),
#[cfg(feature
=
"msgpack"
)]
_
=>
Ok
(
Self
::
Msgpack
),
#[cfg(not(feature
=
"msgpack"
))]
_
=>
Err
(
Error
::
Unimplemented
),
}
}
}
}
}
}
/// Load the state from a file. If "json" extension is specified, the state is loaded in JSON
/// Load the state from a file. If "json" extension is specified, the state is loaded from JSON
/// format. All errors, including missing state file, must be handled by the caller.
/// format (requires crate 'json' feature), otherwise from MessagePack (requires crate 'msgpack'
/// feature). All errors, including missing state file, must be handled by the caller.
pub
fn
load
<
S
:
DeserializeOwned
,
P
:
AsRef
<
Path
>>
(
path
:
P
)
->
Result
<
S
>
{
pub
fn
load
<
S
:
DeserializeOwned
,
P
:
AsRef
<
Path
>>
(
path
:
P
)
->
Result
<
S
>
{
let
format
=
Format
::
from_path
(
&
path
);
let
format
=
Format
::
from_path
(
&
path
)
?
;
let
file
=
File
::
open
(
&
path
)
?
;
let
file
=
File
::
open
(
&
path
)
?
;
let
data
=
match
format
{
let
data
=
match
format
{
#[cfg(feature
=
"json"
)]
Format
::
Json
=>
serde_json
::
from_reader
(
file
)
.map_err
(
Error
::
failed
)
?
,
Format
::
Json
=>
serde_json
::
from_reader
(
file
)
.map_err
(
Error
::
failed
)
?
,
#[cfg(feature
=
"msgpack"
)]
Format
::
Msgpack
=>
rmp_serde
::
from_read
(
file
)
.map_err
(
Error
::
failed
)
?
,
Format
::
Msgpack
=>
rmp_serde
::
from_read
(
file
)
.map_err
(
Error
::
failed
)
?
,
};
};
Ok
(
data
)
Ok
(
data
)
}
}
/// Save the state to a file. If "json" extension is specified, the state is saved in JSON
/// Save the state to a file. If "json" extension is specified, the state is saved in JSON
format
///
format. Otherwise it is saved in MessagePack format
.
///
(requires crate 'json' feature), otherwise in MessagePack (requires crate 'msgpack' feature)
.
pub
fn
save
<
S
:
Serialize
,
P
:
AsRef
<
Path
>>
(
path
:
P
,
state
:
&
S
)
->
Result
<
()
>
{
pub
fn
save
<
S
:
Serialize
,
P
:
AsRef
<
Path
>>
(
path
:
P
,
state
:
&
S
)
->
Result
<
()
>
{
let
format
=
Format
::
from_path
(
&
path
);
let
format
=
Format
::
from_path
(
&
path
)
?
;
let
mut
file
=
File
::
create
(
&
path
)
?
;
let
mut
file
=
File
::
create
(
&
path
)
?
;
let
data
=
match
format
{
let
data
=
match
format
{
#[cfg(feature
=
"json"
)]
Format
::
Json
=>
serde_json
::
to_vec
(
state
)
.map_err
(
Error
::
failed
)
?
,
Format
::
Json
=>
serde_json
::
to_vec
(
state
)
.map_err
(
Error
::
failed
)
?
,
#[cfg(feature
=
"msgpack"
)]
Format
::
Msgpack
=>
rmp_serde
::
to_vec_named
(
state
)
.map_err
(
Error
::
failed
)
?
,
Format
::
Msgpack
=>
rmp_serde
::
to_vec_named
(
state
)
.map_err
(
Error
::
failed
)
?
,
};
};
file
.write_all
(
&
data
)
?
;
file
.write_all
(
&
data
)
?
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论