Skip to content

Commit d2a92ba

Browse files
committed
impl: use interior mut for flush_on_exit
This restores previous behavior where flush on exit behavior can be set without marking KVS object as `mut`.
1 parent 26273c9 commit d2a92ba

File tree

8 files changed

+20
-20
lines changed

8 files changed

+20
-20
lines changed

src/rust/rust_kvs/examples/flush.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn main() -> Result<(), ErrorCode> {
3434
{
3535
// Build KVS instance for given instance ID and temporary directory.
3636
let builder = KvsBuilder::<Kvs>::new(instance_id.clone()).dir(dir_string.clone());
37-
let mut kvs = builder.build()?;
37+
let kvs = builder.build()?;
3838

3939
// Disable flush on exit.
4040
kvs.set_flush_on_exit(FlushOnExit::No);
@@ -55,7 +55,7 @@ fn main() -> Result<(), ErrorCode> {
5555
{
5656
// Build KVS instance for given instance ID and temporary directory.
5757
let builder = KvsBuilder::<Kvs>::new(instance_id.clone()).dir(dir_string.clone());
58-
let mut kvs = builder.build()?;
58+
let kvs = builder.build()?;
5959

6060
// Explicitly enable flush on exit - this is the default.
6161
kvs.set_flush_on_exit(FlushOnExit::Yes);
@@ -76,7 +76,7 @@ fn main() -> Result<(), ErrorCode> {
7676
{
7777
// Build KVS instance for given instance ID and temporary directory.
7878
let builder = KvsBuilder::<Kvs>::new(instance_id.clone()).dir(dir_string.clone());
79-
let mut kvs = builder.build()?;
79+
let kvs = builder.build()?;
8080

8181
// Disable flush on exit.
8282
kvs.set_flush_on_exit(FlushOnExit::No);
@@ -94,7 +94,7 @@ fn main() -> Result<(), ErrorCode> {
9494
let builder = KvsBuilder::<Kvs>::new(instance_id.clone())
9595
.dir(dir_string)
9696
.need_kvs(true);
97-
let mut kvs = builder.build()?;
97+
let kvs = builder.build()?;
9898
kvs.set_flush_on_exit(FlushOnExit::No);
9999

100100
let k1_key = "k1";

src/rust/rust_kvs/examples/snapshots.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() -> Result<(), ErrorCode> {
1818

1919
// Build KVS instance for given instance ID and temporary directory.
2020
let builder = KvsBuilder::<Kvs>::new(instance_id.clone()).dir(dir_string.clone());
21-
let mut kvs = builder.build()?;
21+
let kvs = builder.build()?;
2222
kvs.set_flush_on_exit(FlushOnExit::No);
2323

2424
let max_count = Kvs::snapshot_max_count() as u32;
@@ -40,7 +40,7 @@ fn main() -> Result<(), ErrorCode> {
4040

4141
// Build KVS instance for given instance ID and temporary directory.
4242
let builder = KvsBuilder::<Kvs>::new(instance_id.clone()).dir(dir_string.clone());
43-
let mut kvs = builder.build()?;
43+
let kvs = builder.build()?;
4444
kvs.set_flush_on_exit(FlushOnExit::No);
4545

4646
let max_count = Kvs::snapshot_max_count() as u32;

src/rust/rust_kvs/src/kvs.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
// SPDX-License-Identifier: Apache-2.0
1111

12-
//std dependencies
12+
use std::cell::RefCell;
1313
use std::collections::HashMap;
1414
use std::fs;
1515
use std::path::PathBuf;
@@ -42,7 +42,7 @@ pub struct GenericKvs<J: KvsBackend> {
4242
filename_prefix: PathBuf,
4343

4444
/// Flush on exit flag
45-
flush_on_exit: FlushOnExit,
45+
flush_on_exit: RefCell<FlushOnExit>,
4646

4747
_backend: std::marker::PhantomData<J>,
4848
}
@@ -236,7 +236,7 @@ impl<J: KvsBackend> KvsApi for GenericKvs<J> {
236236
kvs: Mutex::new(kvs),
237237
default,
238238
filename_prefix,
239-
flush_on_exit: FlushOnExit::Yes,
239+
flush_on_exit: RefCell::new(FlushOnExit::Yes),
240240
_backend: std::marker::PhantomData,
241241
})
242242
}
@@ -246,15 +246,15 @@ impl<J: KvsBackend> KvsApi for GenericKvs<J> {
246246
/// # Return Values
247247
/// * `FlushOnExit`: Current flush on exit behavior.
248248
fn flush_on_exit(&self) -> FlushOnExit {
249-
self.flush_on_exit.clone()
249+
self.flush_on_exit.borrow().clone()
250250
}
251251

252252
/// Control the flush on exit behavior
253253
///
254254
/// # Parameters
255255
/// * `flush_on_exit`: Flag to control flush-on-exit behavior
256-
fn set_flush_on_exit(&mut self, flush_on_exit: FlushOnExit) {
257-
self.flush_on_exit = flush_on_exit;
256+
fn set_flush_on_exit(&self, flush_on_exit: FlushOnExit) {
257+
*self.flush_on_exit.borrow_mut() = flush_on_exit
258258
}
259259

260260
/// Resets a key-value-storage to its initial state
@@ -1025,7 +1025,7 @@ mod kvs_tests {
10251025

10261026
#[test]
10271027
fn test_set_flush_on_exit() {
1028-
let mut kvs = get_kvs::<MockBackend>(PathBuf::new(), KvsMap::new(), KvsMap::new());
1028+
let kvs = get_kvs::<MockBackend>(PathBuf::new(), KvsMap::new(), KvsMap::new());
10291029

10301030
kvs.set_flush_on_exit(FlushOnExit::Yes);
10311031
assert_eq!(kvs.flush_on_exit(), FlushOnExit::Yes);
@@ -1201,7 +1201,7 @@ mod kvs_tests {
12011201
let dir = tempdir().unwrap();
12021202
let dir_path = dir.path().to_path_buf();
12031203
{
1204-
let mut kvs = get_kvs::<JsonBackend>(dir_path.clone(), KvsMap::new(), KvsMap::new());
1204+
let kvs = get_kvs::<JsonBackend>(dir_path.clone(), KvsMap::new(), KvsMap::new());
12051205
kvs.set_flush_on_exit(FlushOnExit::Yes);
12061206
kvs.set_value("key", "value").unwrap();
12071207
}

src/rust/rust_kvs/src/kvs_api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub trait KvsApi {
127127
) -> Result<(), ErrorCode>;
128128
fn remove_key(&self, key: &str) -> Result<(), ErrorCode>;
129129
fn flush_on_exit(&self) -> FlushOnExit;
130-
fn set_flush_on_exit(&mut self, flush_on_exit: FlushOnExit);
130+
fn set_flush_on_exit(&self, flush_on_exit: FlushOnExit);
131131
fn flush(&self) -> Result<(), ErrorCode>;
132132
fn snapshot_count(&self) -> usize;
133133
fn snapshot_max_count() -> usize

src/rust/rust_kvs/src/kvs_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ mod tests {
215215
unimplemented!()
216216
}
217217

218-
fn set_flush_on_exit(&mut self, _flush_on_exit: FlushOnExit) {
218+
fn set_flush_on_exit(&self, _flush_on_exit: FlushOnExit) {
219219
unimplemented!()
220220
}
221221

src/rust/rust_kvs/src/kvs_mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl KvsApi for MockKvs {
4646
fn flush_on_exit(&self) -> FlushOnExit {
4747
FlushOnExit::No
4848
}
49-
fn set_flush_on_exit(&mut self, _flush_on_exit: FlushOnExit) {}
49+
fn set_flush_on_exit(&self, _flush_on_exit: FlushOnExit) {}
5050
fn reset(&self) -> Result<(), ErrorCode> {
5151
if self.fail {
5252
return Err(ErrorCode::UnmappedError);

src/rust/rust_kvs/tests/cit_persistency.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn cit_persistency_flush_on_exit_disabled_drop_data() -> Result<(), ErrorCode> {
100100

101101
{
102102
// First KVS run.
103-
let mut kvs = Kvs::open(
103+
let kvs = Kvs::open(
104104
InstanceId(0),
105105
OpenNeedDefaults::Optional,
106106
OpenNeedKvs::Optional,
@@ -158,7 +158,7 @@ fn cit_persistency_flush_on_exit_disabled_manual_flush() -> Result<(), ErrorCode
158158

159159
{
160160
// First KVS run.
161-
let mut kvs = Kvs::open(
161+
let kvs = Kvs::open(
162162
InstanceId(0),
163163
OpenNeedDefaults::Optional,
164164
OpenNeedKvs::Optional,

tests/rust_test_scenarios/src/test_basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Scenario for BasicScenario {
4646
}
4747

4848
// Create KVS.
49-
let mut kvs: Kvs = builder.build().unwrap();
49+
let kvs: Kvs = builder.build().unwrap();
5050
if let Some(flag) = params.flush_on_exit {
5151
let mode = if flag {
5252
FlushOnExit::Yes

0 commit comments

Comments
 (0)