Skip to content

Commit d222857

Browse files
committed
glib: Add optional support for serialization and deserialization with serde
This feature is gated as `serde` Supports both serialization and deserialization: - glib::Bytes - glib::GString (with in-place deserialization) Supports serialization only: - glib::ByteArray - glib::GStr - glib::StrV Collection types are also supported as long as the type parameters implement the necessary traits: - glib::Slice<T: TransparentType + _> - glib::PtrSlice<T: TransparentPtrType + _> - glib::List<T: TransparentPtrType + _> - glib::SList<T: TransparentPtrType + _>
1 parent 4510666 commit d222857

File tree

5 files changed

+631
-1
lines changed

5 files changed

+631
-1
lines changed

.github/workflows/CI.yml

+6
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,15 @@ jobs:
8282
- name: build
8383
run: cargo build --manifest-path ${{ matrix.conf.name }}/Cargo.toml --features "${{ matrix.conf.features }}"
8484
if: matrix.rust != 'nightly'
85+
- name: build-serde
86+
run: cargo build --manifest-path ${{ matrix.conf.name }}/Cargo.toml --features "${{ matrix.conf.features }},serde"
87+
if: matrix.rust != 'nightly' && matrix.conf_name == 'glib'
8588
- name: tests
8689
run: cargo test --manifest-path ${{ matrix.conf.name }}/Cargo.toml --features "${{ matrix.conf.features }}"
8790
if: matrix.rust != 'nightly'
91+
- name: test-serde
92+
run: cargo test --manifest-path ${{ matrix.conf.name }}/Cargo.toml --features "${{ matrix.conf.features }},serde"
93+
if: matrix.rust != 'nightly' && matrix.conf_name == 'glib'
8894
- name: Test ${{ matrix.conf.name }}/sys
8995
run: cargo test
9096
working-directory: ${{ matrix.conf.name }}/sys

glib/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ smallvec = "1.0"
3535
thiserror = "1"
3636
gio_ffi = { package = "gio-sys", path = "../gio/sys", optional = true }
3737
memchr = "2.5.0"
38+
serde = { version = "1.0", optional = true }
3839

3940
[dev-dependencies]
4041
tempfile = "3"
4142
gir-format-check = "^0.1"
4243
trybuild2 = "1"
4344
criterion = "0.4.0"
45+
serde_json = "1.0"
4446

4547
[features]
46-
default = ["gio"]
48+
default = ["gio", "serde"]
4749
v2_58 = ["ffi/v2_58", "gobject_ffi/v2_58"]
4850
v2_60 = ["v2_58", "ffi/v2_60"]
4951
v2_62 = ["v2_60", "ffi/v2_62", "gobject_ffi/v2_62"]
@@ -59,6 +61,7 @@ log_macros = ["log"]
5961
dox = ["ffi/dox", "gobject_ffi/dox", "log_macros"]
6062
compiletests = []
6163
gio = ["gio_ffi"]
64+
serde = ["dep:serde"]
6265

6366
[package.metadata.docs.rs]
6467
features = ["dox"]

glib/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ glib = "0.13"
9696
glib = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "glib" }
9797
```
9898

99+
### Serialization with Serde
100+
101+
If you want to serialize (and deserialize) some GLib types (e.g. `GString`) with Serde, you can enable the `serde` feature:
102+
103+
```toml
104+
glib = { version = "0.18", features = ["serde"] }
105+
```
106+
107+
This library implements serialization and deserialization as described in the `serde@^1.0` API.
108+
99109
## License
100110

101111
__glib__ is available under the MIT License, please refer to it.

glib/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ pub use self::thread_pool::{ThreadHandle, ThreadPool};
213213

214214
pub mod thread_guard;
215215

216+
#[cfg(feature = "serde")]
217+
mod serde;
218+
216219
// rustdoc-stripper-ignore-next
217220
/// This is the log domain used by the [`clone!`][crate::clone!] macro. If you want to use a custom
218221
/// logger (it prints to stdout by default), you can set your own logger using the corresponding

0 commit comments

Comments
 (0)