Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
RoboPLC
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄新宇
RoboPLC
Commits
08fb1e81
提交
08fb1e81
authored
12月 28, 2024
作者:
Serhij S
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
state helpers
上级
1ccf3f34
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
53 行增加
和
0 行删除
+53
-0
Cargo.toml
Cargo.toml
+2
-0
lib.rs
src/lib.rs
+3
-0
state.rs
src/state.rs
+48
-0
没有找到文件。
Cargo.toml
浏览文件 @
08fb1e81
...
@@ -54,6 +54,8 @@ once_cell = { version = "1.19.0", optional = true }
...
@@ -54,6 +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"
rmp-serde
=
"1.3.0"
[target.'cfg(windows)'.dependencies]
[target.'cfg(windows)'.dependencies]
parking_lot_rt
=
{
version
=
"0.12.1"
}
parking_lot_rt
=
{
version
=
"0.12.1"
}
...
...
src/lib.rs
浏览文件 @
08fb1e81
...
@@ -107,6 +107,9 @@ pub mod supervisor;
...
@@ -107,6 +107,9 @@ pub mod supervisor;
/// Real-time thread functions to work with [`supervisor::Supervisor`] and standalone, Linux only
/// Real-time thread functions to work with [`supervisor::Supervisor`] and standalone, Linux only
pub
mod
thread_rt
;
pub
mod
thread_rt
;
/// State helper functions
pub
mod
state
;
/// The crate result type
/// The crate result type
pub
type
Result
<
T
>
=
std
::
result
::
Result
<
T
,
Error
>
;
pub
type
Result
<
T
>
=
std
::
result
::
Result
<
T
,
Error
>
;
...
...
src/state.rs
0 → 100644
浏览文件 @
08fb1e81
use
std
::{
fs
::
File
,
io
::
Write
,
path
::
Path
};
use
serde
::{
de
::
DeserializeOwned
,
Serialize
};
use
crate
::{
Error
,
Result
};
enum
Format
{
Json
,
Msgpack
,
}
impl
Format
{
fn
from_path
<
P
:
AsRef
<
Path
>>
(
path
:
P
)
->
Self
{
match
path
.as_ref
()
.extension
()
.map_or
(
""
,
|
ext
|
ext
.to_str
()
.unwrap
())
{
"json"
=>
Self
::
Json
,
_
=>
Self
::
Msgpack
,
}
}
}
/// Load the state from a file. If "json" extension is specified, the state is loaded in JSON
/// format. All errors, including missing state file, must be handled by the caller.
pub
fn
load
<
S
:
DeserializeOwned
,
P
:
AsRef
<
Path
>>
(
path
:
P
)
->
Result
<
S
>
{
let
format
=
Format
::
from_path
(
&
path
);
let
file
=
File
::
open
(
&
path
)
?
;
let
data
=
match
format
{
Format
::
Json
=>
serde_json
::
from_reader
(
file
)
.map_err
(
Error
::
failed
)
?
,
Format
::
Msgpack
=>
rmp_serde
::
from_read
(
file
)
.map_err
(
Error
::
failed
)
?
,
};
Ok
(
data
)
}
/// Save the state to a file. If "json" extension is specified, the state is saved in JSON
/// format. Otherwise it is saved in MessagePack format.
pub
fn
save
<
S
:
Serialize
,
P
:
AsRef
<
Path
>>
(
state
:
&
S
,
path
:
P
)
->
Result
<
()
>
{
let
format
=
Format
::
from_path
(
&
path
);
let
mut
file
=
File
::
create
(
&
path
)
?
;
let
data
=
match
format
{
Format
::
Json
=>
serde_json
::
to_vec
(
state
)
.map_err
(
Error
::
failed
)
?
,
Format
::
Msgpack
=>
rmp_serde
::
to_vec_named
(
state
)
.map_err
(
Error
::
failed
)
?
,
};
file
.write_all
(
&
data
)
?
;
Ok
(())
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论