rxrust 1.0.0-rc.4

A Rust implementation of Reactive Extensions.
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
//! Observable traits and type inference
//!
//! This module contains CoreObservable, Observable traits.
//!
//! The architecture has two layers:
//! 1. CoreObservable<O>: The pure logic kernel, generic over observer context
//! 2. Observable: User-facing trait with inferred Item/Err types via
//!    ObservableType

// Submodules
pub mod boxed;
pub mod connectable;
pub mod create;
pub mod defer;
pub mod from_fn;
pub mod from_future;
pub mod from_iter;
pub mod from_stream;
pub mod interval;
pub mod of;
pub mod timer;
pub mod trivial;

// Re-exports
// Standard library imports

pub use boxed::*;
pub use connectable::*;
pub use create::*;
pub use defer::*;
pub use from_fn::*;
pub use from_future::FromFuture;
pub use from_iter::*;
pub use from_stream::{FromStream, FromStreamResult};
pub use interval::*;
pub use of::*;
pub use timer::*;
pub use trivial::*;

// Internal imports (avoid circular dependency with prelude)
use crate::context::Context;
use crate::ops::{
  average::{Average, Averageable},
  buffer::Buffer, // Restored
  buffer_count::BufferCount,
  buffer_time::BufferTime,
  collect::Collect,
  combine_latest::CombineLatest,
  contains::Contains,
  debounce::Debounce,
  default_if_empty::DefaultIfEmpty,
  delay::{Delay, DelaySubscriptionOp},
  distinct::{Distinct, DistinctKey},
  distinct_until_changed::{DistinctUntilChanged, DistinctUntilKeyChanged},
  filter::Filter,
  filter_map::FilterMap,
  finalize::Finalize,
  flat_map::FlatMap,
  group_by::GroupBy,
  into_future::{ObservableFuture, SupportsIntoFuture},
  into_stream::SupportsIntoStream,
  last::Last,
  lifecycle::{OnComplete, OnError},
  map::Map,
  map_err::MapErr,
  map_to::MapTo,
  merge::Merge,
  merge_all::MergeAll,
  observe_on::ObserveOn,
  pairwise::Pairwise,
  reduce::{Reduce, ReduceFn, ReduceInitialFn},
  retry::{Retry, RetryPolicy},
  sample::Sample,
  scan::Scan,
  skip::Skip,
  skip_last::SkipLast,
  skip_until::SkipUntil,
  skip_while::SkipWhile,
  start_with::StartWith,
  subscribe_on::SubscribeOn,
  switch_map::SwitchMap,
  take::Take,
  take_last::TakeLast,
  take_until::TakeUntil,
  take_while::TakeWhile,
  tap::Tap,
  throttle::{Throttle, ThrottleEdge, ThrottleWhenParam},
  with_latest_from::WithLatestFrom,
  zip::Zip,
};
use crate::{
  observer::FnMutObserver,
  scheduler::{Duration, Instant},
  subject::{Subject, SubjectPtr, SubjectPtrMutRef},
  subscription::Subscription,
};

/// The Public Type Trait
/// This trait purely exposes the types. It is what users see in error messages
/// and documentation.
pub trait ObservableType {
  type Item<'a>
  where
    Self: 'a;
  type Err;
}

/// CoreObservable: The publisher kernel
///
/// This trait represents the pure logic of an observable sequence.
/// It is generic over the observer context (O), which allows the same
/// logic to work with both Local and Shared contexts.
pub trait CoreObservable<O>: ObservableType {
  /// The subscription handle returned when subscribing
  type Unsub: Subscription;

  /// Subscribe an observer to this observable
  ///
  /// The observer parameter is typically a Context wrapping the actual
  /// observer.
  fn subscribe(self, observer: O) -> Self::Unsub;
}

/// Observable: The user-facing API
///
/// This trait extends Context and provides a clean API where users can see
/// the Item and Err types.
pub trait Observable: Context {
  /// The type of values emitted by this observable
  type Item<'a>
  where
    Self: 'a;

  /// The type of errors emitted by this observable
  type Err;

  /// Subscribe with a closure for the next values.
  ///
  /// This is the primary subscription method, optimized for ergonomics.
  fn subscribe<F, U>(self, f: F) -> U
  where
    F: for<'a> FnMut(Self::Item<'a>),
    Self::Inner: CoreObservable<Self::With<FnMutObserver<F>>, Unsub = U>,
  {
    let wrapped = Self::lift(FnMutObserver(f));
    self.into_inner().subscribe(wrapped)
  }

  /// Subscribe with a full Observer implementation.
  ///
  /// Use this method when you need to handle `error` and `complete` explicitly,
  /// or when subscribing a Subject/custom Observer struct.
  fn subscribe_with<O>(self, observer: O) -> <Self::Inner as CoreObservable<Self::With<O>>>::Unsub
  where
    Self::Inner: CoreObservable<Self::With<O>>,
  {
    let (core, wrapped) = self.swap(observer);
    core.subscribe(wrapped)
  }

  /// Apply a transformation to the values emitted by this observable
  ///
  /// This is a higher-order operator that returns a new observable
  /// with the transformed values.
  #[cfg(not(feature = "nightly"))]
  fn map<F, Out>(self, f: F) -> Self::With<Map<Self::Inner, F>>
  where
    F: for<'a> FnMut(Self::Item<'a>) -> Out,
  {
    self.transform(|core| Map { source: core, func: f })
  }

  // Keep the nightly `FnMut<(...)>` path here instead of replacing it with a
  // custom callable trait. A custom trait can model the lifetime-dependent
  // output for `map` itself, but it breaks closure inference further
  // downstream, especially for `subscribe`, and that damage compounds across an
  // operator chain. If we revisit stable support here, prefer a narrow,
  // explicit API rather than swapping the whole closure ecosystem over to a
  // library-defined trait.
  #[cfg(feature = "nightly")]
  fn map<F>(self, f: F) -> Self::With<Map<Self::Inner, F>>
  where
    F: for<'a> FnMut<(Self::Item<'a>,)>,
  {
    self.transform(|core| Map { source: core, func: f })
  }

  /// Map every emission to a constant value
  ///
  /// This operator maps every emission from the source observable to a constant
  /// value. It requires the constant value to be `Clone` since it is emitted
  /// multiple times.
  fn map_to<B>(self, value: B) -> Self::With<MapTo<Self::Inner, B>>
  where
    B: Clone,
  {
    self.transform(|core| MapTo { source: core, value })
  }

  /// Filter items emitted by the source observable
  ///
  /// This operator emits only those items from the source Observable that pass
  /// a predicate test. Items that cause the predicate to return false are
  /// simply not emitted and are discarded.
  ///
  /// # Arguments
  ///
  /// * `filter` - A predicate function that takes a reference to an item and
  ///   returns true if it should be emitted
  ///
  /// # Type Parameters
  ///
  /// * `F` - The predicate function type
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).filter(|v| v % 2 == 0);
  /// // Emits: 2, 4
  /// ```
  fn filter<F>(self, filter: F) -> Self::With<Filter<Self::Inner, F>>
  where
    F: for<'a> FnMut(&Self::Item<'a>) -> bool,
  {
    self.transform(|source| Filter { source, filter })
  }

  /// Emit only items that have not been emitted before
  ///
  /// This operator filters out duplicate items, emitting only the first
  /// occurrence of each identical item. It uses a `HashSet` to keep track of
  /// seen items.
  ///
  /// # Requirements
  ///
  /// * `Item` must implement `Eq`, `Hash`, and `Clone`.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 2, 3, 1, 3, 4]).distinct();
  /// // Emits: 1, 2, 3, 4
  /// ```
  fn distinct(self) -> Self::With<Distinct<Self::Inner>>
  where
    for<'a> Self::Item<'a>: Eq + std::hash::Hash + Clone,
  {
    self.transform(Distinct)
  }

  /// Emit only items where the key has not been emitted before
  ///
  /// This operator filters out items based on a key derived from each item.
  /// Only the first item with a specific key is emitted.
  ///
  /// # Arguments
  ///
  /// * `key_selector` - A function that extracts a key from each item used for
  ///   uniqueness
  ///
  /// # Requirements
  ///
  /// * `Key` must implement `Eq`, `Hash`, and `Clone`.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([(1, "a"), (1, "b"), (2, "a")]).distinct_key(|(id, _)| *id);
  /// // Emits: (1, "a"), (2, "a")
  /// ```
  fn distinct_key<F, Key>(self, key_selector: F) -> Self::With<DistinctKey<Self::Inner, F>>
  where
    F: for<'a> Fn(&Self::Item<'a>) -> Key,
    Key: Eq + std::hash::Hash + Clone,
  {
    self.transform(|source| DistinctKey { source, key_selector })
  }

  /// Emit only items that are different from the previous item
  ///
  /// This operator filters out consecutive duplicate items.
  ///
  /// # Requirements
  ///
  /// * `Item` must implement `PartialEq` and `Clone`.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 2, 3]).distinct_until_changed();
  /// // Emits: 1, 2, 3
  /// ```
  fn distinct_until_changed(self) -> Self::With<DistinctUntilChanged<Self::Inner>>
  where
    for<'a> Self::Item<'a>: PartialEq + Clone,
  {
    self.transform(DistinctUntilChanged)
  }

  /// Emit items which are different from the previous item based on a key
  /// selector
  ///
  /// This operator filters out consecutive items that have the same key.
  ///
  /// # Arguments
  ///
  /// * `key_selector` - A function that extracts a key from each item
  ///
  /// # Requirements
  ///
  /// * `Key` must implement `PartialEq`.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable =
  ///   Local::from_iter([(1, 1), (1, 2), (2, 1)]).distinct_until_key_changed(|(k, _)| *k);
  /// // Emits: (1, 1), (2, 1)
  /// // Note: (1, 2) is skipped because its key (1) is same as previous (1)
  /// ```
  fn distinct_until_key_changed<F, Key>(
    self, key_selector: F,
  ) -> Self::With<DistinctUntilKeyChanged<Self::Inner, F>>
  where
    F: for<'a> Fn(&Self::Item<'a>) -> Key,
    Key: PartialEq,
  {
    self.transform(|source| DistinctUntilKeyChanged { source, key_selector })
  }

  /// Apply a function to each item and emit only the Some values
  ///
  /// This operator maps each item to an Option<T>, emitting only the Some(T)
  /// values. It effectively combines filtering and mapping in a single
  /// operation, allowing you to transform values while simultaneously
  /// filtering out None results.
  ///
  /// # Arguments
  ///
  /// * `f` - A function that takes an item and returns Option<Out>
  ///
  /// # Type Parameters
  ///
  /// * `Out` - The output type contained in the Option
  /// * `F` - The filter_map function type
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable =
  ///   Local::from_iter(["1", "2", "invalid", "3"]).filter_map(|s| s.parse::<i32>().ok());
  /// // Emits: 1, 2, 3
  /// ```
  fn filter_map<F, Out>(self, f: F) -> Self::With<FilterMap<Self::Inner, F>>
  where
    F: for<'a> FnMut(Self::Item<'a>) -> Option<Out>,
  {
    self.transform(|source| FilterMap { source, func: f })
  }

  /// Apply an accumulator function and emit each intermediate result
  ///
  /// This operator applies an accumulator function over the source Observable
  /// and returns each intermediate result. It's similar to `reduce` but emits
  /// the intermediate accumulations.
  ///
  /// # Arguments
  ///
  /// * `initial` - The initial accumulator value
  /// * `f` - A function that takes the current accumulator and a value, and
  ///   returns the new accumulator
  ///
  /// # Type Parameters
  ///
  /// * `Output` - The type of the accumulator value (must be Clone)
  /// * `F` - The accumulator function type
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4]).scan(0, |acc, v| acc + v);
  /// // Emits: 1, 3, 6, 10
  /// ```
  fn scan<Output, F>(self, initial: Output, f: F) -> Self::With<Scan<Self::Inner, F, Output>>
  where
    Output: Clone,
    F: for<'a> FnMut(Output, Self::Item<'a>) -> Output,
  {
    self.transform(|source| Scan { source, func: f, initial_value: initial })
  }

  /// Emit only the first `count` values from the source observable
  ///
  /// This operator emits only the first `count` values emitted by the source
  /// observable, then completes. If the source completes before emitting
  /// `count` values, `take` will also complete early.
  ///
  /// # Arguments
  ///
  /// * `count` - The maximum number of values to emit
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).take(3);
  /// // Emits: 1, 2, 3
  /// ```
  fn take(self, count: usize) -> Self::With<Take<Self::Inner>> {
    self.transform(|source| Take { source, count })
  }

  /// Emit only the first value from the source observable
  ///
  /// This operator emits only the first value emitted by the source
  /// observable, then completes. If the source completes without emitting
  /// any values, this operator will complete without emitting anything.
  ///
  /// This is equivalent to `take(1)`.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3]).first();
  /// // Emits: 1
  /// ```
  fn first(self) -> Self::With<Take<Self::Inner>> { self.take(1) }

  /// Emit the first value from the source observable, or a default if empty
  ///
  /// This operator emits the first value that was emitted. If the source
  /// emits no values, it emits the provided default value instead.
  ///
  /// This is equivalent to `take(1).default_if_empty(default_value)`.
  ///
  /// # Arguments
  ///
  /// * `default_value` - The default value to emit if the source emits no
  ///   values
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// // With values - emits the first value
  /// let observable = Local::from_iter([1, 2, 3]).first_or(0);
  /// // Emits: 1
  ///
  /// // Without values - emits the default
  /// let observable = Local::from_iter(std::iter::empty::<i32>()).first_or(0);
  /// // Emits: 0
  /// ```
  fn first_or<'a>(
    self, default_value: Self::Item<'a>,
  ) -> Self::With<DefaultIfEmpty<Take<Self::Inner>, Self::Item<'a>>> {
    self.transform(|source| DefaultIfEmpty { source: Take { source, count: 1 }, default_value })
  }

  /// Skip the first `count` values from the source observable
  ///
  /// This operator ignores the first `count` values emitted by the source
  /// observable, then emits all subsequent values. If the source completes
  /// before emitting `count` values, `skip` will complete without emitting
  /// any values.
  ///
  /// # Arguments
  ///
  /// * `count` - The number of values to skip
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).skip(2);
  /// // Emits: 3, 4, 5
  /// ```
  fn skip(self, count: usize) -> Self::With<Skip<Self::Inner>> {
    self.transform(|source| Skip { source, count })
  }

  /// Emit only the last `count` values from the source observable
  ///
  /// This operator buffers the last `count` values emitted by the source
  /// observable. When the source completes, it emits all buffered values
  /// in order and then completes. If the source emits fewer than `count`
  /// values, all values are emitted.
  ///
  /// # Arguments
  ///
  /// * `count` - The maximum number of last values to emit
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).take_last(3);
  /// // Emits: 3, 4, 5
  /// ```
  fn take_last(self, count: usize) -> Self::With<TakeLast<Self::Inner>> {
    self.transform(|source| TakeLast { source, count })
  }

  /// Emit only the last value from the source observable
  ///
  /// This operator waits for the source to complete, then emits only the
  /// last value that was emitted. If the source emits no values, this operator
  /// will complete without emitting anything.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).last();
  /// // Emits: 5
  /// ```
  fn last(self) -> Self::With<Last<Self::Inner>> { self.transform(|source| Last { source }) }

  /// Emit the last value from the source observable, or a default if empty
  ///
  /// This operator waits for the source to complete, then emits the last value
  /// that was emitted. If the source emits no values, it emits the provided
  /// default value instead.
  ///
  /// This is equivalent to `last().default_if_empty(default_value)` but
  /// implemented as a single operator for better performance and convenience.
  ///
  /// # Arguments
  ///
  /// * `default_value` - The default value to emit if the source emits no
  ///   values
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// // With values - emits the last value
  /// let observable = Local::from_iter([1, 2, 3]).last_or(0);
  /// // Emits: 3
  ///
  /// // Without values - emits the default
  /// let observable = Local::from_iter(std::iter::empty::<i32>()).last_or(0);
  /// // Emits: 0
  /// ```
  fn last_or<'a>(
    self, default_value: Self::Item<'a>,
  ) -> Self::With<DefaultIfEmpty<Last<Self::Inner>, Self::Item<'a>>> {
    self.transform(|source| DefaultIfEmpty { source: Last { source }, default_value })
  }

  /// Skip the last `count` values from the source observable
  ///
  /// This operator buffers up to `count` values. When a new value arrives
  /// and the buffer is full, the oldest value is emitted. When the source
  /// completes, the buffered values (the last `count` items) are discarded.
  ///
  /// # Arguments
  ///
  /// * `count` - The number of last values to skip
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).skip_last(2);
  /// // Emits: 1, 2, 3
  /// ```
  fn skip_last(self, count: usize) -> Self::With<SkipLast<Self::Inner>> {
    self.transform(|source| SkipLast { source, count })
  }

  /// Skip values while a predicate returns true
  ///
  /// This operator skips values from the source observable as long as the
  /// predicate function returns true. Once the predicate returns false,
  /// it emits that value and all subsequent values.
  ///
  /// # Arguments
  ///
  /// * `predicate` - A function that takes a reference to an item and returns
  ///   true to skip
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).skip_while(|v| *v < 3);
  /// // Emits: 3, 4, 5
  /// ```
  fn skip_while<P>(self, predicate: P) -> Self::With<SkipWhile<Self::Inner, P>>
  where
    P: for<'a> FnMut(&Self::Item<'a>) -> bool,
  {
    self.transform(|source| SkipWhile { source, predicate })
  }

  /// Check if the source observable emits a specific value
  ///
  /// This operator emits `true` if the specified value is emitted by the source
  /// observable, otherwise it emits `false` when the source completes. The
  /// operation short-circuits and completes as soon as the target value is
  /// found.
  ///
  /// # Arguments
  ///
  /// * `target` - The value to search for (must implement PartialEq)
  ///
  /// # Requirements
  ///
  /// * `Item` must implement `PartialEq` and `Clone`.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// // Check for existing value
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).contains(3);
  /// // Emits: true
  ///
  /// // Check for missing value
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).contains(10);
  /// // Emits: false
  /// ```
  fn contains<Item>(self, target: Item) -> Self::With<Contains<Self::Inner, Item>>
  where
    for<'a> Self::Item<'a>: PartialEq<Item>,
    Item: Clone,
  {
    self.transform(|source| Contains { source, target })
  }

  /// Emit values while a predicate returns true
  ///
  /// This operator emits values from the source observable as long as the
  /// predicate function returns true. When the predicate returns false,
  /// it completes the stream and stops emitting values.
  ///
  /// # Arguments
  ///
  /// * `predicate` - A function that takes a reference to an item and returns
  ///   true to continue emitting
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).take_while(|v| *v < 4);
  /// // Emits: 1, 2, 3
  /// ```
  fn take_while<P>(self, predicate: P) -> Self::With<TakeWhile<Self::Inner, P>>
  where
    P: for<'a> FnMut(&Self::Item<'a>) -> bool,
  {
    self.transform(|source| TakeWhile { source, predicate, inclusive: false })
  }

  /// Emit values while a predicate returns true, including the first failing
  /// value
  ///
  /// This operator emits values from the source observable as long as the
  /// predicate function returns true. When the predicate returns false,
  /// it emits that value (inclusive), then completes the stream.
  ///
  /// # Arguments
  ///
  /// * `predicate` - A function that takes a reference to an item and returns
  ///   true to continue emitting
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4, 5]).take_while_inclusive(|v| *v < 4);
  /// // Emits: 1, 2, 3, 4
  /// ```
  fn take_while_inclusive<P>(self, predicate: P) -> Self::With<TakeWhile<Self::Inner, P>>
  where
    P: for<'a> FnMut(&Self::Item<'a>) -> bool,
  {
    self.transform(|source| TakeWhile { source, predicate, inclusive: true })
  }

  /// Emit values until a notifier observable emits
  ///
  /// This operator emits values from the source observable until the notifier
  /// observable emits a value. When the notifier emits, the source
  /// subscription is cancelled and the stream completes.
  ///
  /// # Arguments
  ///
  /// * `notifier` - The observable that triggers termination
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let mut notifier = Local::subject();
  /// let source = Local::from_iter(0..10);
  /// source
  ///   .take_until(notifier.clone())
  ///   .subscribe(|v| println!("{}", v));
  /// notifier.next(());
  /// ```
  fn take_until<N>(self, notifier: N) -> Self::With<TakeUntil<Self::Inner, N::Inner>>
  where
    N: Observable<Err = Self::Err, Inner: ObservableType>,
  {
    self.transform(|source| TakeUntil { source, notifier: notifier.into_inner() })
  }

  /// Skip values until a notifier observable emits
  ///
  /// This operator skips values from the source observable until the notifier
  /// observable emits a value. Once the notifier emits, subsequent source
  /// values are passed through.
  ///
  /// # Arguments
  ///
  /// * `notifier` - The observable that triggers the start of emissions
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let mut notifier = Local::subject();
  /// let source = Local::from_iter(0..10);
  /// source
  ///   .skip_until(notifier.clone())
  ///   .subscribe(|v| println!("{}", v));
  /// notifier.next(());
  /// ```
  fn skip_until<N>(self, notifier: N) -> Self::With<SkipUntil<Self::Inner, N::Inner>>
  where
    N: Observable<Err = Self::Err, Inner: ObservableType>,
  {
    self.transform(|source| SkipUntil { source, notifier: notifier.into_inner() })
  }

  /// Group consecutive emissions into pairs
  ///
  /// This operator groups consecutive emissions from the source Observable into
  /// tuples `(previous, current)`. On the first emission, it stores the value.
  /// On subsequent emissions, it emits `(stored, current)` and updates the
  /// stored value.
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// let observable = Local::from_iter([1, 2, 3, 4]).pairwise();
  /// let mut result = Vec::new();
  /// observable.subscribe(|pair| result.push(pair));
  /// assert_eq!(result, vec![(1, 2), (2, 3), (3, 4)]);
  /// ```
  fn pairwise(self) -> Self::With<Pairwise<Self::Inner>> {
    self.transform(|source| Pairwise { source })
  }

  /// Merge this observable with another observable
  ///
  /// This operator combines two observables by subscribing to both and emitting
  /// values from either source as they arrive. The merged stream only completes
  /// when BOTH source streams complete.
  fn merge<'a, S2>(self, other: S2) -> Self::With<Merge<Self::Inner, S2::Inner>>
  where
    Self: 'a,
    S2: Observable<Inner: ObservableType<Item<'a> = Self::Item<'a>, Err = Self::Err>> + 'a,
  {
    self.transform(|core| Merge { source1: core, source2: other.into_inner() })
  }

  /// Combine the latest values from two observables
  ///
  /// This operator combines the latest values from two observables using a
  /// binary function. It emits whenever either source emits, using the most
  /// recent value from the other source.
  ///
  /// # Arguments
  ///
  /// * `other` - The second observable to combine with
  /// * `binary_op` - A function that combines the latest values from both
  ///   sources
  fn combine_latest<S2, F, OutputItem>(
    self, other: S2, binary_op: F,
  ) -> Self::With<CombineLatest<Self::Inner, S2::Inner, F>>
  where
    S2: Observable<Inner: ObservableType<Err = Self::Err>>,
    F: for<'a> FnMut(Self::Item<'a>, S2::Item<'a>) -> OutputItem,
  {
    self.transform(|core| CombineLatest { source_a: core, source_b: other.into_inner(), binary_op })
  }

  /// Combine each emission from this observable with the latest value from
  /// another
  ///
  /// Unlike `combine_latest`, this operator only emits when the primary (self)
  /// observable emits. The secondary observable just provides its latest
  /// value. If the secondary hasn't emitted yet, primary emissions are
  /// dropped.
  ///
  /// # Arguments
  ///
  /// * `other` - The secondary observable providing the latest value
  ///
  /// # Examples
  ///
  /// ```rust
  /// use std::convert::Infallible;
  ///
  /// use rxrust::prelude::*;
  ///
  /// let primary = Local::subject::<i32, Infallible>();
  /// let secondary = Local::subject::<i32, Infallible>();
  ///
  /// primary
  ///   .with_latest_from(secondary)
  ///   .subscribe(|(a, b): (i32, i32)| println!("({}, {})", a, b));
  /// ```
  fn with_latest_from<S2>(self, other: S2) -> Self::With<WithLatestFrom<Self::Inner, S2::Inner>>
  where
    S2: Observable<Inner: ObservableType<Err = Self::Err>>,
  {
    self.transform(|core| WithLatestFrom { source_a: core, source_b: other.into_inner() })
  }

  /// Flatten a Higher-Order Observable by merging inner observables
  ///
  /// This operator subscribes to inner observables as they arrive, up to
  /// the `concurrent` limit. When an inner observable completes, the next
  /// queued one is subscribed to.
  ///
  /// The stream completes when the outer observable AND all inner observables
  /// have completed.
  ///
  /// # Arguments
  ///
  /// * `concurrent` - Maximum number of inner observables to subscribe to
  ///   concurrently. Use `usize::MAX` for unlimited concurrency.
  fn merge_all(self, concurrent: usize) -> Self::With<MergeAll<Self::Inner>> {
    self.transform(|source| MergeAll { source, concurrent })
  }

  /// Flatten a Higher-Order Observable by subscribing to inner observables
  /// sequentially
  ///
  /// This is equivalent to `merge_all(1)` - waits for each inner observable to
  /// complete before subscribing to the next one.
  fn concat_all(self) -> Self::With<MergeAll<Self::Inner>> { self.merge_all(1) }

  /// Split the source observable into multiple GroupedObservables
  ///
  /// Each unique key derived from source values creates a new
  /// `GroupedObservable`. Values with the same key are forwarded to the
  /// corresponding group's observer.
  ///
  /// # Arguments
  ///
  /// * `key_selector` - A function that extracts a key from each item
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// Local::from_iter(0..10)
  ///   .group_by(|v| *v % 2 == 0)
  ///   .subscribe(|group| {
  ///     if group.inner.key {
  ///       group.subscribe(|v| println!("Even: {}", v));
  ///     } else {
  ///       group.subscribe(|v| println!("Odd: {}", v));
  ///     }
  ///   });
  /// ```
  #[allow(clippy::type_complexity)]
  fn group_by<'a, F, Key>(
    self, key_selector: F,
  ) -> Self::With<
    GroupBy<Self::Inner, F, Self::With<Subject<SubjectPtr<'a, Self, Self::Item<'a>, Self::Err>>>>,
  >
  where
    F: FnMut(&Self::Item<'a>) -> Key,
    Key: std::hash::Hash + Eq + Clone,
  {
    self.transform(|source| GroupBy::new(source, key_selector))
  }

  /// Split the source observable into multiple GroupedObservables for mutable
  /// references
  ///
  /// Each unique key derived from source values creates a new
  /// `GroupedObservable`. Values with the same key are forwarded to the
  /// corresponding group's observer. The values are mutable references,
  /// allowing modification.
  ///
  /// # Arguments
  ///
  /// * `key_selector` - A function that extracts a key from each item
  #[allow(clippy::type_complexity)]
  fn group_by_mut_ref<'a, F, Key, Item: 'a>(
    self, key_selector: F,
  ) -> Self::With<
    GroupBy<Self::Inner, F, Self::With<Subject<SubjectPtrMutRef<'a, Self, Item, Self::Err>>>>,
  >
  where
    Self: Observable<Item<'a> = &'a mut Item> + 'a,
    F: FnMut(&Self::Item<'a>) -> Key,
    Key: std::hash::Hash + Eq + Clone,
  {
    self.transform(|source| GroupBy::new(source, key_selector))
  }

  /// Execute a callback when an error occurs
  ///
  /// This operator allows intercepting errors for side effects while preserving
  /// the original error flow downstream.
  fn on_error<F>(self, f: F) -> Self::With<OnError<Self::Inner, F>>
  where
    F: FnOnce(Self::Err),
  {
    self.transform(|core| OnError::new(core, f))
  }

  /// Transform the error emitted by the source observable
  ///
  /// This operator intercepts an error emission from the source and maps it to
  /// a new error value/type using the provided function.
  ///
  /// # Arguments
  ///
  /// * `f` - A function that takes the original error and returns the new error
  ///
  /// # Type Parameters
  ///
  /// * `OutErr` - The new error type
  /// * `F` - The mapping function type
  ///
  /// # Examples
  ///
  /// ```rust
  /// use std::{cell::RefCell, rc::Rc};
  ///
  /// use rxrust::prelude::*;
  ///
  /// let got = Rc::new(RefCell::new(None));
  /// let got_clone = got.clone();
  ///
  /// Local::throw_err("oops")
  ///   .map_err(|e| format!("Error: {}", e))
  ///   .on_error(move |e| *got_clone.borrow_mut() = Some(e))
  ///   .subscribe(|_| {});
  ///
  /// assert_eq!(*got.borrow(), Some("Error: oops".to_string()));
  /// ```
  fn map_err<F, OutErr>(self, f: F) -> Self::With<MapErr<Self::Inner, F>>
  where
    F: FnOnce(Self::Err) -> OutErr,
  {
    self.transform(|source| MapErr { source, func: f })
  }

  /// Execute a callback when the stream completes
  ///
  /// This operator allows reacting to completion events for side effects.
  fn on_complete<F>(self, f: F) -> Self::With<OnComplete<Self::Inner, F>>
  where
    F: FnOnce(),
  {
    self.transform(|core| OnComplete::new(core, f))
  }

  /// Delay emissions by the specified duration
  ///
  /// This operator delays each emission by the specified duration using the
  /// context's scheduler. Error and completion notifications are NOT delayed.
  fn delay(self, delay: Duration) -> Self::With<Delay<Self::Inner, Self::Scheduler>> {
    let scheduler = self.scheduler().clone();
    self.transform(|core| Delay { source: core, delay, scheduler })
  }

  /// Delay emissions until the specified instant
  ///
  /// This operator delays each emission until the specified instant using the
  /// context's scheduler. If the instant is in the past, emissions happen
  /// immediately.
  fn delay_at(self, instant: Instant) -> Self::With<Delay<Self::Inner, Self::Scheduler>> {
    let now = Instant::now();
    let delay = if instant <= now { Duration::ZERO } else { instant - now };
    self.delay(delay)
  }

  /// Delay emissions by a specified duration using a custom scheduler
  ///
  /// This operator delays each emission by the specified duration using the
  /// provided scheduler, overriding the context's default scheduler.
  fn delay_with<Sch>(self, delay: Duration, scheduler: Sch) -> Self::With<Delay<Self::Inner, Sch>> {
    self.transform(|core| Delay { source: core, delay, scheduler })
  }

  /// Delay the subscription to the source observable by the specified duration
  ///
  /// This operator timeshifts the moment of subscription to the source
  /// Observable.
  fn delay_subscription(
    self, delay: Duration,
  ) -> Self::With<DelaySubscriptionOp<Self::Inner, Self::Scheduler>> {
    let scheduler = self.scheduler().clone();
    self.transform(|core| DelaySubscriptionOp { source: core, delay, scheduler })
  }

  /// Delay the subscription to the source observable until the specified
  /// instant
  fn delay_subscription_at(
    self, instant: Instant,
  ) -> Self::With<DelaySubscriptionOp<Self::Inner, Self::Scheduler>> {
    let now = Instant::now();
    let delay = if instant <= now { Duration::ZERO } else { instant - now };
    self.delay_subscription(delay)
  }

  /// Delay the subscription to the source observable by the specified duration
  /// using a custom scheduler
  fn delay_subscription_with<Sch>(
    self, delay: Duration, scheduler: Sch,
  ) -> Self::With<DelaySubscriptionOp<Self::Inner, Sch>> {
    self.transform(|core| DelaySubscriptionOp { source: core, delay, scheduler })
  }

  /// Emit a value only after a quiet period has passed
  ///
  /// Emits an item from the source Observable only after a particular duration
  /// has passed without another source emission. Each new emission resets the
  /// timer.
  ///
  /// # Arguments
  ///
  /// * `duration` - The quiet period duration after which to emit the last
  ///   value
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use rxrust::prelude::*;
  ///
  /// // Only emit after 100ms of no emissions
  /// let source = Local::from_iter([1, 2, 3, 4, 5]);
  /// source
  ///   .debounce(Duration::from_millis(100))
  ///   .subscribe(|v| println!("Debounced: {}", v));
  /// ```
  fn debounce(self, duration: Duration) -> Self::With<Debounce<Self::Inner, Self::Scheduler>> {
    let scheduler = self.scheduler().clone();
    self.transform(|core| Debounce { source: core, duration, scheduler })
  }

  /// Emit a value only after a quiet period has passed, using a custom
  /// scheduler
  ///
  /// This variant allows specifying a custom scheduler for the debounce timer.
  fn debounce_with<Sch>(
    self, duration: Duration, scheduler: Sch,
  ) -> Self::With<Debounce<Self::Inner, Sch>> {
    self.transform(|core| Debounce { source: core, duration, scheduler })
  }

  /// Re-emits all notifications from this Observable on the specified
  /// Scheduler.
  ///
  /// This operator ensures that the downstream observer's methods (`next`,
  /// `error`, `complete`) are executed in the context of the provided
  /// scheduler.
  ///
  /// # Arguments
  ///
  /// * `scheduler` - The scheduler to use for emitting notifications
  fn observe_on<Sch>(self, scheduler: Sch) -> Self::With<ObserveOn<Self::Inner, Sch>> {
    self.transform(|core| ObserveOn { source: core, scheduler })
  }

  /// Schedule the subscription (and work done during subscription) on a
  /// specified Scheduler.
  ///
  /// Unlike `observe_on` which affects where notifications are delivered,
  /// `subscribe_on` affects where the source observable's subscription logic
  /// runs.
  ///
  /// # Arguments
  ///
  /// * `scheduler` - The scheduler on which to schedule the subscription
  fn subscribe_on<Sch>(self, scheduler: Sch) -> Self::With<SubscribeOn<Self::Inner, Sch>> {
    self.transform(|core| SubscribeOn { source: core, scheduler })
  }

  /// Resubscribe to the source observable when it errors
  ///
  /// This operator resubscribes to the source observable when it emits an
  /// error. It can be used to retry failed operations.
  ///
  /// # Arguments
  ///
  /// * `policy` - The retry policy. This can be:
  ///   - `usize`: A simple retry count (e.g., `.retry(3)`).
  ///   - [`crate::ops::RetryConfig`]: A configuration object for advanced
  ///     control (delay, reset on success).
  ///   - A custom type implementing the [`RetryPolicy`] trait.
  ///
  /// # Examples
  ///
  /// Simple retry with count:
  ///
  /// ```rust,no_run
  /// use rxrust::prelude::*;
  ///
  /// Local::throw_err("fail")
  ///   .retry(3)
  ///   .on_error(|_e| {})
  ///   .subscribe(|_| {});
  /// ```
  ///
  /// Advanced retry with configuration:
  ///
  /// ```rust,no_run
  /// use rxrust::{ops::RetryConfig, prelude::*};
  ///
  /// Local::throw_err("fail")
  ///   .retry(
  ///     RetryConfig::new()
  ///       .count(3)
  ///       .delay(Duration::from_millis(100)),
  ///   )
  ///   .on_error(|_e| {})
  ///   .subscribe(|_| {});
  /// ```
  ///
  /// Custom retry policy (e.g., HTTP status code):
  ///
  /// ```rust,no_run
  /// use rxrust::{ops::retry::RetryPolicy, prelude::*};
  ///
  /// #[derive(Clone)]
  /// struct HttpRetry;
  ///
  /// impl RetryPolicy<u16> for HttpRetry {
  ///   fn should_retry(&self, err: &u16, attempt: usize) -> Option<Duration> {
  ///     match err {
  ///       500 => Some(Duration::from_millis(100)), // Retry server errors
  ///       404 => None,                             // Do not retry not found
  ///       _ => None,
  ///     }
  ///   }
  /// }
  ///
  /// Local::throw_err(500u16)
  ///   .retry(HttpRetry)
  ///   .on_error(|_e| {})
  ///   .subscribe(|_| {});
  /// ```
  fn retry<P>(self, policy: P) -> Self::With<Retry<Self::Inner, P, Self::Scheduler>>
  where
    P: RetryPolicy<Self::Err>,
  {
    let scheduler = self.scheduler().clone();
    self.transform(|source| Retry { source, policy, scheduler })
  }

  /// Throttle emissions by ignoring values during a window
  ///
  /// Each emitted value starts a throttle window. While the
  /// window is active, additional source values are suppressed.
  ///
  /// The window is closed by a notifier Observable returned by `selector`.
  /// The first `next` (or `complete`) from the notifier ends the window.
  ///
  /// If `edge.trailing` is enabled, the last suppressed value is emitted when
  /// the window ends. If a trailing value is emitted, it starts a new window.
  ///
  /// Completion semantics: if the source completes while a window is active
  /// and a trailing value is pending, completion is delayed until the window
  /// ends and the trailing value is emitted.
  ///
  /// # Arguments
  ///
  /// * `selector` - A function that takes a reference to an item and returns a
  ///   notifier Observable that closes the throttle window
  /// * `edge` - Configuration for leading/trailing behavior
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use rxrust::prelude::*;
  ///
  /// // Throttle with a dynamic notifier based on value
  /// let source = Local::from_iter([1u64, 2, 3, 4, 5]);
  /// source
  ///   .throttle(|v| Local::timer(Duration::from_millis(*v * 10)), ThrottleEdge::leading())
  ///   .subscribe(|v| println!("Throttled: {}", v));
  /// ```
  fn throttle<F, Out>(
    self, selector: F, edge: ThrottleEdge,
  ) -> Self::With<Throttle<Self::Inner, ThrottleWhenParam<F>>>
  where
    F: for<'a> FnMut(&Self::Item<'a>) -> Out,
    Out: Observable<Err = Self::Err>,
  {
    self.transform(|core| Throttle { source: core, param: ThrottleWhenParam { selector }, edge })
  }

  /// Throttle emissions with a fixed duration
  ///
  /// Time-based convenience wrapper around notifier-based throttle.
  ///
  /// # Arguments
  ///
  /// * `duration` - The throttle window duration
  /// * `edge` - Configuration for leading/trailing behavior
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use rxrust::prelude::*;
  ///
  /// // Emit first value, then ignore for 100ms
  /// let source = Local::from_iter([1, 2, 3, 4, 5]);
  /// source
  ///   .throttle_time(Duration::from_millis(100), ThrottleEdge::leading())
  ///   .subscribe(|v| println!("Throttled: {}", v));
  /// ```
  fn throttle_time(
    self, duration: Duration, edge: ThrottleEdge,
  ) -> Self::With<Throttle<Self::Inner, Self::With<Duration>>>
  where
    Self::With<Duration>: Context<Scheduler = Self::Scheduler>,
  {
    let scheduler = self.scheduler().clone();
    self.throttle_time_with(duration, edge, scheduler)
  }

  /// Throttle emissions with a fixed duration using a custom scheduler
  ///
  /// This variant allows specifying a custom scheduler for the throttle timer.
  fn throttle_time_with<Sch>(
    self, duration: Duration, edge: ThrottleEdge, scheduler: Sch,
  ) -> Self::With<Throttle<Self::Inner, Self::With<Duration>>>
  where
    Self::With<Duration>: Context<Scheduler = Sch>,
  {
    self.transform(move |core| Throttle {
      source: core,
      param: Self::With::from_parts(duration, scheduler),
      edge,
    })
  }

  /// Emit the most recently emitted value when a sampler Observable emits
  ///
  /// Whenever the sampler Observable emits a value, emit the most recently
  /// emitted value from the source Observable. Also emits the stored value
  /// when the sampler completes.
  ///
  /// # Arguments
  ///
  /// * `sampler` - The observable that controls when to sample the source
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let mut source = Local::subject();
  /// let mut sampler = Local::subject();
  ///
  /// let source_sub = source.clone();
  /// let sampler_sub = sampler.clone();
  ///
  /// source_sub
  ///   .sample(sampler_sub)
  ///   .subscribe(|v| println!("Sampled: {}", v));
  ///
  /// source.next(1);
  /// source.next(2);
  /// sampler.next(()); // Emits 2
  /// source.next(3);
  /// sampler.next(()); // Emits 3
  /// ```
  fn sample<N>(self, sampler: N) -> Self::With<Sample<Self::Inner, N::Inner>>
  where
    N: Observable<Err = Self::Err, Inner: ObservableType>,
  {
    self.transform(|source| Sample { source, sampler: sampler.into_inner() })
  }

  /// Combine items from two observables pairwise
  ///
  /// Zip combines items from this observable with items from another observable
  /// pairwise. It buffers items from each source and emits a tuple `(ItemA,
  /// ItemB)` when both sources have emitted a value. The stream completes
  /// when both sources complete.
  ///
  /// # Arguments
  ///
  /// * `other` - The second observable to zip with
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// Local::from_iter([1, 2, 3])
  ///   .zip(Local::from_iter([4, 5, 6]))
  ///   .subscribe(|(a, b)| println!("({}, {})", a, b));
  /// // Prints: (1, 4), (2, 5), (3, 6)
  /// ```
  fn zip<B>(self, other: B) -> Self::With<Zip<Self::Inner, B::Inner>>
  where
    B: Observable<Err = Self::Err, Inner: ObservableType>,
  {
    self.transform(|source_a| Zip { source_a, source_b: other.into_inner() })
  }

  /// Perform a side effect for each emission
  ///
  /// The `tap` operator allows you to perform side effects for each value
  /// emitted by the source observable, without modifying the values themselves.
  /// It's useful for debugging, logging, or triggering external actions.
  ///
  /// # Arguments
  ///
  /// * `f` - A function that takes a reference to each emitted value
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// Local::from_iter([1, 2, 3])
  ///   .tap(|v| println!("Got value: {}", v))
  ///   .subscribe(|_| {});
  /// ```
  fn tap<F>(self, f: F) -> Self::With<Tap<Self::Inner, F>>
  where
    F: for<'a> FnMut(&Self::Item<'a>),
  {
    self.transform(|source| Tap { source, func: f })
  }

  /// Execute cleanup logic when the Observable terminates
  ///
  /// The `finalize` operator calls a function when the Observable completes,
  /// errors, or is unsubscribed. The function is guaranteed to be called
  /// exactly once, regardless of how the Observable terminates.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use std::{cell::Cell, rc::Rc};
  ///
  /// use rxrust::prelude::*;
  ///
  /// let finalized = Rc::new(Cell::new(false));
  /// let finalized_clone = finalized.clone();
  ///
  /// Local::of(1)
  ///   .finalize(move || finalized_clone.set(true))
  ///   .subscribe(|_| {});
  ///
  /// assert!(finalized.get());
  /// ```
  fn finalize<F>(self, f: F) -> Self::With<Finalize<Self::Inner, F>>
  where
    F: FnOnce(),
  {
    self.transform(|source| Finalize { source, func: f })
  }

  /// Emit specified values before beginning to emit source values
  ///
  /// The `start_with` operator prepends the provided values to the source
  /// Observable. The values are emitted synchronously before subscribing
  /// to the source.
  ///
  /// # Arguments
  ///
  /// * `values` - A Vec of items to emit before the source items
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// Local::from_iter([3, 4, 5])
  ///   .start_with(vec![1, 2])
  ///   .subscribe(|v| println!("{}", v));
  /// // Prints: 1, 2, 3, 4, 5
  /// ```
  fn start_with<Item>(self, values: Vec<Item>) -> Self::With<StartWith<Self::Inner, Item>> {
    self.transform(|source| StartWith { source, values })
  }

  /// Emit a default value if the observable completes without emitting any
  /// items
  ///
  /// The `default_if_empty` operator emits a default value if the source
  /// Observable completes without emitting any items. If the source emits
  /// any items, the default value is not emitted.
  ///
  /// # Arguments
  ///
  /// * `default_value` - The value to emit if the source is empty
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// Local::empty()
  ///   .map_to(0)
  ///   .default_if_empty(42)
  ///   .subscribe(|v| println!("{}", v));
  /// // Prints: 42
  /// ```
  fn default_if_empty<'a>(
    self, default_value: Self::Item<'a>,
  ) -> Self::With<DefaultIfEmpty<Self::Inner, Self::Item<'a>>> {
    self.transform(|source| DefaultIfEmpty::new(source, default_value))
  }

  /// Collect all emitted items into a collection
  ///
  /// The `collect` operator accumulates all items emitted by the source
  /// Observable into a collection. When the source completes, the collection
  /// is emitted as a single value.
  ///
  /// If the source errors, the error is propagated without emitting the
  /// collection.
  ///
  /// # Type Parameters
  ///
  /// * `C` - The collection type, must implement `Default` and `Extend<Item>`
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// Local::from_iter([1, 2, 3])
  ///   .collect::<Vec<_>>()
  ///   .subscribe(|v| println!("{:?}", v));
  /// // Prints: [1, 2, 3]
  /// ```
  fn collect<C>(self) -> Self::With<Collect<Self::Inner, C>>
  where
    C: Default,
  {
    self.collect_into(C::default())
  }

  /// Collect all emitted items into an existing collection
  ///
  /// Similar to `collect`, but starts with a pre-populated collection.
  /// New items are appended to the provided initial collection.
  ///
  /// # Arguments
  ///
  /// * `initial` - The initial collection to start with
  ///
  /// # Type Parameters
  ///
  /// * `C` - The collection type, must implement `Extend<Item>`
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// Local::from_iter([4, 5, 6])
  ///   .collect_into::<Vec<_>>(vec![1, 2, 3])
  ///   .subscribe(|v| println!("{:?}", v));
  /// // Prints: [1, 2, 3, 4, 5, 6]
  /// ```
  fn collect_into<C>(self, initial: C) -> Self::With<Collect<Self::Inner, C>> {
    self.transform(|source| Collect { source, collection: initial })
  }

  /// Buffer items until a notifier observable emits
  ///
  /// Collects items from the source observable into a Vec. When the notifier
  /// emits, the buffered items are emitted as a Vec and the buffer is cleared.
  /// When the source completes, any remaining items are emitted.
  ///
  /// # Arguments
  ///
  /// * `notifier` - The observable that triggers buffer emission
  ///
  /// # Examples
  ///
  /// ```rust
  /// use std::convert::Infallible;
  ///
  /// use rxrust::prelude::*;
  ///
  /// let notifier = Local::subject::<(), Infallible>();
  /// let source = Local::subject::<i32, Infallible>();
  ///
  /// source
  ///   .buffer(notifier)
  ///   .subscribe(|v| println!("{:?}", v));
  /// ```
  fn buffer<N>(self, notifier: N) -> Self::With<Buffer<Self::Inner, N::Inner>>
  where
    N: Observable<Err = Self::Err, Inner: ObservableType>,
  {
    self.transform(|source| Buffer { source, notifier: notifier.into_inner() })
  }

  /// Buffer items until a count is reached
  ///
  /// Collects items from the source observable into a Vec. When the count
  /// is reached, the buffered items are emitted as a Vec and the buffer is
  /// cleared. When the source completes, any remaining items are emitted.
  ///
  /// # Arguments
  ///
  /// * `count` - The number of items to buffer before emitting
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// Local::from_iter([1, 2, 3, 4, 5])
  ///   .buffer_count(2)
  ///   .subscribe(|v| println!("{:?}", v));
  /// // Prints: [1, 2], [3, 4], [5]
  /// ```
  fn buffer_count(self, count: usize) -> Self::With<BufferCount<Self::Inner>> {
    self.transform(|source| BufferCount { source, count })
  }

  /// Buffer items for a time window
  ///
  /// Collects items from the source observable into a Vec. When the time
  /// window expires, the buffered items are emitted as a Vec and a new
  /// window starts. When the source completes, any remaining items are emitted.
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use rxrust::prelude::*;
  ///
  /// let source = Local::from_iter([1, 2, 3, 4, 5]);
  /// source
  ///   .buffer_time(Duration::from_millis(100))
  ///   .subscribe(|v| println!("{:?}", v));
  /// ```
  fn buffer_time(self, duration: Duration) -> Self::With<BufferTime<Self::Inner, Self::Scheduler>> {
    let scheduler = self.scheduler().clone();
    self.transform(|source| BufferTime { source, duration, max_buffer_size: None, scheduler })
  }

  /// Buffer items until time OR max count is reached
  ///
  /// The buffer is emitted when either the time window expires or the max
  /// buffer size is reached, whichever comes first.
  ///
  /// # Examples
  ///
  /// ```rust,no_run
  /// use rxrust::prelude::*;
  ///
  /// let source = Local::from_iter([1, 2, 3, 4, 5]);
  /// source
  ///   .buffer_time_max(Duration::from_millis(100), 10)
  ///   .subscribe(|v| println!("{:?}", v));
  /// ```
  fn buffer_time_max(
    self, duration: Duration, max_buffer_size: usize,
  ) -> Self::With<BufferTime<Self::Inner, Self::Scheduler>> {
    let scheduler = self.scheduler().clone();
    self.transform(|source| BufferTime {
      source,
      duration,
      max_buffer_size: Some(max_buffer_size),
      scheduler,
    })
  }

  /// Buffers items emitted by the source observable for a specified time
  /// window, using a custom scheduler for timing control.
  ///
  /// When the time window expires, the buffered items are emitted as a `Vec<T>`
  /// and a new buffer window starts. This variant allows you to provide your
  /// own scheduler for precise timing control (e.g., `TestScheduler` for
  /// testing).
  ///
  /// # Arguments
  /// * `duration` - The time span for each buffer window
  /// * `scheduler` - The scheduler to use for timing the buffer windows
  ///
  /// # Example
  /// ```ignore
  /// observable
  ///   .buffer_time_with(Duration::from_millis(100), my_scheduler)
  ///   .subscribe(|buffer| println!("Buffered: {:?}", buffer));
  /// ```
  ///
  /// # See Also
  /// * [`buffer_time`] - Uses the default scheduler
  /// * [`buffer_time_max_with`] - Adds a max buffer size constraint
  fn buffer_time_with<Sch>(
    self, duration: Duration, scheduler: Sch,
  ) -> Self::With<BufferTime<Self::Inner, Sch>> {
    self.transform(|source| BufferTime { source, duration, max_buffer_size: None, scheduler })
  }

  /// Buffers items emitted by the source observable until either the time
  /// window expires OR the maximum buffer size is reached, whichever comes
  /// first. Uses a custom scheduler for timing control.
  ///
  /// This is useful when you want to limit memory usage by capping the buffer
  /// size while still ensuring timely delivery of smaller batches.
  ///
  /// # Arguments
  /// * `duration` - The maximum time span for each buffer window
  /// * `max_buffer_size` - The maximum number of items per buffer; when
  ///   reached, the buffer is emitted immediately and a new window starts
  /// * `scheduler` - The scheduler to use for timing the buffer windows
  ///
  /// # Example
  /// ```ignore
  /// // Emit buffer every 100ms OR when 5 items are collected
  /// observable
  ///   .buffer_time_max_with(Duration::from_millis(100), 5, my_scheduler)
  ///   .subscribe(|buffer| println!("Buffered: {:?}", buffer));
  /// ```
  ///
  /// # See Also
  /// * [`buffer_time_max`] - Uses the default scheduler
  /// * [`buffer_time_with`] - Without max buffer size constraint
  fn buffer_time_max_with<Sch>(
    self, duration: Duration, max_buffer_size: usize, scheduler: Sch,
  ) -> Self::With<BufferTime<Self::Inner, Sch>> {
    self.transform(|source| BufferTime {
      source,
      duration,
      max_buffer_size: Some(max_buffer_size),
      scheduler,
    })
  }

  /// Convert this observable into a ConnectableObservable using the specified
  /// subject
  ///
  /// This operator creates a ConnectableObservable that will multicast values
  /// from this observable through the provided subject. The connection must
  /// be established explicitly via the `connect()` method on the resulting
  /// ConnectableObservable.
  ///
  /// # Multicasting Behavior
  ///
  /// - The source observable is subscribed to only once when `connect()` is
  ///   called
  /// - Values are broadcast to all subscribed observers
  /// - Requires `Item: Clone` for value broadcasting
  /// - Multiple observers share the same subscription to the source
  ///
  /// # Arguments
  ///
  /// * `subject` - The subject that will multicast values to observers
  ///
  /// # Returns
  ///
  /// A ConnectableObservable wrapped in the same Context. Use `fork()` to
  /// create Observable instances and `connect()` to start multicasting.
  ///
  /// # Example
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let source = Local::of(1).merge(Local::of(2));
  /// let subject = Local::subject();
  /// let connectable = source.multicast(subject.into_inner());
  ///
  /// // Subscribe multiple observers (they will receive cloned values)
  /// let obs1 = connectable.fork();
  /// let obs2 = connectable.fork();
  /// obs1.subscribe(|v| println!("Observer 1: {}", v));
  /// obs2.subscribe(|v| println!("Observer 2: {}", v));
  ///
  /// // Start multicasting - source is subscribed to once
  /// let connection = connectable.connect();
  ///
  /// // Later, disconnect to stop multicasting
  /// connection.unsubscribe();
  /// ```
  fn multicast<'a>(
    self, subject: Subject<SubjectPtr<'a, Self, Self::Item<'a>, Self::Err>>,
  ) -> ConnectableObservableCtx<'a, Self> {
    self.transform(|source| ConnectableObservable { source, subject })
  }

  /// Convert this observable into a ConnectableObservable using the specified
  /// subject for mutable reference broadcasting
  ///
  /// This operator creates a ConnectableObservable that will multicast mutable
  /// references from this observable through the provided subject. The
  /// connection must be established explicitly via the `connect()` method on
  /// the resulting ConnectableObservable.
  ///
  /// # Mutable Reference Broadcasting
  ///
  /// - Uses Higher-Rank Trait Bounds (HRTB) to allow sequential modification
  /// - No `Clone` requirement on `Item` type
  /// - Observers receive `&mut Item` and can modify values
  /// - Multiple observers can modify the same value sequentially
  /// - Source observable is subscribed to only once when `connect()` is called
  ///
  /// # Arguments
  ///
  /// * `subject` - The subject that will multicast mutable references to
  ///   observers
  ///
  /// # Returns
  ///
  /// A ConnectableObservable wrapped in the same Context.
  ///
  /// # Example
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let source = Local::subject_mut_ref();
  /// let subject = Local::subject_mut_ref();
  /// let connectable = source.multicast_mut_ref(subject.into_inner());
  ///
  /// // Subscribe multiple observers that can modify values sequentially
  /// connectable
  ///   .fork()
  ///   .subscribe(|v: &mut i32| *v += 1);
  /// connectable
  ///   .fork()
  ///   .subscribe(|v: &mut i32| *v *= 2);
  ///
  /// // Start multicasting - source is subscribed to once
  /// let connection = connectable.connect();
  ///
  /// // Later, disconnect to stop multicasting
  /// connection.unsubscribe();
  /// ```
  #[allow(clippy::type_complexity)]
  fn multicast_mut_ref<'a, Item: 'a>(
    self, subject: Subject<SubjectPtrMutRef<'a, Self, Item, Self::Err>>,
  ) -> ConnectableObservableCtxMutRef<'a, Self, Item>
  where
    Self: Observable<Item<'a> = &'a mut Item> + 'a,
  {
    self.transform(|source| ConnectableObservable { source, subject })
  }

  /// Convert this observable into a ConnectableObservable for value
  /// broadcasting
  ///
  /// This operator creates a ConnectableObservable that will multicast values
  /// from this observable through a subject. The connection must
  /// be established explicitly via the `connect()` method on the resulting
  /// ConnectableObservable.
  ///
  /// This is equivalent to `multicast(Subject::default())` and provides
  /// a convenient shorthand for creating a standard hot observable.
  ///
  /// # Value Broadcasting Behavior
  ///
  /// - New subscribers will only receive values emitted **after** they
  ///   subscribe
  /// - No historical values are replayed
  /// - Multiple observers share the same subscription to the source
  /// - Requires `Item: Clone` for broadcasting values to multiple observers
  /// - Source observable is subscribed to only once when `connect()` is called
  ///
  /// # Returns
  ///
  /// A ConnectableObservable wrapped in the same Context.
  ///
  /// # Example
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let source = Local::of(1).merge(Local::of(2));
  /// let connectable = source.publish();
  ///
  /// // Subscribe multiple observers
  /// let obs1 = connectable.fork();
  /// let obs2 = connectable.fork();
  /// obs1.subscribe(|v| println!("Observer 1: {}", v));
  /// obs2.subscribe(|v| println!("Observer 2: {}", v));
  ///
  /// // Start multicasting - source is subscribed to once
  /// let connection = connectable.connect();
  ///
  /// // Later, disconnect to stop multicasting
  /// connection.unsubscribe();
  /// ```
  fn publish<'a>(self) -> ConnectableObservableCtx<'a, Self> { self.multicast(Subject::default()) }

  /// Convert this observable into a ConnectableObservable for mutable reference
  /// broadcasting
  ///
  /// This operator creates a ConnectableObservable that will multicast mutable
  /// references from this observable through a subject. The connection must
  /// be established explicitly via the `connect()` method on the resulting
  /// ConnectableObservable.
  ///
  /// This is equivalent to `multicast_mut_ref(Subject::default())` and provides
  /// a convenient shorthand for creating a standard hot observable that
  /// supports sequential modification via re-borrowing.
  ///
  /// # Mutable Reference Broadcasting Behavior
  ///
  /// - Uses Higher-Rank Trait Bounds (HRTB) to allow sequential modification
  /// - Subscribers receive `&mut Item` and can modify values
  /// - Multiple observers can modify the same value sequentially
  /// - No `Clone` requirement on `Item` type
  /// - No historical values are replayed
  /// - Source observable is subscribed to only once when `connect()` is called
  ///
  /// # Returns
  ///
  /// A ConnectableObservable wrapped in the same Context.
  ///
  /// # Example
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let source = Local::subject_mut_ref();
  /// let connectable = source.publish_mut_ref();
  ///
  /// // Subscribe multiple observers that can modify values sequentially
  /// let obs1 = connectable.fork();
  /// let obs2 = connectable.fork();
  /// obs1.subscribe(|v: &mut i32| *v += 1);
  /// obs2.subscribe(|v: &mut i32| *v *= 2);
  ///
  /// // Start multicasting - source is subscribed to once
  /// let connection = connectable.connect();
  ///
  /// // Later, disconnect to stop multicasting
  /// connection.unsubscribe();
  /// ```
  #[allow(clippy::type_complexity)]
  fn publish_mut_ref<'a, Item: 'a>(
    self,
  ) -> Self::With<ConnectableObservable<Self::Inner, SubjectPtrMutRef<'a, Self, Item, Self::Err>>>
  where
    Self: Observable<Item<'a> = &'a mut Item> + 'a,
  {
    self.multicast_mut_ref(Subject::default())
  }

  /// Type-erases a local observable into a boxed observable.
  ///
  /// This enables storing observables in collections or returning them from
  /// functions without complex generic types. All type information except
  /// `Item` and `Err` is erased.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// // Different source types become the same boxed type
  /// let boxed1 = Local::of(1).box_it();
  /// let boxed2 = Local::from_iter([2, 3]).map(|x| x * 2).box_it();
  ///
  /// // Store in a collection
  /// let observables = vec![boxed1, boxed2];
  /// ```
  fn box_it<'a, 'b>(self) -> Self::With<Self::BoxedCoreObservable<'a, Self::Item<'b>, Self::Err>>
  where
    Self::Inner: IntoBoxedCoreObservable<Self::BoxedCoreObservable<'a, Self::Item<'b>, Self::Err>>,
  {
    self.transform(|inner| inner.into_boxed())
  }

  /// Type-erases a local/shared observable into a *cloneable* boxed observable.
  ///
  /// This is only available when the underlying observable pipeline is
  /// `Clone` (enforced via trait bounds on the boxing target).
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// let boxed = Local::of(42).box_it_clone();
  /// let boxed2 = boxed.clone();
  ///
  /// boxed.subscribe(|v| assert_eq!(v, 42));
  /// boxed2.subscribe(|v| assert_eq!(v, 42));
  /// ```
  fn box_it_clone<'a, 'b>(
    self,
  ) -> Self::With<Self::BoxedCoreObservableClone<'a, Self::Item<'b>, Self::Err>>
  where
    Self::Inner:
      IntoBoxedCoreObservable<Self::BoxedCoreObservableClone<'a, Self::Item<'b>, Self::Err>>,
  {
    self.transform(|inner| inner.into_boxed())
  }

  /// Type-erases the observable into a boxed mutable reference observable.
  ///
  /// This enables type erasure for observables that emit mutable references,
  /// useful for mut_ref broadcasting patterns.
  fn box_it_mut_ref<'a, Item>(
    self,
  ) -> Self::With<Self::BoxedCoreObservableMutRef<'a, Item, Self::Err>>
  where
    Self: 'a,
    Self::Item<'a>: std::ops::DerefMut<Target = Item> + 'a,
    Self::Inner: IntoBoxedCoreObservable<Self::BoxedCoreObservableMutRef<'a, Item, Self::Err>>,
  {
    self.transform(|inner| inner.into_boxed())
  }

  /// Type-erases the observable into a *cloneable* boxed mutable reference
  /// observable.
  ///
  /// This is only available when the underlying observable pipeline is
  /// `Clone` (enforced via trait bounds on the boxing target).
  ///
  /// # Examples
  ///
  /// ```rust
  /// use std::convert::Infallible;
  ///
  /// use rxrust::prelude::*;
  ///
  /// // Choose `Item = i32` so the observable emits `&mut i32`.
  /// let subject = Local::subject_mut_ref::<i32, Infallible>();
  /// let boxed = subject.clone().box_it_mut_ref_clone();
  /// let boxed2 = boxed.clone();
  ///
  /// boxed.subscribe(|_v: &mut i32| {});
  /// boxed2.subscribe(|_v: &mut i32| {});
  /// ```
  fn box_it_mut_ref_clone<'a, Item>(
    self,
  ) -> Self::With<Self::BoxedCoreObservableMutRefClone<'a, Item, Self::Err>>
  where
    Self: 'a,
    Self::Item<'a>: std::ops::DerefMut<Target = Item> + 'a,
    Self::Inner: IntoBoxedCoreObservable<Self::BoxedCoreObservableMutRefClone<'a, Item, Self::Err>>,
  {
    self.transform(|inner| inner.into_boxed())
  }

  /// Convert this observable into a Future that resolves with the last emitted
  /// value.
  ///
  /// This method subscribes to the observable and returns a future that
  /// resolves when the observable completes. It supports both synchronous and
  /// asynchronous observables.
  ///
  /// # Returns
  ///
  /// - `Ok(Ok(value))` - Observable emitted exactly one value
  /// - `Ok(Err(error))` - Observable emitted an error
  /// - `Err(IntoFutureError::Empty)` - Observable completed without emitting
  ///   any values
  /// - `Err(IntoFutureError::MultipleValues)` - Observable emitted more than
  ///   one value
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rxrust::prelude::*;
  ///
  /// #[cfg(not(target_arch = "wasm32"))]
  /// #[tokio::main]
  /// async fn main() {
  ///   // Single value
  ///   let fut = Local::of(42).into_future();
  ///   let value = fut.await.unwrap().ok().unwrap();
  ///   assert_eq!(value, 42);
  ///
  ///   // Empty observable
  ///   let fut = Local::from_iter(std::iter::empty::<i32>()).into_future();
  ///   assert!(matches!(fut.await, Err(IntoFutureError::Empty)));
  ///
  ///   // Multiple values
  ///   let fut = Local::from_iter([1, 2, 3]).into_future();
  ///   assert!(matches!(fut.await, Err(IntoFutureError::MultipleValues)));
  /// }
  /// #[cfg(target_arch = "wasm32")]
  /// fn main() {}
  /// ```
  fn into_future<'a>(self) -> ObservableFuture<Self::Item<'a>, Self::Err>
  where
    Self::Inner: SupportsIntoFuture<'a, Self>,
  {
    <Self::Inner as SupportsIntoFuture<'a, Self>>::into_future(self)
  }

  /// Convert this observable into a `futures_core::stream::Stream`.
  ///
  /// This method transforms the observable into an asynchronous stream,
  /// allowing you to iterate over the emitted values using standard async
  /// patterns like `while let` or stream combinators.
  ///
  /// The stream yields items of type `Result<Self::Item, Self::Err>`.
  ///
  /// # Requirements
  ///
  /// * The observable items must be `'static` (owned values) to safely cross
  ///   async boundaries.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use futures::stream::StreamExt;
  /// use rxrust::prelude::*;
  ///
  /// async fn test_stream() {
  ///   let mut stream = Local::of(42).into_stream();
  ///
  ///   while let Some(result) = stream.next().await {
  ///     match result {
  ///       Ok(value) => println!("Got value: {}", value),
  ///       Err(e) => println!("Error: {:?}", e),
  ///     }
  ///   }
  /// }
  /// ```
  fn into_stream<'a>(self) -> <Self::Inner as SupportsIntoStream<'a, Self>>::Stream
  where
    Self::Inner: SupportsIntoStream<'a, Self>,
  {
    <Self::Inner as SupportsIntoStream<'a, Self>>::into_stream(self)
  }

  /// Apply an accumulator function and emit only the final result
  ///
  /// This operator applies an accumulator function over the source Observable
  /// and returns only the final accumulated value when the source completes.
  /// If the source is empty, nothing is emitted.
  ///
  /// This is equivalent to `scan(Default::default(), f).last()`.
  ///
  /// # Arguments
  ///
  /// * `f` - A function that takes the current accumulator and a value, and
  ///   returns the new accumulator
  ///
  /// # Type Parameters
  ///
  /// * `Output` - The type of the accumulator value (must implement `Default`
  ///   and `Clone`)
  /// * `F` - The accumulator function type
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// let mut result = 0;
  /// Local::from_iter([1, 2, 3, 4, 5])
  ///   .reduce(|acc: i32, v| acc + v)
  ///   .subscribe(|v| result = v);
  /// assert_eq!(result, 15);
  ///
  /// // On empty observable, nothing is emitted
  /// let mut emitted = false;
  /// Local::from_iter(std::iter::empty::<i32>())
  ///   .reduce(|acc: i32, v| acc + v)
  ///   .subscribe(|_| emitted = true);
  /// assert!(!emitted);
  /// ```
  #[allow(clippy::type_complexity)]
  fn reduce<'a, F>(self, f: F) -> Self::With<Reduce<Self::Inner, ReduceFn<F>, Self::Item<'a>>>
  where
    F: FnMut(Self::Item<'a>, Self::Item<'a>) -> Self::Item<'a>,
  {
    self.transform(|source| Reduce { source, strategy: ReduceFn(f), initial: None })
  }

  /// Apply an accumulator function with an initial value and emit the final
  /// result
  ///
  /// This operator applies an accumulator function over the source Observable,
  /// starting with an initial value, and returns the final accumulated value
  /// when the source completes. If the source is empty, the initial value is
  /// emitted.
  ///
  /// This is equivalent to `scan(initial, f).last().default_if_empty(initial)`.
  ///
  /// # Arguments
  ///
  /// * `initial` - The initial accumulator value
  /// * `f` - A function that takes the current accumulator and a value, and
  ///   returns the new accumulator
  ///
  /// # Type Parameters
  ///
  /// * `Output` - The type of the accumulator value
  /// * `F` - The accumulator function type
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// let mut result = 0;
  /// Local::from_iter([1, 1, 1, 1, 1])
  ///   .reduce_initial(100, |acc, v| acc + v)
  ///   .subscribe(|v| result = v);
  /// assert_eq!(result, 105);
  ///
  /// // On empty observable, the initial value is emitted
  /// let mut result = 0;
  /// Local::from_iter(std::iter::empty::<i32>())
  ///   .reduce_initial(100, |acc, v| acc + v)
  ///   .subscribe(|v| result = v);
  /// assert_eq!(result, 100);
  /// ```
  fn reduce_initial<Output, F>(
    self, initial: Output, f: F,
  ) -> Self::With<Reduce<Self::Inner, ReduceInitialFn<F>, Output>>
  where
    F: for<'a> FnMut(Output, Self::Item<'a>) -> Output,
  {
    self.transform(|source| Reduce { source, strategy: ReduceInitialFn(f), initial: Some(initial) })
  }

  /// Emit the maximum item emitted by the source observable.
  ///
  /// **Empty source:** emits nothing.
  ///
  /// **Note:** This operator compares and returns owned items
  /// (`Item<'static>`).
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// let mut out = None;
  /// Local::from_iter([3, 1, 4])
  ///   .max()
  ///   .subscribe(|v| out = Some(v));
  /// assert_eq!(out, Some(4));
  /// ```
  #[allow(clippy::type_complexity)]
  fn max<'a>(
    self,
  ) -> Self::With<
    Reduce<
      Self::Inner,
      ReduceFn<fn(Self::Item<'a>, Self::Item<'a>) -> Self::Item<'a>>,
      Self::Item<'a>,
    >,
  >
  where
    Self::Item<'a>: PartialOrd,
  {
    self.reduce(|acc, v| if v > acc { v } else { acc })
  }

  /// Emit the minimum item emitted by the source observable.
  ///
  /// **Empty source:** emits nothing.
  ///
  /// **Note:** This operator compares and returns owned items
  /// (`Item<'static>`).
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// let mut out = None;
  /// Local::from_iter([3, 1, 4])
  ///   .min()
  ///   .subscribe(|v| out = Some(v));
  /// assert_eq!(out, Some(1));
  /// ```
  #[allow(clippy::type_complexity)]
  fn min<'a>(
    self,
  ) -> Self::With<
    Reduce<
      Self::Inner,
      ReduceFn<fn(Self::Item<'a>, Self::Item<'a>) -> Self::Item<'a>>,
      Self::Item<'a>,
    >,
  >
  where
    Self::Item<'a>: PartialOrd,
  {
    self.reduce(|acc, v| if v < acc { v } else { acc })
  }

  /// Emit the sum of items emitted by the source observable.
  ///
  /// **Empty source:** emits the additive identity (via `Default`).
  ///
  /// # Type requirements
  ///
  /// Items must implement `Default` and `Add<Output = Self>`.
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// let mut out = 0;
  /// Local::from_iter([1, 2, 3])
  ///   .sum()
  ///   .subscribe(|v| out = v);
  /// assert_eq!(out, 6);
  /// ```
  #[allow(clippy::type_complexity)]
  fn sum<'a>(
    self,
  ) -> Self::With<
    Reduce<
      Self::Inner,
      ReduceInitialFn<fn(Self::Item<'a>, Self::Item<'a>) -> Self::Item<'a>>,
      Self::Item<'a>,
    >,
  >
  where
    Self::Item<'a>: Default + std::ops::Add<Output = Self::Item<'a>>,
  {
    self.transform(|source| Reduce {
      source,
      strategy: ReduceInitialFn((|acc, v| acc + v) as _),
      initial: Some(Default::default()),
    })
  }

  /// Count the number of items emitted by the source observable.
  ///
  /// **Empty source:** emits `0`.
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// let mut out = 0;
  /// Local::from_iter(['a', 'b', 'c'])
  ///   .count()
  ///   .subscribe(|v| out = v);
  /// assert_eq!(out, 3);
  /// ```
  #[allow(clippy::type_complexity)]
  fn count(
    self,
  ) -> Self::With<
    Reduce<Self::Inner, ReduceInitialFn<for<'a> fn(usize, Self::Item<'a>) -> usize>, usize>,
  > {
    self.transform(|source| Reduce {
      source,
      strategy: ReduceInitialFn(
        (|acc, _v| acc + 1usize) as for<'a> fn(usize, Self::Item<'a>) -> usize,
      ),
      initial: Some(0),
    })
  }

  /// Emit the average of items emitted by the source observable
  ///
  /// This operator calculates the average of all items emitted by the source
  /// observable and emits the result when the source completes.
  ///
  /// # Requirements
  ///
  /// * `Item` must implement [`Averageable`] (e.g., `f32`, `f64`).
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// // Average of floats
  /// let mut result_f = 0.0;
  /// Local::from_iter([1.0, 2.0, 3.0])
  ///   .average()
  ///   .subscribe(|v| result_f = v);
  /// assert_eq!(result_f, 2.0);
  ///
  /// // Average of integers
  /// let mut result_i = 0;
  /// Local::from_iter([1, 2, 3, 4, 5])
  ///   .average()
  ///   .subscribe(|v| result_i = v);
  /// assert_eq!(result_i, 3);
  ///
  /// // Empty observable
  /// let mut emitted_empty = false;
  /// Local::from_iter(std::iter::empty::<f64>())
  ///   .average()
  ///   .subscribe(|_| emitted_empty = true);
  /// assert!(!emitted_empty);
  /// ```
  fn average(self) -> Self::With<Average<Self::Inner>>
  where
    for<'a> Self::Item<'a>: Averageable,
  {
    self.transform(Average::new)
  }

  /// Map each item into an inner observable and merge the results.
  ///
  /// This is equivalent to `map(f).merge_all(usize::MAX)`.
  ///
  /// - Subscribes to inner observables eagerly (unlimited concurrency).
  /// - Does **not** guarantee ordering across inners.
  /// - Errors from either the outer or any inner observable terminate
  ///   downstream.
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// let mut out = Vec::new();
  /// Local::from_iter([1_i32, 2_i32])
  ///   .flat_map(|x| Local::from_iter([x, x + 10]))
  ///   .subscribe(|v| out.push(v));
  /// assert_eq!(out, vec![1, 11, 2, 12]);
  /// ```
  fn flat_map<F, Inner>(self, f: F) -> Self::With<FlatMap<Self::Inner, F, Inner>>
  where
    F: for<'a> FnMut(Self::Item<'a>) -> Inner,
    Inner: Context<Inner: ObservableType<Err = Self::Err>>,
  {
    self.transform(|source| FlatMap::new(source, f, usize::MAX))
  }

  /// Map each item into an inner observable and concatenate the results.
  ///
  /// This is equivalent to `map(f).concat_all()`.
  ///
  /// - Subscribes to inner observables sequentially (concurrency = 1).
  /// - Preserves ordering between inners.
  /// - Errors from either the outer or any inner observable terminate
  ///   downstream.
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// let mut out = Vec::new();
  /// Local::from_iter([1_i32, 2_i32])
  ///   .concat_map(|x| Local::from_iter([x, x + 10]))
  ///   .subscribe(|v| out.push(v));
  /// assert_eq!(out, vec![1, 11, 2, 12]);
  /// ```
  fn concat_map<F, Inner>(self, f: F) -> Self::With<FlatMap<Self::Inner, F, Inner>>
  where
    F: for<'a> FnMut(Self::Item<'a>) -> Inner,
    Inner: Context<Inner: ObservableType<Err = Self::Err>>,
  {
    self.transform(|source| FlatMap::new(source, f, 1))
  }

  /// Map each item into an inner observable and switch to the latest one.
  ///
  /// This operator transforms each item emitted by the source observable into
  /// a new observable. It subscribes to each inner observable, but when a new
  /// inner observable arrives, it unsubscribes from the previous one.
  ///
  /// - Only emits values from the most recent inner observable.
  /// - Previous inner observables are unsubscribed when new ones arrive.
  /// - Errors from either the outer or the current inner observable terminate
  ///   downstream.
  ///
  /// # Examples
  ///
  /// ```
  /// use rxrust::prelude::*;
  ///
  /// let mut out = Vec::new();
  /// Local::from_iter([1_i32, 2_i32, 3_i32])
  ///   .switch_map(|x| {
  ///     // Each number creates an observable that emits that number twice
  ///     Local::from_iter([x, x])
  ///   })
  ///   .subscribe(|v| out.push(v));
  /// assert_eq!(out, vec![1, 1, 2, 2, 3, 3]);
  /// ```
  fn switch_map<F, Out>(self, f: F) -> Self::With<SwitchMap<Self::Inner, F>>
  where
    F: for<'a> FnMut(Self::Item<'a>) -> Out,
    Out: Context<Inner: ObservableType<Err = Self::Err> + 'static>,
  {
    self.transform(|source| SwitchMap { source, func: f })
  }
}

pub type ConnectableObservableCtx<'a, O> = <O as Context>::With<
  ConnectableObservable<
    <O as Context>::Inner,
    SubjectPtr<'a, O, <O as Observable>::Item<'a>, <O as Observable>::Err>,
  >,
>;
pub type ConnectableObservableCtxMutRef<'a, O, Item> = <O as Context>::With<
  ConnectableObservable<
    <O as Context>::Inner,
    SubjectPtrMutRef<'a, O, Item, <O as Observable>::Err>,
  >,
>;

/// Blanket implementation of Observable for any Context
///
/// This implementation uses ObservableType to infer Item and Err types
/// from the CoreObservable implementation.
impl<T> Observable for T
where
  T: Context,
  T::Inner: ObservableType,
{
  type Item<'a>
    = <T::Inner as ObservableType>::Item<'a>
  where
    T: 'a;
  type Err = <T::Inner as ObservableType>::Err;
}