farce 0.0.1.alpha2-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (376) hide show
  1. checksums.yaml +7 -0
  2. data/CODE_OF_CONDUCT.md +26 -0
  3. data/CONTRIBUTING.md +71 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +1524 -0
  6. data/SECURITY.md +10 -0
  7. data/docs/benchmarks.md +185 -0
  8. data/docs/gems/dry-types.md +290 -0
  9. data/docs/gems/msgpack.md +70 -0
  10. data/docs/gems/ractor-shim.md +63 -0
  11. data/docs/modes.md +628 -0
  12. data/docs/scopes.md +649 -0
  13. data/docs/variants.md +346 -0
  14. data/lib/farce/_yard/internal.rb +13 -0
  15. data/lib/farce/_yard/macros.rb +61 -0
  16. data/lib/farce/_yard/ractor.rb +46 -0
  17. data/lib/farce/abstract/atom.rb +186 -0
  18. data/lib/farce/abstract/bounded_map.rb +232 -0
  19. data/lib/farce/abstract/collection.rb +151 -0
  20. data/lib/farce/abstract/concurrent_map.rb +381 -0
  21. data/lib/farce/abstract/counter.rb +193 -0
  22. data/lib/farce/abstract/duplicable_map.rb +229 -0
  23. data/lib/farce/abstract/exchanger.rb +26 -0
  24. data/lib/farce/abstract/flag.rb +71 -0
  25. data/lib/farce/abstract/lazy.rb +115 -0
  26. data/lib/farce/abstract/lease.rb +104 -0
  27. data/lib/farce/abstract/lease_map.rb +261 -0
  28. data/lib/farce/abstract/lease_pool.rb +91 -0
  29. data/lib/farce/abstract/lfu_map.rb +20 -0
  30. data/lib/farce/abstract/lru_map.rb +25 -0
  31. data/lib/farce/abstract/map.rb +345 -0
  32. data/lib/farce/abstract/molecule.rb +245 -0
  33. data/lib/farce/abstract/port.rb +74 -0
  34. data/lib/farce/abstract/priority_queue.rb +117 -0
  35. data/lib/farce/abstract/queue.rb +269 -0
  36. data/lib/farce/abstract/scheduler.rb +111 -0
  37. data/lib/farce/abstract/set.rb +910 -0
  38. data/lib/farce/abstract/sorted_set.rb +172 -0
  39. data/lib/farce/abstract/timer_queue.rb +136 -0
  40. data/lib/farce/abstract/tree_map.rb +269 -0
  41. data/lib/farce/abstract/value.rb +68 -0
  42. data/lib/farce/abstract/vector.rb +1126 -0
  43. data/lib/farce/abstract/weak_atom.rb +32 -0
  44. data/lib/farce/abstract/weak_key_map.rb +12 -0
  45. data/lib/farce/abstract/weak_map.rb +13 -0
  46. data/lib/farce/abstract/weak_set.rb +12 -0
  47. data/lib/farce/abstract/weak_value_map.rb +12 -0
  48. data/lib/farce/abstract.rb +17 -0
  49. data/lib/farce/atom.rb +293 -0
  50. data/lib/farce/class_mirror.rb +87 -0
  51. data/lib/farce/clock.rb +121 -0
  52. data/lib/farce/config.rb +229 -0
  53. data/lib/farce/counter.rb +71 -0
  54. data/lib/farce/deduper.rb +122 -0
  55. data/lib/farce/engine/jruby/bounded_map.rb +314 -0
  56. data/lib/farce/engine/jruby/fiber_scheduler.jar +0 -0
  57. data/lib/farce/engine/jruby/fiber_scheduler.rb +119 -0
  58. data/lib/farce/engine/jruby/lease_waiting.rb +19 -0
  59. data/lib/farce/engine/jruby/map.rb +505 -0
  60. data/lib/farce/engine/jruby/mutable_numeric_copy.rb +42 -0
  61. data/lib/farce/engine/jruby/signal.rb +147 -0
  62. data/lib/farce/engine/jruby.rb +63 -0
  63. data/lib/farce/engine/jvm/concurrent_weak_registry.rb +55 -0
  64. data/lib/farce/engine/jvm/counter.rb +102 -0
  65. data/lib/farce/engine/jvm/extension.rb +32 -0
  66. data/lib/farce/engine/jvm/farce.jar +0 -0
  67. data/lib/farce/engine/jvm/flag.rb +79 -0
  68. data/lib/farce/engine/jvm/priority_queue.rb +217 -0
  69. data/lib/farce/engine/jvm/tree_map.rb +350 -0
  70. data/lib/farce/engine/jvm/types.rb +180 -0
  71. data/lib/farce/engine/jvm.rb +19 -0
  72. data/lib/farce/engine/ruby/3.4/fiber_scheduler.rb +20 -0
  73. data/lib/farce/engine/ruby/3.4/port.rb +186 -0
  74. data/lib/farce/engine/ruby/3.4/ractor_methods.rb +26 -0
  75. data/lib/farce/engine/ruby/3.4/ractor_selector.rb +92 -0
  76. data/lib/farce/engine/ruby/3.4/vault.rb +56 -0
  77. data/lib/farce/engine/ruby/4.0/port.rb +19 -0
  78. data/lib/farce/engine/ruby/4.0/ractor_methods.rb +20 -0
  79. data/lib/farce/engine/ruby/4.0/ractor_selector.rb +108 -0
  80. data/lib/farce/engine/ruby/4.0/vault.rb +74 -0
  81. data/lib/farce/engine/ruby/4.1/port.rb +21 -0
  82. data/lib/farce/engine/ruby/4.1/ractor_methods.rb +22 -0
  83. data/lib/farce/engine/ruby/4.1/ractor_selector.rb +25 -0
  84. data/lib/farce/engine/ruby/4.1/vault.rb +5 -0
  85. data/lib/farce/engine/ruby/fiber_scheduler.rb +32 -0
  86. data/lib/farce/engine/ruby/key_lock_map.rb +28 -0
  87. data/lib/farce/engine/ruby/shared/lease.rb +26 -0
  88. data/lib/farce/engine/ruby/shared/lease_pool.rb +24 -0
  89. data/lib/farce/engine/ruby/shared/main_scheduler.rb +9 -0
  90. data/lib/farce/engine/ruby/shared/parallel_scheduler.rb +9 -0
  91. data/lib/farce/engine/ruby/shared/proxy_owner.rb +31 -0
  92. data/lib/farce/engine/ruby/shared/ractor_methods.rb +34 -0
  93. data/lib/farce/engine/ruby/shared/ractor_selector.rb +328 -0
  94. data/lib/farce/engine/ruby/shared/strict_map.rb +13 -0
  95. data/lib/farce/engine/ruby/shared/unshared_vector.rb +14 -0
  96. data/lib/farce/engine/ruby/shared/vault.rb +144 -0
  97. data/lib/farce/engine/ruby/shared/vault_weak_map.rb +336 -0
  98. data/lib/farce/engine/ruby/shared/weak_atom.rb +54 -0
  99. data/lib/farce/engine/ruby/shared/weak_map.rb +413 -0
  100. data/lib/farce/engine/ruby.rb +130 -0
  101. data/lib/farce/engine/shared/atom.rb +10 -0
  102. data/lib/farce/engine/shared/exchanger.rb +130 -0
  103. data/lib/farce/engine/shared/identity_key.rb +22 -0
  104. data/lib/farce/engine/shared/lease.rb +10 -0
  105. data/lib/farce/engine/shared/lease_pool.rb +10 -0
  106. data/lib/farce/engine/shared/main_scheduler.rb +20 -0
  107. data/lib/farce/engine/shared/map_key_coordination.rb +226 -0
  108. data/lib/farce/engine/shared/parallel_scheduler.rb +9 -0
  109. data/lib/farce/engine/shared/port.rb +44 -0
  110. data/lib/farce/engine/shared/portable_bounded_map.rb +619 -0
  111. data/lib/farce/engine/shared/proxy_owner.rb +43 -0
  112. data/lib/farce/engine/shared/queue.rb +233 -0
  113. data/lib/farce/engine/shared/ractor_methods.rb +73 -0
  114. data/lib/farce/engine/shared/rebindable.rb +15 -0
  115. data/lib/farce/engine/shared/strict_atom.rb +73 -0
  116. data/lib/farce/engine/shared/strict_map.rb +117 -0
  117. data/lib/farce/engine/shared/strict_queue_values.rb +27 -0
  118. data/lib/farce/engine/shared/strict_tree_map.rb +56 -0
  119. data/lib/farce/engine/shared/transaction_map_backend.rb +20 -0
  120. data/lib/farce/engine/shared/trie.rb +317 -0
  121. data/lib/farce/engine/shared/trie_builder.rb +199 -0
  122. data/lib/farce/engine/shared/unshareable.rb +14 -0
  123. data/lib/farce/engine/shared/unshared_atom.rb +256 -0
  124. data/lib/farce/engine/shared/unshared_priority_queue.rb +9 -0
  125. data/lib/farce/engine/shared/unshared_queue.rb +18 -0
  126. data/lib/farce/engine/shared/unshared_signal.rb +21 -0
  127. data/lib/farce/engine/shared/unshared_vector.rb +377 -0
  128. data/lib/farce/engine/shared/unshared_weak_atom.rb +30 -0
  129. data/lib/farce/engine/shared/unshared_weak_map.rb +445 -0
  130. data/lib/farce/engine/shared/vault.rb +31 -0
  131. data/lib/farce/engine/shared/vector.rb +83 -0
  132. data/lib/farce/engine/shared/weak_atom/base.rb +189 -0
  133. data/lib/farce/engine/shared/weak_atom.rb +20 -0
  134. data/lib/farce/engine/shared/weak_map/cell.rb +99 -0
  135. data/lib/farce/engine/shared/weak_map/index.rb +249 -0
  136. data/lib/farce/engine/shared/weak_map/lock.rb +80 -0
  137. data/lib/farce/engine/shared/weak_map/reference.rb +33 -0
  138. data/lib/farce/engine/shared.rb +63 -0
  139. data/lib/farce/engine/truffleruby/fiber_scheduler.rb +13 -0
  140. data/lib/farce/engine/truffleruby/lock.rb +121 -0
  141. data/lib/farce/engine/truffleruby/map.rb +656 -0
  142. data/lib/farce/engine/truffleruby/native/counter.rb +105 -0
  143. data/lib/farce/engine/truffleruby/native/flag.rb +77 -0
  144. data/lib/farce/engine/truffleruby/native/ordered_array_support.rb +67 -0
  145. data/lib/farce/engine/truffleruby/native/priority_queue.rb +388 -0
  146. data/lib/farce/engine/truffleruby/native/tree_map.rb +51 -0
  147. data/lib/farce/engine/truffleruby/native/unsafe_tree_map.rb +350 -0
  148. data/lib/farce/engine/truffleruby/signal.rb +77 -0
  149. data/lib/farce/engine/truffleruby.rb +148 -0
  150. data/lib/farce/envelope.rb +335 -0
  151. data/lib/farce/error.rb +35 -0
  152. data/lib/farce/exchanger.rb +41 -0
  153. data/lib/farce/flag.rb +27 -0
  154. data/lib/farce/integrations/active_support/blank.rb +68 -0
  155. data/lib/farce/integrations/active_support/clock.rb +17 -0
  156. data/lib/farce/integrations/active_support/duplicable.rb +78 -0
  157. data/lib/farce/integrations/active_support/map.rb +175 -0
  158. data/lib/farce/integrations/active_support/set.rb +60 -0
  159. data/lib/farce/integrations/active_support/value_serialization.rb +17 -0
  160. data/lib/farce/integrations/active_support/vector.rb +231 -0
  161. data/lib/farce/integrations/active_support.rb +9 -0
  162. data/lib/farce/integrations/activesupport.rb +5 -0
  163. data/lib/farce/integrations/bson.rb +108 -0
  164. data/lib/farce/integrations/cbor.rb +51 -0
  165. data/lib/farce/integrations/concurrent.rb +112 -0
  166. data/lib/farce/integrations/dry-types.rb +5 -0
  167. data/lib/farce/integrations/dry_types.rb +694 -0
  168. data/lib/farce/integrations/json.rb +7 -0
  169. data/lib/farce/integrations/msgpack.rb +112 -0
  170. data/lib/farce/integrations/oj.rb +69 -0
  171. data/lib/farce/integrations/psych.rb +191 -0
  172. data/lib/farce/integrations/ractor-sharing.rb +5 -0
  173. data/lib/farce/integrations/ractor-tmvar.rb +5 -0
  174. data/lib/farce/integrations/ractor_sharing.rb +172 -0
  175. data/lib/farce/integrations/ractor_tmvar.rb +64 -0
  176. data/lib/farce/integrations/shared/to_json.rb +42 -0
  177. data/lib/farce/integrations/sorted_set.rb +11 -0
  178. data/lib/farce/integrations/weakref.rb +38 -0
  179. data/lib/farce/integrations/yajl.rb +10 -0
  180. data/lib/farce/integrations.rb +156 -0
  181. data/lib/farce/internal/_frozen_config.rb +11 -0
  182. data/lib/farce/internal/autoloads.rb +47 -0
  183. data/lib/farce/internal/blocking_priority_queue.rb +122 -0
  184. data/lib/farce/internal/converter.rb +106 -0
  185. data/lib/farce/internal/copyable.rb +31 -0
  186. data/lib/farce/internal/delegation.rb +20 -0
  187. data/lib/farce/internal/external_transaction.rb +59 -0
  188. data/lib/farce/internal/fake_ractor.rb +179 -0
  189. data/lib/farce/internal/freeze.rb +118 -0
  190. data/lib/farce/internal/inspect.rb +157 -0
  191. data/lib/farce/internal/key_lock_map.rb +29 -0
  192. data/lib/farce/internal/key_normalizer.rb +289 -0
  193. data/lib/farce/internal/lease_initialization.rb +115 -0
  194. data/lib/farce/internal/lease_map.rb +441 -0
  195. data/lib/farce/internal/lease_pool_state.rb +228 -0
  196. data/lib/farce/internal/lease_state.rb +342 -0
  197. data/lib/farce/internal/lease_waiting.rb +13 -0
  198. data/lib/farce/internal/managed_queue.rb +26 -0
  199. data/lib/farce/internal/map_value_modes.rb +227 -0
  200. data/lib/farce/internal/marshal_support.rb +227 -0
  201. data/lib/farce/internal/mixin.rb +20 -0
  202. data/lib/farce/internal/mutable_ordered_key_lock_map.rb +63 -0
  203. data/lib/farce/internal/noncopyable.rb +17 -0
  204. data/lib/farce/internal/ordered_key_lock_map.rb +69 -0
  205. data/lib/farce/internal/pool_supervisor.rb +41 -0
  206. data/lib/farce/internal/pool_worker.rb +61 -0
  207. data/lib/farce/internal/portable_transaction/reservation_entry.rb +29 -0
  208. data/lib/farce/internal/portable_transaction/strong_map_entry.rb +137 -0
  209. data/lib/farce/internal/portable_transaction/strong_map_size_entry.rb +44 -0
  210. data/lib/farce/internal/portable_transaction/tree_entry.rb +64 -0
  211. data/lib/farce/internal/portable_transaction.rb +164 -0
  212. data/lib/farce/internal/proxy_owner_notifications.rb +29 -0
  213. data/lib/farce/internal/reservation_waiting.rb +24 -0
  214. data/lib/farce/internal/scheduled_task.rb +48 -0
  215. data/lib/farce/internal/scheduler_io.rb +171 -0
  216. data/lib/farce/internal/scheduler_lifecycle.rb +314 -0
  217. data/lib/farce/internal/select_scheduler.rb +235 -0
  218. data/lib/farce/internal/storage.rb +162 -0
  219. data/lib/farce/internal/strict_lease.rb +30 -0
  220. data/lib/farce/internal/strict_lease_map.rb +20 -0
  221. data/lib/farce/internal/strict_lease_pool.rb +29 -0
  222. data/lib/farce/internal/thread_pool.rb +166 -0
  223. data/lib/farce/internal/transaction_conflict.rb +9 -0
  224. data/lib/farce/internal/transaction_freeze_guard.rb +30 -0
  225. data/lib/farce/internal/transaction_map_snapshot.rb +123 -0
  226. data/lib/farce/internal/undefined.rb +22 -0
  227. data/lib/farce/internal/unshared_lease.rb +29 -0
  228. data/lib/farce/internal/unshared_lease_pool.rb +34 -0
  229. data/lib/farce/internal/unshared_queue_waiting.rb +28 -0
  230. data/lib/farce/internal/value_serialization.rb +13 -0
  231. data/lib/farce/internal/weak_map_value_modes.rb +58 -0
  232. data/lib/farce/internal/weak_mode_manager.rb +26 -0
  233. data/lib/farce/internal.rb +129 -0
  234. data/lib/farce/lazy.rb +100 -0
  235. data/lib/farce/lazy_ref.rb +40 -0
  236. data/lib/farce/lease.rb +28 -0
  237. data/lib/farce/lease_map.rb +34 -0
  238. data/lib/farce/lease_pool.rb +29 -0
  239. data/lib/farce/lfu_map.rb +53 -0
  240. data/lib/farce/local/atom.rb +23 -0
  241. data/lib/farce/local/counter.rb +88 -0
  242. data/lib/farce/local/flag.rb +65 -0
  243. data/lib/farce/local/lazy.rb +38 -0
  244. data/lib/farce/local/lazy_ref.rb +36 -0
  245. data/lib/farce/local/lease.rb +77 -0
  246. data/lib/farce/local/lease_map.rb +80 -0
  247. data/lib/farce/local/lease_pool.rb +38 -0
  248. data/lib/farce/local/lfu_map.rb +55 -0
  249. data/lib/farce/local/lru_map.rb +62 -0
  250. data/lib/farce/local/map.rb +47 -0
  251. data/lib/farce/local/molecule.rb +45 -0
  252. data/lib/farce/local/priority_queue.rb +53 -0
  253. data/lib/farce/local/queue.rb +32 -0
  254. data/lib/farce/local/scoped.rb +166 -0
  255. data/lib/farce/local/set.rb +18 -0
  256. data/lib/farce/local/sorted_set.rb +50 -0
  257. data/lib/farce/local/timer_queue.rb +41 -0
  258. data/lib/farce/local/tree_map.rb +47 -0
  259. data/lib/farce/local/vector.rb +30 -0
  260. data/lib/farce/local/weak_atom.rb +23 -0
  261. data/lib/farce/local/weak_key_map.rb +39 -0
  262. data/lib/farce/local/weak_map.rb +39 -0
  263. data/lib/farce/local/weak_set.rb +18 -0
  264. data/lib/farce/local/weak_value_map.rb +39 -0
  265. data/lib/farce/local.rb +35 -0
  266. data/lib/farce/lock.rb +54 -0
  267. data/lib/farce/lru_map.rb +63 -0
  268. data/lib/farce/map.rb +58 -0
  269. data/lib/farce/mode_manager.rb +142 -0
  270. data/lib/farce/molecule.rb +60 -0
  271. data/lib/farce/mutable.rb +171 -0
  272. data/lib/farce/pool.rb +332 -0
  273. data/lib/farce/port.rb +134 -0
  274. data/lib/farce/priority_queue.rb +70 -0
  275. data/lib/farce/proxy/register.rb +101 -0
  276. data/lib/farce/proxy/supervisor.rb +137 -0
  277. data/lib/farce/proxy/wrapper.rb +49 -0
  278. data/lib/farce/proxy.rb +291 -0
  279. data/lib/farce/queue.rb +60 -0
  280. data/lib/farce/ractor.rb +269 -0
  281. data/lib/farce/read_write_lock.rb +256 -0
  282. data/lib/farce/reference.rb +158 -0
  283. data/lib/farce/resolv/dns.rb +42 -0
  284. data/lib/farce/resolv.rb +85 -0
  285. data/lib/farce/scheduler.rb +536 -0
  286. data/lib/farce/set.rb +39 -0
  287. data/lib/farce/shareable.rb +150 -0
  288. data/lib/farce/signal.rb +97 -0
  289. data/lib/farce/sorted_set.rb +36 -0
  290. data/lib/farce/strict/atom.rb +34 -0
  291. data/lib/farce/strict/counter.rb +10 -0
  292. data/lib/farce/strict/exchanger.rb +25 -0
  293. data/lib/farce/strict/flag.rb +10 -0
  294. data/lib/farce/strict/lazy.rb +26 -0
  295. data/lib/farce/strict/lazy_ref.rb +26 -0
  296. data/lib/farce/strict/lease.rb +27 -0
  297. data/lib/farce/strict/lease_map.rb +33 -0
  298. data/lib/farce/strict/lease_pool.rb +28 -0
  299. data/lib/farce/strict/lfu_map.rb +27 -0
  300. data/lib/farce/strict/lru_map.rb +28 -0
  301. data/lib/farce/strict/map.rb +54 -0
  302. data/lib/farce/strict/molecule.rb +16 -0
  303. data/lib/farce/strict/port.rb +40 -0
  304. data/lib/farce/strict/priority_queue.rb +20 -0
  305. data/lib/farce/strict/queue.rb +17 -0
  306. data/lib/farce/strict/set.rb +14 -0
  307. data/lib/farce/strict/sorted_set.rb +19 -0
  308. data/lib/farce/strict/timer_queue.rb +13 -0
  309. data/lib/farce/strict/tree_map.rb +27 -0
  310. data/lib/farce/strict/vector.rb +24 -0
  311. data/lib/farce/strict/weak_atom.rb +31 -0
  312. data/lib/farce/strict/weak_key_map.rb +49 -0
  313. data/lib/farce/strict/weak_map.rb +49 -0
  314. data/lib/farce/strict/weak_set.rb +14 -0
  315. data/lib/farce/strict/weak_value_map.rb +48 -0
  316. data/lib/farce/strict.rb +28 -0
  317. data/lib/farce/system.rb +40 -0
  318. data/lib/farce/thread_scheduler.rb +70 -0
  319. data/lib/farce/timer_queue.rb +66 -0
  320. data/lib/farce/transaction/atom.rb +73 -0
  321. data/lib/farce/transaction/map.rb +25 -0
  322. data/lib/farce/transaction/map_operations.rb +143 -0
  323. data/lib/farce/transaction/molecule.rb +90 -0
  324. data/lib/farce/transaction/mutable.rb +61 -0
  325. data/lib/farce/transaction/set.rb +17 -0
  326. data/lib/farce/transaction/set_operations.rb +48 -0
  327. data/lib/farce/transaction/sorted_set.rb +18 -0
  328. data/lib/farce/transaction/tree_map.rb +78 -0
  329. data/lib/farce/transaction/vector.rb +139 -0
  330. data/lib/farce/transaction/wrapper.rb +202 -0
  331. data/lib/farce/transaction.rb +343 -0
  332. data/lib/farce/tree_map.rb +56 -0
  333. data/lib/farce/unsafe/lfu_map.rb +36 -0
  334. data/lib/farce/unsafe/lru_map.rb +36 -0
  335. data/lib/farce/unsafe/tree_map.rb +21 -0
  336. data/lib/farce/unsafe.rb +29 -0
  337. data/lib/farce/unshareable.rb +67 -0
  338. data/lib/farce/unshared/atom.rb +24 -0
  339. data/lib/farce/unshared/counter.rb +10 -0
  340. data/lib/farce/unshared/flag.rb +10 -0
  341. data/lib/farce/unshared/lazy.rb +38 -0
  342. data/lib/farce/unshared/lazy_ref.rb +26 -0
  343. data/lib/farce/unshared/lease.rb +27 -0
  344. data/lib/farce/unshared/lease_map.rb +26 -0
  345. data/lib/farce/unshared/lease_pool.rb +25 -0
  346. data/lib/farce/unshared/lfu_map.rb +20 -0
  347. data/lib/farce/unshared/lru_map.rb +20 -0
  348. data/lib/farce/unshared/map.rb +49 -0
  349. data/lib/farce/unshared/molecule.rb +16 -0
  350. data/lib/farce/unshared/priority_queue.rb +16 -0
  351. data/lib/farce/unshared/queue.rb +27 -0
  352. data/lib/farce/unshared/set.rb +14 -0
  353. data/lib/farce/unshared/sorted_set.rb +28 -0
  354. data/lib/farce/unshared/timer_queue.rb +16 -0
  355. data/lib/farce/unshared/tree_map.rb +21 -0
  356. data/lib/farce/unshared/vector.rb +19 -0
  357. data/lib/farce/unshared/weak_atom.rb +31 -0
  358. data/lib/farce/unshared/weak_key_map.rb +44 -0
  359. data/lib/farce/unshared/weak_map.rb +44 -0
  360. data/lib/farce/unshared/weak_set.rb +14 -0
  361. data/lib/farce/unshared/weak_value_map.rb +43 -0
  362. data/lib/farce/unshared.rb +28 -0
  363. data/lib/farce/vector.rb +228 -0
  364. data/lib/farce/version.rb +8 -0
  365. data/lib/farce/walker/definitions.rb +186 -0
  366. data/lib/farce/walker/modification.rb +355 -0
  367. data/lib/farce/walker.rb +319 -0
  368. data/lib/farce/weak_atom.rb +121 -0
  369. data/lib/farce/weak_key_map.rb +31 -0
  370. data/lib/farce/weak_map.rb +33 -0
  371. data/lib/farce/weak_ref.rb +67 -0
  372. data/lib/farce/weak_set.rb +82 -0
  373. data/lib/farce/weak_value.rb +195 -0
  374. data/lib/farce/weak_value_map.rb +32 -0
  375. data/lib/farce.rb +519 -0
  376. metadata +420 -0
@@ -0,0 +1,175 @@
1
+ # frozen_string_literal: true
2
+ # shareable_constant_value: literal
3
+ # warn_indent: true
4
+
5
+ # @!group ActiveSupport Integration
6
+ require "farce/integrations/active_support"
7
+
8
+ module Farce
9
+ module Abstract
10
+ class Map
11
+ # @!macro active_support
12
+ # Convert entries using ActiveSupport's Hash JSON conversion.
13
+ # @return [Hash] The entries converted using Hash#as_json.
14
+ def as_json(...) = to_h.as_json(...)
15
+
16
+ # @!macro active_support
17
+ # Encode public entries as a query string, optionally under a namespace.
18
+ # @return [String]
19
+ def to_query(...) = to_h.to_query(...)
20
+ alias to_param to_query
21
+
22
+ # @!macro active_support
23
+ # Validate observed keys without normalizing the allowed keys.
24
+ # Concurrent changes may invalidate the result immediately.
25
+ # @return [self]
26
+ # @raise [ArgumentError] if an observed key is not allowed
27
+ def assert_valid_keys(*valid_keys)
28
+ valid_keys.flatten!
29
+ each_key do |key|
30
+ unless valid_keys.include?(key)
31
+ raise ArgumentError, "Unknown key: #{key.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(", ")}"
32
+ end
33
+ end
34
+ self
35
+ end
36
+ end
37
+
38
+ module DuplicableMap
39
+ # @!macro active_support
40
+ # Return a new map of the same class with interchangeable Symbol and String keys.
41
+ # Other key types and nested hashes are not normalized. Existing normalization is replaced.
42
+ # The copy preserves its value mode, value comparison, capacity, and Local scope where supported.
43
+ # Keys use equality. Entries from the current scope seed a Local copy.
44
+ # Values in move mode are copied to preserve the source.
45
+ # @return [Map] An independent map with indifferent key access.
46
+ def with_indifferent_access
47
+ normalizer = Ractor.shareable_proc { |key| Symbol === key ? key.name : key }
48
+ build_indifferent_access(**indifferent_access_options, normalize_keys: normalizer)
49
+ end
50
+
51
+ # @!macro active_support
52
+ # Deeply copy entries into a map of the same kind with the same settings.
53
+ # String and Symbol keys retain their identity, as with ActiveSupport's Hash#deep_dup.
54
+ # Copied keys and values must satisfy the map's normal storage rules.
55
+ # @return [Map]
56
+ def deep_dup
57
+ copier = ModeManager.new(mode: :copy) if respond_to?(:mode) && mode == :move
58
+ with_map_copy(empty: true) do |copy, map|
59
+ internal_map.each do |key, value|
60
+ key = key.deep_dup unless String === key || Symbol === key
61
+ value = unwrap_value(value).deep_dup
62
+ value = copier.unwrap(copier.wrap(value)) if copier
63
+ map[key] = copy.wrap_value(value)
64
+ end
65
+ end
66
+ end
67
+
68
+ # @!macro active_support
69
+ # Return a same-kind map with string keys, subject to its key normalizer.
70
+ # @return [Map]
71
+ def stringify_keys = transform_keys { |key| Symbol === key ? key.name : key.to_s }
72
+
73
+ # @!macro active_support
74
+ # Return a same-kind map with symbol keys where conversion succeeds.
75
+ # @return [Map]
76
+ def symbolize_keys
77
+ transform_keys do |key|
78
+ key.to_sym
79
+ rescue StandardError
80
+ key
81
+ end
82
+ end
83
+ alias to_options symbolize_keys
84
+
85
+ # @!macro active_support
86
+ # Return a same-kind map without blank values, including false.
87
+ # @return [Map]
88
+ def compact_blank = reject { |_, value| value.blank? }
89
+
90
+ # @!macro active_support
91
+ # Return a same-kind map with defaults applied before the receiver's entries.
92
+ # Existing entries win. Bounded copies rebuild eviction history and enforce their capacity.
93
+ # Move-mode defaults are copied so the input remains usable.
94
+ # @param other [Map, Hash, #to_hash] default entries
95
+ # @return [Map]
96
+ def reverse_merge(other)
97
+ unless Map === other
98
+ other = Hash.try_convert(other) || raise(TypeError, "input must be a Map, Hash, or respond to #to_hash")
99
+ end
100
+ copier = ModeManager.new(mode: :copy) if respond_to?(:mode) && mode == :move
101
+ with_map_copy(empty: true) do |copy, map|
102
+ other.each_pair do |key, value|
103
+ key = normalize_copied_key(key)
104
+ value = copier.unwrap(copier.wrap(value)) if copier
105
+ map[key] = copy.wrap_value(value)
106
+ end
107
+ internal_map.each { |key, value| map[key] = value }
108
+ end
109
+ end
110
+ alias with_defaults reverse_merge
111
+
112
+ private
113
+
114
+ def indifferent_access_options = respond_to?(:mode) ? { mode: mode } : {}
115
+
116
+ def build_indifferent_access(**options)
117
+ entries = self
118
+ if options[:mode] == :move
119
+ copier = ModeManager.new(mode: :copy)
120
+ entries = each_pair.map { |key, value| [key, copier.unwrap(copier.wrap(value))] }
121
+ end
122
+ self.class.new(entries, **options)
123
+ end
124
+ end
125
+
126
+ class ConcurrentMap
127
+ # @!macro active_support
128
+ # Remove blank values using per-key coordination. Always return self.
129
+ # The operation is not atomic across keys.
130
+ # @return [self]
131
+ def compact_blank! = delete_if { |_, value| value.blank? }
132
+
133
+ # @!macro active_support
134
+ # Store each default only if its key is absent, preserving existing nil and false values.
135
+ # Each decision is atomic for its key. Earlier changes survive exceptions.
136
+ # Inserted values use the map's transfer mode, including move semantics.
137
+ # @param other [Map, Hash, #to_hash] default entries
138
+ # @return [self]
139
+ def reverse_merge!(other)
140
+ unless Map === other
141
+ other = Hash.try_convert(other) || raise(TypeError, "input must be a Map, Hash, or respond to #to_hash")
142
+ end
143
+ other.each_pair { |key, value| store_if_absent(key) { value } }
144
+ self
145
+ end
146
+ alias with_defaults! reverse_merge!
147
+
148
+ private
149
+
150
+ def indifferent_access_options
151
+ super.merge(compare_keys_by_identity: false, compare_values_by_identity: compare_values_by_identity?)
152
+ end
153
+ end
154
+
155
+ class BoundedMap
156
+ private
157
+
158
+ def indifferent_access_options
159
+ super.merge(max_size:, compare_keys_by_identity: false, compare_values_by_identity: compare_values_by_identity?)
160
+ end
161
+ end
162
+ end
163
+
164
+ module Local
165
+ module Scoped
166
+ module Map
167
+ private def indifferent_access_options = super.merge(scope:)
168
+ end
169
+ end
170
+ end
171
+
172
+ Internal::Converter.define(ActiveSupport::HashWithIndifferentAccess, :Map) do
173
+ def prepare(...) = super.with_indifferent_access
174
+ end
175
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+ # shareable_constant_value: literal
3
+ # warn_indent: true
4
+
5
+ # @!group ActiveSupport Integration
6
+ require "farce/integrations/active_support"
7
+
8
+ module Farce
9
+ module Abstract
10
+ class Set
11
+ # @!macro active_support
12
+ # Deeply copy elements into a set of the same kind with the same settings.
13
+ # Copied elements pass through the set's normal storage rules.
14
+ # Weak results do not keep copied heap elements alive by themselves.
15
+ # @return [Farce::Abstract::Set]
16
+ def deep_dup = map_canonical(&:deep_dup)
17
+
18
+ # @!macro active_support
19
+ # Return a same-kind set without blank elements.
20
+ # @return [Farce::Abstract::Set]
21
+ def compact_blank = reject(&:blank?)
22
+
23
+ # @!macro active_support
24
+ # Remove blank elements and return self.
25
+ # @return [self]
26
+ def compact_blank! = delete_if(&:blank?)
27
+
28
+ # @!macro active_support
29
+ # Return a same-kind set with elements added. Array arguments are flattened by one level.
30
+ # @param elements [Array<BasicObject>] Elements or Arrays of elements to add.
31
+ # @return [Farce::Abstract::Set] A new set of the same class with the same settings.
32
+ def including(*elements) = union(elements.flatten(1))
33
+
34
+ # @!macro active_support
35
+ # Return a same-kind set without the supplied elements. Array arguments are flattened by one level.
36
+ # @param elements [Array<BasicObject>] Elements or Arrays of elements to remove.
37
+ # @return [Farce::Abstract::Set] A new set of the same class with the same settings.
38
+ def excluding(*elements) = difference(elements.flatten(1))
39
+ alias without excluding
40
+
41
+ # @!macro active_support
42
+ # Convert the elements through ActiveSupport's Array JSON conversion.
43
+ # @overload as_json(options = nil)
44
+ # @param options [Hash, nil] Options passed to each element's JSON conversion.
45
+ # @return [Array] The JSON-compatible values.
46
+ def as_json(...) = to_a.as_json(...)
47
+
48
+ # @!macro active_support
49
+ # Return the elements' ActiveSupport parameter representation.
50
+ # @return [String]
51
+ def to_param = to_a.to_param
52
+
53
+ # @!macro active_support
54
+ # Encode the elements as repeated query parameters under a key.
55
+ # @param key [String, Symbol] The query parameter name.
56
+ # @return [String] The encoded query string.
57
+ def to_query(key) = to_a.to_query(key)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ # shareable_constant_value: literal
3
+ # warn_indent: true
4
+
5
+ # @!group ActiveSupport Integration
6
+ require "farce/integrations/active_support"
7
+
8
+ module Farce
9
+ module Internal::ValueSerialization
10
+ # @!macro active_support
11
+ # Convert the current value using ActiveSupport's JSON conversion.
12
+ # @overload as_json(options = nil)
13
+ # @param options [Hash, nil] Options forwarded to the current value's as_json.
14
+ # @return [Object, nil] The JSON-compatible value.
15
+ def as_json(...) = value.as_json(...)
16
+ end
17
+ end
@@ -0,0 +1,231 @@
1
+ # frozen_string_literal: true
2
+ # shareable_constant_value: literal
3
+ # warn_indent: true
4
+
5
+ # @!group ActiveSupport Integration
6
+ require "farce/integrations/active_support"
7
+
8
+ module Farce
9
+ module Abstract
10
+ class Vector
11
+ # @!macro active_support
12
+ # Return entries starting at position from one snapshot.
13
+ # @param position [Integer] The starting index. Negative indexes count from the end.
14
+ # @return [Vector] The tail, or an empty Vector for an out-of-range position.
15
+ def from(position)
16
+ snapshot = internal_vector.snapshot
17
+ build_derived_vector(snapshot.from(position))
18
+ end
19
+
20
+ # @!macro active_support
21
+ # Return entries through position from one snapshot.
22
+ # @param position [Integer] The final index. Negative indexes count from the end.
23
+ # @return [Vector] The prefix, or an empty Vector for an out-of-range negative position.
24
+ def to(position) = build_derived_vector(internal_vector.snapshot.to(position))
25
+
26
+ # @!macro active_support
27
+ # Append entries to a new Vector, flattening argument Arrays by one level.
28
+ # New entries follow the receiver's storage rules and transfer mode.
29
+ # @param elements [Array<BasicObject>] The entries or Arrays of entries to append.
30
+ # @return [Vector] The combined entries.
31
+ def including(*elements) = self + elements.flatten(1)
32
+
33
+ # @!macro active_support
34
+ # Exclude entries using Array's hash-based difference semantics.
35
+ # @param elements [Array<BasicObject>] Entries to exclude, with Arrays flattened by one level.
36
+ # @return [Vector] The remaining entries.
37
+ def excluding(*elements) = self - elements.flatten(1)
38
+ alias without excluding
39
+
40
+ # @!macro active_support
41
+ # @return [BasicObject, nil] The second entry, or nil if absent.
42
+ def second = self[1]
43
+
44
+ # @!macro active_support
45
+ # @return [BasicObject, nil] The third entry, or nil if absent.
46
+ def third = self[2]
47
+
48
+ # @!macro active_support
49
+ # @return [BasicObject, nil] The fourth entry, or nil if absent.
50
+ def fourth = self[3]
51
+
52
+ # @!macro active_support
53
+ # @return [BasicObject, nil] The fifth entry, or nil if absent.
54
+ def fifth = self[4]
55
+
56
+ # @!macro active_support
57
+ # @return [BasicObject, nil] The forty-second entry, or nil if absent.
58
+ def forty_two = self[41]
59
+
60
+ # @!macro active_support
61
+ # @return [BasicObject, nil] The third entry from the end, or nil if absent.
62
+ def third_to_last = self[-3]
63
+
64
+ # @!macro active_support
65
+ # @return [BasicObject, nil] The second entry from the end, or nil if absent.
66
+ def second_to_last = self[-2]
67
+
68
+ # @!macro active_support
69
+ # Copy entries using ActiveSupport's deep-copy rules and the Vector's storage rules.
70
+ # Strict Vectors reject deep copies that are not shareable.
71
+ # @return [Vector] An independent same-kind Vector with deeply copied entries.
72
+ def deep_dup = map(&:deep_dup)
73
+
74
+ # @!macro active_support
75
+ # @return [Vector] Entries for which `blank?` is false, excluding nil and false.
76
+ def compact_blank = reject(&:blank?)
77
+
78
+ # @!macro active_support
79
+ # Extract keys from each entry in a snapshot.
80
+ # Extracted values follow the receiver's storage rules and transfer mode.
81
+ # @param keys [Array<BasicObject>] Keys passed to each entry's `[]` method.
82
+ # @return [Vector] Values for one key, or Vector rows for multiple keys.
83
+ def pluck(*keys)
84
+ return map { |entry| entry[keys.first] } unless keys.length > 1
85
+ map { |entry| build_derived_values(keys.map { |key| entry[key] }) }
86
+ end
87
+
88
+ # @!macro active_support
89
+ # Extract keys from the first entry in a snapshot.
90
+ # @param keys [Array<BasicObject>] Keys passed to the first entry's `[]` method.
91
+ # @return [BasicObject, Vector, nil] One value, a Vector for multiple keys, or nil if empty.
92
+ def pick(*keys)
93
+ snapshot = internal_vector.snapshot
94
+ return if snapshot.empty?
95
+ entry = logical_value(snapshot.first)
96
+ return entry[keys.first] unless keys.length > 1
97
+ build_derived_values(keys.map { |key| entry[key] })
98
+ end
99
+
100
+ # @!macro active_support
101
+ # Order a snapshot by the keys in series, preserving retained entries.
102
+ # @param key [Symbol, #to_proc] The method or callable used to extract ordering keys.
103
+ # @param series [Array] The desired order of keys.
104
+ # @param filter [Boolean] Whether to omit entries whose keys are absent from series.
105
+ # @return [Vector] The ordered entries. Repeated keys repeat their groups when filtering.
106
+ def in_order_of(key, series, filter: true)
107
+ snapshot = internal_vector.snapshot
108
+ logical = snapshot.map { logical_value(it) }
109
+ result = logical.in_order_of(key, series, filter:)
110
+ build_derived_from_logical(result, snapshot, logical)
111
+ end
112
+
113
+ # @!macro active_support
114
+ # Find the minimum extracted value without storing intermediate results in a Vector.
115
+ # @param key [Symbol, #to_proc] The method or callable used to extract comparison values.
116
+ # @return [BasicObject, nil] The minimum, or nil if empty.
117
+ def minimum(key) = to_a.minimum(key)
118
+
119
+ # @!macro active_support
120
+ # Find the maximum extracted value without storing intermediate results in a Vector.
121
+ # @param key [Symbol, #to_proc] The method or callable used to extract comparison values.
122
+ # @return [BasicObject, nil] The maximum, or nil if empty.
123
+ def maximum(key) = to_a.maximum(key)
124
+
125
+ # @!macro active_support
126
+ # Divide a snapshot into a requested number of Vector groups.
127
+ # @param number [Integer] The number of groups, following ActiveSupport's validation rules.
128
+ # @param fill_with [BasicObject] The padding value. False disables padding.
129
+ # @yield [group] Optionally visit each group after grouping the snapshot.
130
+ # @yieldparam group [Vector] A same-kind Vector containing one group.
131
+ # @yieldreturn [void] The result is ignored.
132
+ # @return [Vector] A Vector of groups, whether or not a block is supplied.
133
+ def in_groups(number, fill_with = nil)
134
+ groups = active_support_groups(:in_groups, number, fill_with)
135
+ result = build_derived_values(groups.map { build_derived_vector(it) })
136
+ result.each { yield it } if block_given?
137
+ result
138
+ end
139
+
140
+ # @!macro active_support
141
+ # Divide a snapshot into Vector groups with a maximum size.
142
+ # @param number [Integer] The positive group size.
143
+ # @param fill_with [BasicObject] The padding value. False disables padding.
144
+ # @yield [group] Optionally visit each group in the snapshot.
145
+ # @yieldparam group [Vector] A same-kind Vector containing one group.
146
+ # @yieldreturn [void] The result is ignored.
147
+ # @return [Vector, self] Groups without a block. With a block, the padded snapshot or self if padding is disabled.
148
+ def in_groups_of(number, fill_with = nil)
149
+ padding = fill_with != false
150
+ groups = active_support_groups(:in_groups_of, number, fill_with)
151
+ return build_derived_values(groups.map { build_derived_vector(it) }) unless block_given?
152
+ groups.each { yield build_derived_vector(it) }
153
+ padding ? build_derived_vector(groups.flatten(1)) : self
154
+ end
155
+
156
+ # @!macro active_support
157
+ # Split a snapshot at matching entries, omitting the separators.
158
+ # @param value [BasicObject] The separator compared with `==`, ignored when a block is supplied.
159
+ # @yield [entry] Optionally identify separator entries.
160
+ # @yieldparam entry [BasicObject] The current logical entry.
161
+ # @yieldreturn [BasicObject] A truthy value to split at this entry.
162
+ # @return [Vector] A Vector of Vector groups, including empty groups between adjacent separators.
163
+ def split(value = nil)
164
+ groups = internal_vector.snapshot.split do |stored|
165
+ entry = logical_value(stored)
166
+ block_given? ? yield(entry) : array_value_equal?(entry, value)
167
+ end
168
+ build_derived_values(groups.map { build_derived_vector(it) })
169
+ end
170
+
171
+ # @!macro active_support
172
+ # Convert a snapshot to ActiveSupport's explicit JSON representation.
173
+ # @param options [Hash, nil] Options passed to each entry's JSON conversion.
174
+ # @return [Array] The JSON-compatible values.
175
+ def as_json(options = nil) = to_a.as_json(options)
176
+
177
+ # @!macro active_support
178
+ # @return [String] The snapshot's URL parameter representation, joined with slashes.
179
+ def to_param = to_a.to_param
180
+
181
+ # @!macro active_support
182
+ # @param key [String] The query parameter name.
183
+ # @return [String] The encoded query string for the snapshot.
184
+ def to_query(key) = to_a.to_query(key)
185
+
186
+ # @!macro active_support
187
+ # Format a snapshot as a sentence using ActiveSupport's connectors and locale.
188
+ # @param options [Hash] Connector and locale options accepted by Array#to_sentence.
189
+ # @return [String] The formatted sentence.
190
+ def to_sentence(options = {}) = to_a.to_sentence(options)
191
+
192
+ # @!macro active_support
193
+ # Format a snapshot, optionally as a database ID list.
194
+ # @param format [Symbol] Use :db for comma-separated IDs, or :default for Array formatting.
195
+ # @return [String] The formatted snapshot.
196
+ def to_fs(format = :default) = to_a.to_fs(format)
197
+ alias to_formatted_s to_fs
198
+
199
+ # @!macro active_support
200
+ # Serialize a logical snapshot with ActiveSupport's Array XML conversion.
201
+ # Requires ActiveSupport's optional Builder dependency, as Array#to_xml does.
202
+ # @param options [Hash] XML options, including root, children, builder, and indentation.
203
+ # @yield [builder] Optionally append XML inside a nonempty collection's root element.
204
+ # @yieldparam builder [Builder::XmlMarkup] The XML builder used for serialization.
205
+ # @yieldreturn [void] The result is ignored.
206
+ # @return [String] The generated XML.
207
+ def to_xml(options = {}, &) = to_a.to_xml(options, &)
208
+
209
+ # @!macro active_support
210
+ # Create ActiveSupport's specialized query wrapper from a logical snapshot.
211
+ # @return [ActiveSupport::ArrayInquirer] An Array wrapper supporting predicate methods.
212
+ def inquiry = to_a.inquiry
213
+
214
+ private
215
+
216
+ def active_support_groups(method, number, fill_with)
217
+ marker = Object.new
218
+ groups = internal_vector.snapshot.public_send(method, number, fill_with == false ? false : marker)
219
+ stored_fill = UNDEFINED
220
+ groups.each do |group|
221
+ group.map! do |entry|
222
+ next entry unless entry.equal?(marker)
223
+ stored_fill = derived_storage(fill_with) if stored_fill.equal?(UNDEFINED)
224
+ stored_fill
225
+ end
226
+ end
227
+ groups
228
+ end
229
+ end
230
+ end
231
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+ # shareable_constant_value: literal
3
+ # warn_indent: true
4
+
5
+ require "farce"
6
+ require "active_support"
7
+ require "active_support/core_ext"
8
+
9
+ Dir.glob("active_support/*.rb", base: __dir__) { require_relative it }
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # shareable_constant_value: literal
3
+ # warn_indent: true
4
+
5
+ require_relative "active_support"
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+ # shareable_constant_value: literal
3
+ # warn_indent: true
4
+
5
+ # @!group BSON Integration
6
+ require "farce"
7
+ require "bson"
8
+
9
+ module Farce
10
+ class Abstract::Vector
11
+ # @note This method is only available if BSON has been loaded.
12
+ # Serialize a snapshot using BSON's array representation.
13
+ # Nested value wrappers are read before encoding their BSON types and payloads.
14
+ # @param buffer [BSON::ByteBuffer] An optional buffer to append to.
15
+ # @return [BSON::ByteBuffer] The buffer containing the encoded array.
16
+ def to_bson(buffer = ::BSON::ByteBuffer.new) = to_bson_normalized_value.to_bson(buffer)
17
+
18
+ # @note This method is only available if BSON has been loaded.
19
+ # Identify this value as a BSON array when embedded in a document or array.
20
+ # @return [String] The BSON array type byte.
21
+ def bson_type = ::BSON::Array::BSON_TYPE
22
+
23
+ # @note This method is only available if BSON has been loaded.
24
+ # Return a snapshot with nested values normalized by BSON.
25
+ # @return [Array] The normalized array.
26
+ def to_bson_normalized_value = to_a.to_bson_normalized_value
27
+
28
+ # @overload as_extended_json(**options)
29
+ # @note This method is only available if BSON has been loaded.
30
+ # Represent a snapshot as Extended JSON, forwarding BSON's format options.
31
+ # @param options [Hash] Options forwarded to Array#as_extended_json.
32
+ # @return [Array] The Extended JSON representation.
33
+ def as_extended_json(**) = to_a.as_extended_json(**)
34
+ end
35
+
36
+ class Abstract::Map
37
+ # @note This method is only available if BSON has been loaded.
38
+ # Serialize current entries as a BSON document.
39
+ # Nested value wrappers are read before encoding their BSON types and payloads.
40
+ # @param buffer [BSON::ByteBuffer] An optional buffer to append to.
41
+ # @return [BSON::ByteBuffer] The buffer containing the encoded document.
42
+ def to_bson(buffer = ::BSON::ByteBuffer.new) = to_bson_normalized_value.to_bson(buffer)
43
+
44
+ # @note This method is only available if BSON has been loaded.
45
+ # Identify this value as a BSON document when embedded in a document or array.
46
+ # @return [String] The BSON document type byte.
47
+ def bson_type = ::BSON::Hash::BSON_TYPE
48
+
49
+ # @note This method is only available if BSON has been loaded.
50
+ # Return current entries with keys and nested values normalized by BSON.
51
+ # @return [BSON::Document] The normalized document.
52
+ def to_bson_normalized_value = to_h.to_bson_normalized_value
53
+
54
+ # @overload as_extended_json(**options)
55
+ # @note This method is only available if BSON has been loaded.
56
+ # Represent current entries as Extended JSON, forwarding BSON's format options.
57
+ # @param options [Hash] Options forwarded to Hash#as_extended_json.
58
+ # @return [Hash] The Extended JSON representation.
59
+ def as_extended_json(**) = to_h.as_extended_json(**)
60
+ end
61
+
62
+ class Abstract::Set
63
+ # @note This method is only available if BSON has been loaded.
64
+ # Serialize current members using BSON's array representation.
65
+ # Nested value wrappers are read before encoding their BSON types and payloads.
66
+ # @param buffer [BSON::ByteBuffer] An optional buffer to append to.
67
+ # @return [BSON::ByteBuffer] The buffer containing the encoded array.
68
+ def to_bson(buffer = ::BSON::ByteBuffer.new) = to_bson_normalized_value.to_bson(buffer)
69
+
70
+ # @note This method is only available if BSON has been loaded.
71
+ # Identify this value as a BSON array when embedded in a document or array.
72
+ # @return [String] The BSON array type byte.
73
+ def bson_type = ::BSON::Array::BSON_TYPE
74
+
75
+ # @note This method is only available if BSON has been loaded.
76
+ # Return current members with nested values normalized by BSON.
77
+ # @return [Array] The normalized array.
78
+ def to_bson_normalized_value = to_a.to_bson_normalized_value
79
+
80
+ # @overload as_extended_json(**options)
81
+ # @note This method is only available if BSON has been loaded.
82
+ # Represent current members as Extended JSON, forwarding BSON's format options.
83
+ # @param options [Hash] Options forwarded to Array#as_extended_json.
84
+ # @return [Array] The Extended JSON representation.
85
+ def as_extended_json(**) = to_a.as_extended_json(**)
86
+ end
87
+
88
+ module Internal::ValueSerialization
89
+ # @note This method is only available if BSON has been loaded.
90
+ # Serialize the current stored value using its BSON representation.
91
+ # Use BSON::Document.new when embedding value wrappers in ordinary Ruby hashes.
92
+ # @param buffer [BSON::ByteBuffer] An optional buffer to append to.
93
+ # @return [BSON::ByteBuffer] The buffer containing the encoded value.
94
+ def to_bson(buffer = ::BSON::ByteBuffer.new) = to_bson_normalized_value.to_bson(buffer)
95
+
96
+ # @note This method is only available if BSON has been loaded.
97
+ # Read the stored value once and normalize it for BSON encoding.
98
+ # @return [Object] The normalized current value.
99
+ def to_bson_normalized_value = value.to_bson_normalized_value
100
+
101
+ # @overload as_extended_json(**options)
102
+ # @note This method is only available if BSON has been loaded.
103
+ # Represent the current value as Extended JSON, forwarding BSON's format options.
104
+ # @param options [Hash] Options forwarded to the value's as_extended_json.
105
+ # @return [Object] The Extended JSON representation.
106
+ def as_extended_json(**) = value.as_extended_json(**)
107
+ end
108
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+ # shareable_constant_value: literal
3
+ # warn_indent: true
4
+
5
+ # @!group CBOR Integration
6
+ require "farce"
7
+ require "cbor"
8
+
9
+ module Farce
10
+ class Abstract::Vector
11
+ # @overload to_cbor(*arguments)
12
+ # @note This method is only available if CBOR has been loaded.
13
+ # @note On CRuby, CBOR's native encoder must run in the main Ractor.
14
+ # Serialize a snapshot as a CBOR array.
15
+ # @example
16
+ # CBOR.decode(Farce::Vector.new([1, false]).to_cbor) # => [1, false]
17
+ # @param arguments [Array<Object>] Arguments forwarded to Array#to_cbor.
18
+ # @return [String, ::CBOR::Packer, nil] Encoded bytes, the supplied packer, or nil when writing to IO.
19
+ def to_cbor(...) = to_a.to_cbor(...)
20
+ end
21
+
22
+ class Abstract::Map
23
+ # @overload to_cbor(*arguments)
24
+ # @note This method is only available if CBOR has been loaded.
25
+ # @note On CRuby, CBOR's native encoder must run in the main Ractor.
26
+ # Serialize current entries as a CBOR map.
27
+ # @param arguments [Array<Object>] Arguments forwarded to Hash#to_cbor.
28
+ # @return [String, ::CBOR::Packer, nil] Encoded bytes, the supplied packer, or nil when writing to IO.
29
+ def to_cbor(...) = to_h.to_cbor(...)
30
+ end
31
+
32
+ class Abstract::Set
33
+ # @overload to_cbor(*arguments)
34
+ # @note This method is only available if CBOR has been loaded.
35
+ # @note On CRuby, CBOR's native encoder must run in the main Ractor.
36
+ # Serialize current members as a CBOR array.
37
+ # @param arguments [Array<Object>] Arguments forwarded to Array#to_cbor.
38
+ # @return [String, ::CBOR::Packer, nil] Encoded bytes, the supplied packer, or nil when writing to IO.
39
+ def to_cbor(...) = to_a.to_cbor(...)
40
+ end
41
+
42
+ module Internal::ValueSerialization
43
+ # @overload to_cbor(*arguments)
44
+ # @note This method is only available if CBOR has been loaded.
45
+ # @note On CRuby, CBOR's native encoder must run in the main Ractor.
46
+ # Serialize the current value using its CBOR representation.
47
+ # @param arguments [Array<Object>] Arguments forwarded to the value's to_cbor.
48
+ # @return [String, ::CBOR::Packer, nil] Encoded bytes, the supplied packer, or nil when writing to IO.
49
+ def to_cbor(...) = value.to_cbor(...)
50
+ end
51
+ end