Skip to content

Commit 515e753

Browse files
committed
Remove reference-counted pointer from DataTree
This commit reverts 945368c. That commit intended to simplify lifetime management by changing `DataTree` to contain a reference-counting pointer instead of a regular reference to the associated YANG context. In practice, however, that change caused more problems than it solved. The major issue was the inconsistency where all other structs were using regular references instead of reference-counting pointers. This inconsistency was problematic for expanding the API surface to cover features like YANG extensions, where data trees can be created from schema structs. Having explicit control over the data trees lifetimes is also desired in many cases. With this change, `DataNodeRef` now has two lifetime parameters: one for the associated data tree and another for the associated YANG context. These two lifetimes are necessary to express the logic of the `DataNodeRef::duplicate` method, in which a new data tree is created with a new lifetime while maintaining the same lifetime for the YANG context. Signed-off-by: Renato Westphal <[email protected]>
1 parent f40af12 commit 515e753

File tree

9 files changed

+122
-141
lines changed

9 files changed

+122
-141
lines changed

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ By default, yang-rs uses pre-generated FFI bindings and uses dynamic linking to
4848
A basic example that parses and validates JSON instance data, and then converts
4949
it to the XML format:
5050
```rust,no_run
51-
use std::sync::Arc;
5251
use std::fs::File;
5352
use yang3::context::{Context, ContextFlags};
5453
use yang3::data::{
@@ -70,7 +69,6 @@ fn main() -> std::io::Result<()> {
7069
ctx.load_module(module_name, None, &[])
7170
.expect("Failed to load module");
7271
}
73-
let ctx = Arc::new(ctx);
7472
7573
// Parse and validate data tree in the JSON format.
7674
let dtree = DataTree::parse_file(

benches/data.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use std::sync::Arc;
2-
31
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
42
use yang3::context::{Context, ContextFlags};
53
use yang3::data::{Data, DataDiffFlags, DataTree, DataValidationFlags};
64

75
static SEARCH_DIR: &str = "./assets/yang/";
86

9-
fn data_generate(ctx: &Arc<Context>, interfaces: u32) -> DataTree {
7+
fn data_generate(ctx: &Context, interfaces: u32) -> DataTree {
108
let mut dtree = DataTree::new(ctx);
119

1210
for i in 1..=interfaces {
@@ -38,18 +36,14 @@ fn criterion_benchmark(c: &mut Criterion) {
3836
];
3937

4038
// Initialize context.
41-
let mut ctx = Arc::new(
42-
Context::new(ContextFlags::NO_YANGLIBRARY)
43-
.expect("Failed to create context"),
44-
);
45-
(*Arc::get_mut(&mut ctx).unwrap())
46-
.set_searchdir(SEARCH_DIR)
39+
let mut ctx = Context::new(ContextFlags::NO_YANGLIBRARY)
40+
.expect("Failed to create context");
41+
ctx.set_searchdir(SEARCH_DIR)
4742
.expect("Failed to set YANG search directory");
4843

4944
// Load YANG modules.
5045
for module_name in &["ietf-interfaces", "iana-if-type"] {
51-
(*Arc::get_mut(&mut ctx).unwrap())
52-
.load_module(module_name, None, &[])
46+
ctx.load_module(module_name, None, &[])
5347
.expect("Failed to load module");
5448
}
5549

examples/data_diff.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::sync::Arc;
21
use yang3::context::{Context, ContextFlags};
32
use yang3::data::{
43
Data, DataDiffFlags, DataFormat, DataParserFlags, DataPrinterFlags,
@@ -61,7 +60,6 @@ fn main() -> std::io::Result<()> {
6160
ctx.load_module(module_name, None, &[])
6261
.expect("Failed to load module");
6362
}
64-
let ctx = Arc::new(ctx);
6563

6664
// Parse data trees from JSON strings.
6765
let dtree1 = DataTree::parse_string(

examples/data_edit.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::fs::File;
2-
use std::sync::Arc;
32
use yang3::context::{Context, ContextFlags};
43
use yang3::data::{
54
Data, DataFormat, DataParserFlags, DataPrinterFlags, DataTree,
@@ -25,7 +24,6 @@ fn main() -> std::io::Result<()> {
2524
ctx.load_module(module_name, None, &[])
2625
.expect("Failed to load module");
2726
}
28-
let ctx = Arc::new(ctx);
2927

3028
// Parse data tree from JSON file.
3129
let mut dtree = DataTree::parse_file(

examples/data_iteration.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::fs::File;
2-
use std::sync::Arc;
32
use yang3::context::{Context, ContextFlags};
43
use yang3::data::{
54
Data, DataFormat, DataParserFlags, DataTree, DataValidationFlags,
@@ -19,7 +18,6 @@ fn main() -> std::io::Result<()> {
1918
ctx.load_module(module_name, None, &[])
2019
.expect("Failed to load module");
2120
}
22-
let ctx = Arc::new(ctx);
2321

2422
// Parse data tree in the JSON format.
2523
let dtree = DataTree::parse_file(

examples/data_json2xml.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::fs::File;
2-
use std::sync::Arc;
32
use yang3::context::{Context, ContextFlags};
43
use yang3::data::{
54
Data, DataFormat, DataParserFlags, DataPrinterFlags, DataTree,
@@ -20,7 +19,6 @@ fn main() -> std::io::Result<()> {
2019
ctx.load_module(module_name, None, &[])
2120
.expect("Failed to load module");
2221
}
23-
let ctx = Arc::new(ctx);
2422

2523
// Parse data tree in the JSON format.
2624
let dtree = DataTree::parse_file(

0 commit comments

Comments
 (0)