qubit-function 0.10.6

Functional programming traits and Box/Rc/Arc adapters for Rust, inspired by Java functional interfaces
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026.
 *    Haixing Hu, Qubit Co. Ltd.
 *
 *    All rights reserved.
 *
 ******************************************************************************/

//! Unit tests for RunnableOnce and BoxRunnableOnce.

use std::{
    cell::Cell,
    io,
    rc::Rc,
};

use qubit_function::{
    BoxRunnableOnce,
    CallableOnce,
    RunnableOnce,
    SupplierOnce,
};

#[derive(Clone)]
struct ClonedRunnableOnce {
    flag: Rc<Cell<bool>>,
}

impl RunnableOnce<io::Error> for ClonedRunnableOnce {
    fn run(self) -> Result<(), io::Error> {
        self.flag.set(true);
        Ok(())
    }
}

struct FlagCallableOnce {
    flag: Rc<Cell<bool>>,
}

impl CallableOnce<i32, io::Error> for FlagCallableOnce {
    fn call(self) -> Result<i32, io::Error> {
        self.flag.set(true);
        Ok(42)
    }
}

#[test]
fn test_runnable_once_closure_run_returns_success() {
    let flag = Rc::new(Cell::new(false));
    let captured = Rc::clone(&flag);
    let task = move || {
        captured.set(true);
        Ok::<(), io::Error>(())
    };

    task.run().expect("runnable-once closure should succeed");
    assert!(flag.get());
}

#[test]
fn test_runnable_once_closure_run_returns_error() {
    let task = || Err::<(), _>(io::Error::other("failed"));

    let error = task.run().expect_err("runnable-once closure should fail");
    assert_eq!(error.kind(), io::ErrorKind::Other);
    assert_eq!(error.to_string(), "failed");
}

#[test]
fn test_runnable_once_closure_into_box_executes_once() {
    let task = || Ok::<(), io::Error>(());

    let boxed = RunnableOnce::into_box(task);
    boxed.run().expect("boxed runnable-once should succeed");
}

#[test]
fn test_runnable_once_closure_into_fn_returns_fn_once() {
    let task = || Ok::<(), io::Error>(());
    let function = RunnableOnce::into_fn(task);

    function().expect("runnable-once function should succeed");
}

#[test]
fn test_runnable_once_to_box_clones_runnable() {
    let flag = Rc::new(Cell::new(false));
    let task = ClonedRunnableOnce {
        flag: Rc::clone(&flag),
    };

    let first = task.to_box();
    first.run().expect("boxed clone should succeed");
    assert!(flag.get());

    flag.set(false);
    let second = task.to_box();
    second
        .run()
        .expect("original runnable should remain reusable");
    assert!(flag.get());
}

#[test]
fn test_runnable_once_to_fn_clones_runnable() {
    let flag = Rc::new(Cell::new(false));
    let task = ClonedRunnableOnce {
        flag: Rc::clone(&flag),
    };

    let function = task.to_fn();
    function().expect("cloned runnable should succeed");
    assert!(flag.get());
}

#[test]
fn test_runnable_once_default_into_callable_returns_unit() {
    let flag = Rc::new(Cell::new(false));
    let task = ClonedRunnableOnce {
        flag: Rc::clone(&flag),
    };

    let callable = RunnableOnce::into_callable(task);
    callable.call().expect("unit callable should succeed");
    assert!(flag.get());
}

#[test]
fn test_box_runnable_once_new_and_run() {
    let flag = Rc::new(Cell::new(false));
    let captured = Rc::clone(&flag);
    let task = BoxRunnableOnce::new(move || {
        captured.set(true);
        Ok::<(), io::Error>(())
    });

    task.run().expect("box runnable-once should succeed");
    assert!(flag.get());
}

#[test]
fn test_box_runnable_once_name_management() {
    let mut task = BoxRunnableOnce::<io::Error>::new_with_name("cleanup", || Ok(()));
    assert_eq!(task.name(), Some("cleanup"));
    assert_eq!(task.to_string(), "BoxRunnableOnce(cleanup)");
    assert!(format!("{task:?}").contains("cleanup"));

    task.set_name("renamed");
    assert_eq!(task.name(), Some("renamed"));

    task.clear_name();
    assert_eq!(task.name(), None);
    assert_eq!(task.to_string(), "BoxRunnableOnce");
}

#[test]
fn test_box_runnable_once_into_box_returns_self() {
    let task = BoxRunnableOnce::new(|| Ok::<(), io::Error>(()));
    let boxed = RunnableOnce::into_box(task);
    boxed
        .run()
        .expect("boxed runnable conversion should succeed");
}

#[test]
fn test_box_runnable_once_into_fn_extracts_function() {
    let task = BoxRunnableOnce::new(|| Ok::<(), io::Error>(()));
    let function = RunnableOnce::into_fn(task);

    function().expect("runnable-once function should succeed");
}

#[test]
fn test_box_runnable_once_implements_supplier_once() {
    let task = BoxRunnableOnce::new(|| Ok::<(), io::Error>(()));

    let result = SupplierOnce::get(task);

    result.expect("supplier once runnable should succeed");
}

#[test]
fn test_box_runnable_once_and_then_runs_next_on_success() {
    let events = Rc::new(Cell::new(0));
    let first_events = Rc::clone(&events);
    let second_events = Rc::clone(&events);
    let first = BoxRunnableOnce::new(move || {
        first_events.set(1);
        Ok::<(), io::Error>(())
    });
    let second = move || {
        second_events.set(2);
        Ok::<(), io::Error>(())
    };

    let chained = first.and_then(second);
    chained.run().expect("chained runnable-once should succeed");
    assert_eq!(events.get(), 2);
}

#[test]
fn test_box_runnable_once_and_then_skips_next_on_error() {
    let events = Rc::new(Cell::new(0));
    let second_events = Rc::clone(&events);
    let first = BoxRunnableOnce::new(|| Err::<(), _>(io::Error::other("stop")));
    let second = move || {
        second_events.set(2);
        Ok::<(), io::Error>(())
    };

    let chained = first.and_then(second);
    assert_eq!(
        chained
            .run()
            .expect_err("chained runnable should preserve error")
            .to_string(),
        "stop",
    );
    assert_eq!(events.get(), 0);
}

#[test]
fn test_box_runnable_once_combinators_cover_branches_with_same_next_types() {
    let success_flag = Rc::new(Cell::new(false));
    let first = BoxRunnableOnce::new(|| Ok::<(), io::Error>(()));
    let chained = first.and_then(ClonedRunnableOnce {
        flag: Rc::clone(&success_flag),
    });
    chained
        .run()
        .expect("concrete and_then next should run after success");
    assert!(success_flag.get());

    let error_flag = Rc::new(Cell::new(false));
    let first = BoxRunnableOnce::new(|| Err::<(), _>(io::Error::other("stop")));
    let chained = first.and_then(ClonedRunnableOnce {
        flag: Rc::clone(&error_flag),
    });
    assert_eq!(
        chained
            .run()
            .expect_err("concrete and_then next should be skipped")
            .to_string(),
        "stop",
    );
    assert!(!error_flag.get());

    let success_flag = Rc::new(Cell::new(false));
    let first = BoxRunnableOnce::new(|| Ok::<(), io::Error>(()));
    let callable = first.then_callable(FlagCallableOnce {
        flag: Rc::clone(&success_flag),
    });
    assert_eq!(
        callable
            .call()
            .expect("concrete callable should run after success"),
        42
    );
    assert!(success_flag.get());

    let error_flag = Rc::new(Cell::new(false));
    let first = BoxRunnableOnce::new(|| Err::<(), _>(io::Error::other("prepare failed")));
    let callable = first.then_callable(FlagCallableOnce {
        flag: Rc::clone(&error_flag),
    });
    assert_eq!(
        callable
            .call()
            .expect_err("concrete callable should be skipped")
            .to_string(),
        "prepare failed",
    );
    assert!(!error_flag.get());
}

#[test]
fn test_box_runnable_once_then_callable_runs_callable_on_success() {
    let task = BoxRunnableOnce::new_with_name("prepare", || Ok::<(), io::Error>(()));
    let callable = || Ok::<i32, io::Error>(42);

    let chained = task.then_callable(callable);
    assert_eq!(chained.name(), Some("prepare"));
    assert_eq!(chained.call().expect("callable should succeed"), 42);
}

#[test]
fn test_box_runnable_once_then_callable_skips_callable_on_error() {
    let callable_ran = Rc::new(Cell::new(false));
    let callable_ran_capture = Rc::clone(&callable_ran);
    let task = BoxRunnableOnce::<io::Error>::new(|| Err(io::Error::other("prepare failed")));
    let callable = move || {
        callable_ran_capture.set(true);
        Ok::<i32, io::Error>(42)
    };

    let chained = task.then_callable(callable);
    let error = chained
        .call()
        .expect_err("then_callable should preserve runnable error");

    assert_eq!(error.to_string(), "prepare failed");
    assert!(!callable_ran.get());
}

#[test]
fn test_box_runnable_once_into_callable() {
    let task = BoxRunnableOnce::new_with_name("cleanup", || Ok::<(), io::Error>(()));
    let callable = task.into_callable();

    assert_eq!(callable.name(), Some("cleanup"));
    callable.call().expect("unit callable should succeed");
}

#[derive(Clone)]
struct TextRunnableOnce {
    events: Rc<Cell<u32>>,
}

impl RunnableOnce<&'static str> for TextRunnableOnce {
    fn run(self) -> Result<(), &'static str> {
        self.events.set(self.events.get() + 1);
        Ok(())
    }
}

#[test]
fn test_runnable_once_default_conversions_with_text_error_type() {
    let events = Rc::new(Cell::new(0));
    let task = TextRunnableOnce {
        events: Rc::clone(&events),
    };

    let boxed = RunnableOnce::into_box(task.clone());
    boxed.run().expect("into_box should succeed");

    let function = RunnableOnce::into_fn(task.clone());
    function().expect("into_fn should succeed");

    let boxed_from_ref = task.to_box();
    boxed_from_ref.run().expect("to_box should succeed");

    let function_from_ref = task.to_fn();
    function_from_ref().expect("to_fn should succeed");

    let callable = RunnableOnce::into_callable(task);
    callable.call().expect("into_callable should succeed");

    assert_eq!(events.get(), 5);
}

#[test]
fn test_box_runnable_once_from_supplier_with_text_error_type() {
    let task = BoxRunnableOnce::from_supplier(|| Ok::<(), &'static str>(()));
    task.run().expect("from_supplier should succeed");
}

#[test]
fn test_box_runnable_once_combinators_with_text_error_type() {
    let events = Rc::new(Cell::new(0));
    let first_events = Rc::clone(&events);
    let second_events = Rc::clone(&events);

    let first = BoxRunnableOnce::new(move || {
        first_events.set(first_events.get() + 1);
        Ok::<(), &'static str>(())
    });
    let second = move || {
        second_events.set(second_events.get() + 1);
        Ok::<(), &'static str>(())
    };
    let chained = first.and_then(second);
    chained.run().expect("and_then should succeed");
    assert_eq!(events.get(), 2);

    let runnable = BoxRunnableOnce::new(|| Ok::<(), &'static str>(()));
    let callable = runnable.then_callable(|| Ok::<i32, &'static str>(9));
    assert_eq!(callable.call().expect("then_callable should succeed"), 9);

    let skipped = Rc::new(Cell::new(false));
    let skipped_capture = Rc::clone(&skipped);
    let first = BoxRunnableOnce::new(|| Err::<(), &'static str>("stop"));
    let second = move || {
        skipped_capture.set(true);
        Ok::<(), &'static str>(())
    };
    let chained = first.and_then(second);
    assert_eq!(chained.run().expect_err("and_then should fail"), "stop");
    assert!(!skipped.get());

    let callable_ran = Rc::new(Cell::new(false));
    let callable_ran_capture = Rc::clone(&callable_ran);
    let runnable = BoxRunnableOnce::new(|| Err::<(), &'static str>("prepare"));
    let callable = runnable.then_callable(move || {
        callable_ran_capture.set(true);
        Ok::<i32, &'static str>(9)
    });
    assert_eq!(
        callable
            .call()
            .expect_err("then_callable should preserve runnable error"),
        "prepare"
    );
    assert!(!callable_ran.get());
}