zng-var 0.13.4

Part of the zng project.
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
use std::{
    any::{Any, TypeId},
    fmt,
    marker::PhantomData,
    ops,
    sync::{Arc, atomic::AtomicBool},
};

use crate::{
    AnyVarHookArgs, AnyVarValue, BoxAnyVarValue, VarInstanceTag, VarUpdateId, VarValue,
    animation::{AnimationStopFn, ModifyInfo},
    read_only_var::ReadOnlyImpl,
};
use bitflags::bitflags;
use smallbox::{SmallBox, smallbox};

pub(crate) mod shared_var;
pub use shared_var::{any_var, any_var_derived, var, var_derived, var_getter, var_state};

pub(crate) mod const_var;
pub(crate) mod cow_var;
pub use const_var::IntoVar;
pub(crate) mod flat_map_var;
pub(crate) mod read_only_var;

pub(crate) mod contextual_var;
pub use contextual_var::{ContextInitHandle, WeakContextInitHandle, any_contextual_var, contextual_var};

pub(crate) mod context_var;
pub use context_var::{__context_var_local, ContextVar, context_var_init};

pub(crate) mod merge_var;
pub use merge_var::{
    __merge_var, MergeInput, MergeVarBuilder, VarMergeInputs, merge_var, merge_var_input, merge_var_output, merge_var_with,
};

pub(crate) mod response_var;
pub use response_var::{ResponderVar, Response, ResponseVar, response_done_var, response_var};

pub(crate) mod when_var;
pub use when_var::{__when_var, AnyWhenVarBuilder, WhenVarBuilder};

pub(crate) mod expr_var;
pub use expr_var::{__expr_var, expr_var_as, expr_var_into, expr_var_map};

pub(crate) enum DynAnyVar {
    Const(const_var::ConstVar),
    Merge(merge_var::MergeVar),
    When(when_var::WhenVar),

    Shared(shared_var::SharedVar),
    Context(context_var::ContextVarImpl),
    FlatMap(flat_map_var::FlatMapVar),
    Cow(cow_var::CowVar),
    Contextual(contextual_var::ContextualVar),

    ReadOnlyShared(ReadOnlyImpl<shared_var::SharedVar>),
    ReadOnlyFlatMap(ReadOnlyImpl<flat_map_var::FlatMapVar>),
    ReadOnlyContext(ReadOnlyImpl<context_var::ContextVarImpl>),
    ReadOnlyCow(ReadOnlyImpl<cow_var::CowVar>),
    ReadOnlyContextual(ReadOnlyImpl<contextual_var::ContextualVar>),
}
macro_rules! dispatch {
    ($self:ident, $var:ident => $($tt:tt)+) => {
        match $self {
            DynAnyVar::Const($var) => $($tt)+,
            DynAnyVar::Merge($var) => $($tt)+,
            DynAnyVar::FlatMap($var) => $($tt)+,
            DynAnyVar::When($var) => $($tt)+,

            DynAnyVar::Shared($var) => $($tt)+,
            DynAnyVar::Context($var) => $($tt)+,
            DynAnyVar::Cow($var) => $($tt)+,
            DynAnyVar::Contextual($var) => $($tt)+,

            DynAnyVar::ReadOnlyShared($var) => $($tt)+,
            DynAnyVar::ReadOnlyFlatMap($var) => $($tt)+,
            DynAnyVar::ReadOnlyContext($var) => $($tt)+,
            DynAnyVar::ReadOnlyCow($var) => $($tt)+,
            DynAnyVar::ReadOnlyContextual($var) => $($tt)+,
        }
    };
}
impl fmt::Debug for DynAnyVar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        dispatch!(self, v => fmt::Debug::fmt(v, f))
    }
}

pub(crate) enum DynWeakAnyVar {
    Const(const_var::WeakConstVar),
    Merge(merge_var::WeakMergeVar),
    When(when_var::WeakWhenVar),

    Shared(shared_var::WeakSharedVar),
    Context(context_var::ContextVarImpl),
    FlatMap(flat_map_var::WeakFlatMapVar),
    Cow(cow_var::WeakCowVar),
    Contextual(contextual_var::WeakContextualVar),

    ReadOnlyShared(ReadOnlyImpl<shared_var::WeakSharedVar>),
    ReadOnlyContext(ReadOnlyImpl<context_var::ContextVarImpl>),
    ReadOnlyCow(ReadOnlyImpl<cow_var::WeakCowVar>),
    ReadOnlyContextual(ReadOnlyImpl<contextual_var::WeakContextualVar>),
    ReadOnlyFlatMap(ReadOnlyImpl<flat_map_var::WeakFlatMapVar>),
}
macro_rules! dispatch_weak {
    ($self:ident, $var:ident => $($tt:tt)+) => {
        match $self {
            DynWeakAnyVar::Const($var) => $($tt)+,
            DynWeakAnyVar::Shared($var) => $($tt)+,
            DynWeakAnyVar::Context($var) => $($tt)+,
            DynWeakAnyVar::Cow($var) => $($tt)+,
            DynWeakAnyVar::Contextual($var) => $($tt)+,
            DynWeakAnyVar::FlatMap($var) => $($tt)+,
            DynWeakAnyVar::Merge($var) => $($tt)+,
            DynWeakAnyVar::When($var) => $($tt)+,
            DynWeakAnyVar::ReadOnlyShared($var) => $($tt)+,
            DynWeakAnyVar::ReadOnlyContext($var) => $($tt)+,
            DynWeakAnyVar::ReadOnlyCow($var) => $($tt)+,
            DynWeakAnyVar::ReadOnlyContextual($var) => $($tt)+,
            DynWeakAnyVar::ReadOnlyFlatMap($var) => $($tt)+,

        }
    };
}
impl fmt::Debug for DynWeakAnyVar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        dispatch_weak!(self, v => fmt::Debug::fmt(v, f))
    }
}

macro_rules! declare {
    ($(
        $(#[$meta:meta])*
        fn $method:ident(&self $(, $arg:ident : $Input:ty)*) $(-> $Output:ty)?;
    )+) => {
        pub(crate) trait VarImpl: fmt::Debug + Any + Send + Sync {
            $(
                $(#[$meta])*
                fn $method(&self $(, $arg: $Input)*) $(-> $Output)?;
            )+
        }

        impl VarImpl for DynAnyVar {
            $(
                $(#[$meta])*
                fn $method(&self $(, $arg: $Input)*) $(-> $Output)? {
                    dispatch!(self, v => VarImpl::$method(v$(, $arg)*))
                }
            )+
        }
    };
}
declare! {
    fn clone_dyn(&self) -> DynAnyVar;
    fn value_type(&self) -> TypeId;
    #[cfg(feature = "type_names")]
    fn value_type_name(&self) -> &'static str;
    fn strong_count(&self) -> usize;
    fn var_eq(&self, other: &DynAnyVar) -> bool;
    fn var_instance_tag(&self) -> VarInstanceTag;
    fn downgrade(&self) -> DynWeakAnyVar;
    fn capabilities(&self) -> VarCapability;
    fn with(&self, visitor: &mut dyn FnMut(&dyn AnyVarValue));
    fn get(&self) -> BoxAnyVarValue;
    fn set(&self, new_value: BoxAnyVarValue) -> bool;
    fn update(&self) -> bool;
    fn modify(&self, modify: SmallBox<dyn FnMut(&mut AnyVarModify) + Send + 'static, smallbox::space::S4>) -> bool;
    fn hook(&self, on_new: SmallBox<dyn FnMut(&AnyVarHookArgs) -> bool + Send + 'static, smallbox::space::S4>) -> VarHandle;
    fn last_update(&self) -> VarUpdateId;
    fn modify_importance(&self) -> usize;
    fn is_animating(&self) -> bool;
    fn hook_animation_stop(&self, handler: AnimationStopFn) -> VarHandle;
    fn current_context(&self) -> DynAnyVar;
    fn modify_info(&self) -> ModifyInfo;
}

macro_rules! declare_weak {
        ($(
        fn $method:ident(&self $(, $arg:ident : $Input:ty)*) $(-> $Output:ty)?;
    )+) => {
        pub(crate) trait WeakVarImpl: fmt::Debug + Any + Send + Sync {
            $(
                fn $method(&self $(, $arg: $Input)*) $(-> $Output)?;
            )+
        }

        impl WeakVarImpl for DynWeakAnyVar {
            $(
                fn $method(&self $(, $arg: $Input)*) $(-> $Output)? {
                    dispatch_weak!(self, v => WeakVarImpl::$method(v$(, $arg)*))
                }
            )+
        }
    };
}
declare_weak! {
    fn clone_dyn(&self) -> DynWeakAnyVar;
    fn strong_count(&self) -> usize;
    fn upgrade(&self) -> Option<DynAnyVar>;
    fn var_eq(&self, other: &DynWeakAnyVar) -> bool;
}

/// Error when an attempt to modify a variable without the [`MODIFY`] capability is made.
///
/// [`MODIFY`]: VarCapability::MODIFY
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct VarIsReadOnlyError {}
impl fmt::Display for VarIsReadOnlyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "cannot modify read-only variable")
    }
}
impl std::error::Error for VarIsReadOnlyError {}

bitflags! {
    /// Kinds of interactions allowed by a [`Var<T>`] in the current update.
    ///
    /// You can get the current capabilities of a var by using the [`AnyVar::capabilities`] method.
    ///
    /// [`Var<T>`]: crate::Var
    /// [`AnyVar::capabilities`]: crate::AnyVar::capabilities
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct VarCapability: u8 {
        /// Variable value can change.
        ///
        /// If this is set the [`AnyVar::is_new`] can be `true` in some updates, a variable can `NEW`
        /// even if it cannot `MODIFY`, in this case the variable is a read-only wrapper on a read-write variable.
        ///
        /// [`AnyVar::is_new`]: crate::AnyVar::is_new
        const NEW = 0b0000_0010;

        /// Variable can be modified.
        ///
        /// If this is set [`Var::try_modify`] always returns `Ok`, if this is set `NEW` is also set.
        ///
        /// Note that modify requests from inside overridden animations can still be ignored, see [`AnyVar::modify_importance`].
        ///
        /// [`AnyVar::modify_importance`]: crate::AnyVar::modify_importance
        /// [`Var::try_modify`]: crate::Var::try_modify
        const MODIFY = 0b0000_0011;

        /// Var represents different inner variables depending on the context it is used.
        const CONTEXT = 0b1000_0000;

        /// Variable capabilities can change to sometimes have the `MODIFY` capability.
        const MODIFY_CHANGES = 0b0100_0000;
        /// Variable capabilities can change to sometimes have the `CONTEXT` capability.
        const CONTEXT_CHANGES = 0b0010_0000;

        /// Var is an *arc* reference to the value and variable state, cloning the variable only clones a
        /// reference to the variable, all references modify and notify the same state.
        const SHARE = 0b0001_0000;
    }
}
impl VarCapability {
    /// If cannot `NEW` and is not `MODIFY_CHANGES`.
    pub fn is_const(self) -> bool {
        self.is_empty()
    }

    /// If does not have `MODIFY` capability and is not `MODIFY_CHANGES`.
    pub fn is_always_read_only(&self) -> bool {
        !self.contains(Self::MODIFY) && !self.contains(Self::MODIFY_CHANGES)
    }

    /// If does not have `MODIFY` capability.
    pub fn is_read_only(self) -> bool {
        !self.can_modify()
    }

    /// Has the `MODIFY` capability.
    pub fn can_modify(self) -> bool {
        self.contains(Self::MODIFY)
    }

    /// Has the `CONTEXT` capability.
    pub fn is_contextual(self) -> bool {
        self.contains(Self::CONTEXT)
    }

    /// Has the `CONTEXT` capability and does not have `CONTEXT_CHANGES`.
    pub fn is_always_contextual(self) -> bool {
        self.contains(Self::CONTEXT) && !self.contains(Self::CONTEXT_CHANGES)
    }

    /// Has the `SHARE` capability.
    pub fn is_share(&self) -> bool {
        self.contains(Self::SHARE)
    }

    /// Does not have the `SHARE` capability.
    ///
    /// Cloning this variable clones the value.
    pub fn is_local(&self) -> bool {
        !self.is_share()
    }
}
impl VarCapability {
    pub(crate) fn as_always_read_only(self) -> Self {
        let mut out = self;

        // can be new, but not modify
        out.remove(Self::MODIFY & !Self::NEW);
        // never will allow modify
        out.remove(Self::MODIFY_CHANGES);

        out
    }
}

bitflags! {
    #[derive(Clone, Copy)]
    pub(crate) struct VarModifyUpdate: u8 {
        /// Value was deref_mut or update was called
        const UPDATE = 0b001;
        /// Method update was called
        const REQUESTED = 0b011;
        /// Value was deref_mut
        const TOUCHED = 0b101;
    }
}

/// Mutable reference to a variable value.
///
/// The variable will notify an update only on `deref_mut`.
pub struct AnyVarModify<'a> {
    pub(crate) value: &'a mut BoxAnyVarValue,
    pub(crate) update: VarModifyUpdate,
    pub(crate) tags: Vec<BoxAnyVarValue>,
    pub(crate) custom_importance: Option<usize>,
}
impl<'a> AnyVarModify<'a> {
    /// Replace the value if not equal.
    ///
    /// Note that you can also deref_mut to modify the value.
    pub fn set(&mut self, mut new_value: BoxAnyVarValue) -> bool {
        if **self.value != *new_value {
            if !self.value.try_swap(&mut *new_value) {
                #[cfg(feature = "type_names")]
                panic!(
                    "cannot AnyVarModify::set `{}` on variable of type `{}`",
                    new_value.type_name(),
                    self.value.type_name()
                );
                #[cfg(not(feature = "type_names"))]
                panic!("cannot modify set, type mismatch");
            }
            self.update |= VarModifyUpdate::TOUCHED;
            true
        } else {
            false
        }
    }

    /// Notify an update, even if the value does not actually change.
    pub fn update(&mut self) {
        self.update |= VarModifyUpdate::REQUESTED;
    }

    /// Custom tags that will be shared with the var hooks if the value updates.
    ///
    /// The tags where set by previous modify closures or this one during this update cycle, so
    /// tags can also be used to communicate between modify closures.
    pub fn tags(&self) -> &[BoxAnyVarValue] {
        &self.tags
    }

    /// Add a custom tag object that will be shared with the var hooks if the value updates.
    pub fn push_tag(&mut self, tag: impl AnyVarValue) {
        self.tags.push(BoxAnyVarValue::new(tag));
    }

    /// Sets a custom [`AnyVar::modify_importance`] value.
    ///
    /// Note that the modify info is already automatically set, using a custom value here
    /// can easily break all future modify requests for this variable. The importance is set even if the
    /// variable does not update (no actual value change or update request).
    ///
    /// [`AnyVar::modify_importance`]: crate::AnyVar::modify_importance
    pub fn set_modify_importance(&mut self, importance: usize) {
        self.custom_importance = Some(importance);
    }

    /// Strongly typed reference, if it is of the same type.
    pub fn downcast<'s, T: VarValue>(&'s mut self) -> Option<VarModify<'s, 'a, T>> {
        if self.value.is::<T>() {
            Some(VarModify {
                inner: self,
                _t: PhantomData,
            })
        } else {
            None
        }
    }

    /// Immutable reference to the value.
    ///
    /// Note that you can also simply deref to the value.
    pub fn value(&self) -> &dyn AnyVarValue {
        &**self
    }

    /// Mutable reference to the value.
    ///
    /// Getting a mutable reference to the value flags the variable to notify update.
    ///
    /// Note that you can also simply deref to the value.
    pub fn value_mut(&mut self) -> &mut dyn AnyVarValue {
        &mut **self
    }
}
impl<'a> ops::Deref for AnyVarModify<'a> {
    type Target = dyn AnyVarValue;

    fn deref(&self) -> &Self::Target {
        &**self.value
    }
}
impl<'a> ops::DerefMut for AnyVarModify<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.update |= VarModifyUpdate::TOUCHED;
        self.value.deref_mut()
    }
}

/// Mutable reference to a variable value.
///
/// The variable will notify an update only on `deref_mut`.
pub struct VarModify<'s, 'a, T: VarValue> {
    inner: &'s mut AnyVarModify<'a>,
    _t: PhantomData<fn() -> &'a T>,
}
impl<'s, 'a, T: VarValue> VarModify<'s, 'a, T> {
    /// Replace the value if not equal.
    ///
    /// Note that you can also deref_mut to modify the value.
    pub fn set(&mut self, new_value: impl Into<T>) -> bool {
        let new_value = new_value.into();
        if **self != new_value {
            **self = new_value;
            true
        } else {
            false
        }
    }

    /// Notify an update, even if the value does not actually change.
    pub fn update(&mut self) {
        self.inner.update();
    }

    /// Custom tags that will be shared with the var hooks if the value updates.
    ///
    /// The tags where set by previous modify closures or this one during this update cycle, so
    /// tags can also be used to communicate between modify closures.
    pub fn tags(&self) -> &[BoxAnyVarValue] {
        self.inner.tags()
    }

    /// Add a custom tag object that will be shared with the var hooks if the value updates.
    pub fn push_tag(&mut self, tag: impl AnyVarValue) {
        self.inner.push_tag(tag);
    }

    /// Sets a custom [`AnyVar::modify_importance`] value.
    ///
    /// Note that the modify info is already automatically set, using a custom value here
    /// can easily break all future modify requests for this variable. The importance is set even if the
    /// variable does not update (no actual value change or update request).
    ///
    /// [`AnyVar::modify_importance`]: crate::AnyVar::modify_importance
    pub fn set_modify_importance(&mut self, importance: usize) {
        self.inner.set_modify_importance(importance);
    }

    /// Type erased reference.
    pub fn as_any(&mut self) -> &mut AnyVarModify<'a> {
        self.inner
    }

    /// Immutable reference to the value.
    ///
    /// Note that you can also simply deref to the value.
    pub fn value(&self) -> &T {
        self
    }

    /// Mutable reference to the value.
    ///
    /// Getting a mutable reference to the value flags the variable to notify update.
    ///
    /// Note that you can also simply deref to the value.
    pub fn value_mut(&mut self) -> &mut T {
        self
    }
}
impl<'s, 'a, T: VarValue> ops::Deref for VarModify<'s, 'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.inner.downcast_ref().unwrap()
    }
}
impl<'s, 'a, T: VarValue> ops::DerefMut for VarModify<'s, 'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner.downcast_mut().unwrap()
    }
}

/// Handle to a variable or animation hook.
///
/// This can represent a widget subscriber, a var binding, var app handler or animation, dropping the handler stops
/// the behavior it represents.
///
/// Note that the hook closure is not dropped immediately when the handle is dropped, usually it will drop only the next
/// time it would have been called.
#[derive(Clone, Default)]
#[must_use = "var handle stops the behavior it represents on drop"]
pub struct VarHandle(Option<Arc<AtomicBool>>);
impl PartialEq for VarHandle {
    fn eq(&self, other: &Self) -> bool {
        if let Some(a) = &self.0
            && let Some(b) = &other.0
        {
            Arc::ptr_eq(a, b)
        } else {
            false
        }
    }
}
impl fmt::Debug for VarHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_dummy() {
            write!(f, "VarHandle(<dummy>)")
        } else {
            f.debug_tuple("VarHandle").finish_non_exhaustive()
        }
    }
}
impl VarHandle {
    /// Handle to no variable.
    pub const fn dummy() -> Self {
        VarHandle(None)
    }

    pub(crate) fn new() -> (VarHandlerOwner, Self) {
        let h = Arc::new(AtomicBool::new(false));
        (VarHandlerOwner(h.clone()), Self(Some(h)))
    }

    /// Returns `true` if the handle is a [`dummy`].
    ///
    /// [`dummy`]: VarHandle::dummy
    pub fn is_dummy(&self) -> bool {
        self.0.is_none()
    }

    /// Drop the handle without stopping the behavior it represents.
    ///
    /// Note that the behavior can still be stopped by dropping the involved variables.
    pub fn perm(self) {
        if let Some(c) = &self.0 {
            c.store(true, std::sync::atomic::Ordering::Relaxed);
        }
    }

    /// Create a [`VarHandles`] collection with `self` and `other`.
    pub fn chain(self, other: Self) -> VarHandles {
        VarHandles(smallvec::smallvec![self, other])
    }

    /// Create a weak reference to this handle.
    ///
    /// Note that weak references to dummy handles cannot upgrade back.
    pub fn downgrade(&self) -> WeakVarHandle {
        match &self.0 {
            Some(a) => WeakVarHandle(Arc::downgrade(a)),
            None => WeakVarHandle::new(),
        }
    }
}

/// Weak reference to a [`VarHandle`].
#[derive(Clone, Default)]
pub struct WeakVarHandle(std::sync::Weak<AtomicBool>);
impl PartialEq for WeakVarHandle {
    fn eq(&self, other: &Self) -> bool {
        self.0.ptr_eq(&other.0)
    }
}
impl Eq for WeakVarHandle {}
impl fmt::Debug for WeakVarHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("WeakVarHandle").finish_non_exhaustive()
    }
}
impl WeakVarHandle {
    /// Upgrade to strong handle.
    ///
    /// Returns `None` if no strong reference to the handle remains.
    pub fn upgrade(&self) -> Option<VarHandle> {
        let h = VarHandle(self.0.upgrade());
        if h.is_dummy() { None } else { Some(h) }
    }

    /// New dummy weak reference that does not upgrade.
    pub const fn new() -> Self {
        WeakVarHandle(std::sync::Weak::new())
    }
}

pub(crate) struct VarHandlerOwner(Arc<AtomicBool>);
impl fmt::Debug for VarHandlerOwner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", Arc::strong_count(&self.0) - 1)?;
        if self.0.load(std::sync::atomic::Ordering::Relaxed) {
            write!(f, " perm")
        } else {
            Ok(())
        }
    }
}
impl VarHandlerOwner {
    pub fn is_alive(&self) -> bool {
        Arc::strong_count(&self.0) > 1 || self.0.load(std::sync::atomic::Ordering::Relaxed)
    }
}

/// Represents a collection of var handles.
#[must_use = "var handles stops the behavior they represents on drop"]
#[derive(Clone, Default)]
pub struct VarHandles(smallvec::SmallVec<[VarHandle; 2]>);
impl VarHandles {
    /// Empty collection.
    pub const fn dummy() -> Self {
        VarHandles(smallvec::SmallVec::new_const())
    }

    /// Returns `true` if empty or all handles are dummy.
    pub fn is_dummy(&self) -> bool {
        self.0.is_empty() || self.0.iter().all(VarHandle::is_dummy)
    }

    /// Drop all handles without stopping their behavior.
    pub fn perm(self) {
        for handle in self.0 {
            handle.perm()
        }
    }

    /// Add the `other` handle to the collection, if it is not dummy.
    pub fn push(&mut self, other: VarHandle) -> &mut Self {
        if !other.is_dummy() {
            self.0.push(other);
        }
        self
    }

    /// Drop all handles.
    pub fn clear(&mut self) {
        self.0.clear()
    }
}
impl FromIterator<VarHandle> for VarHandles {
    fn from_iter<T: IntoIterator<Item = VarHandle>>(iter: T) -> Self {
        VarHandles(iter.into_iter().filter(|h| !h.is_dummy()).collect())
    }
}
impl<const N: usize> From<[VarHandle; N]> for VarHandles {
    fn from(handles: [VarHandle; N]) -> Self {
        handles.into_iter().collect()
    }
}
impl Extend<VarHandle> for VarHandles {
    fn extend<T: IntoIterator<Item = VarHandle>>(&mut self, iter: T) {
        for handle in iter {
            self.push(handle);
        }
    }
}
impl IntoIterator for VarHandles {
    type Item = VarHandle;

    type IntoIter = smallvec::IntoIter<[VarHandle; 2]>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}
impl ops::Deref for VarHandles {
    type Target = smallvec::SmallVec<[VarHandle; 2]>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl ops::DerefMut for VarHandles {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}
impl From<VarHandle> for VarHandles {
    fn from(value: VarHandle) -> Self {
        let mut r = VarHandles::dummy();
        r.push(value);
        r
    }
}

#[cfg(feature = "type_names")]
fn value_type_name(var: &dyn VarImpl) -> &'static str {
    var.value_type_name()
}
#[cfg(not(feature = "type_names"))]
#[inline(always)]
fn value_type_name(_: &dyn VarImpl) -> &'static str {
    ""
}