# Path: rx/disposable.py
# class BooleanDisposable(Cancelable):
#   """Represents a disposable resource that
#   can be checked if it already has been disposed"""
# 
#   def __init__(self):
#     super(BooleanDisposable, self).__init__()
# 
#   def dispose(self):
#     self._isDisposed.exchange(True)
# 
# class Disposable(object):
#   """Represents a disposable object"""
# 
#   def dispose(self):
#     pass
# 
#   def __enter__(self):
#     return self
# 
#   def __exit__(self, exc_type, exc_value, traceback):
#     self.dispose()
# 
#   @staticmethod
#   def create(action):
#     return AnonymouseDisposable(action)
# 
#   @staticmethod
#   def empty():
#     return disposableEmpty
# 
# Path: rx/internal.py
# class Struct(object):
#   def __init__(self, **entries):
#     self.__dict__.update(entries)
# 
#   def __eq__(self, other):
#     if not isinstance(other, Struct):
#       return False
# 
#     return self.__dict__ == other.__dict__
# 
#   def __repr__(self):
#     return repr(self.__dict__)
# 
# Path: rx/observable.py
# class Producer(Observable):
#   """Base class for implementation of query operators, providing
#   performance benefits over the use of Observable.Create"""
# 
#   def subscribeCore(self, observer):
#     return self.subscribeRaw(observer, True)
# 
#   def subscribeRaw(self, observer, enableSafequard):
#     sink = SingleAssignmentDisposable()
#     subscription = SingleAssignmentDisposable()
# 
#     d = CompositeDisposable(sink, subscription)
# 
#     if enableSafequard:
#       observer = AutoDetachObserver(observer, d)
# 
#     def assignSink(s):
#       sink.disposable = s
# 
#     def scheduled():
#       subscription.disposable = self.run(observer, subscription, assignSink)
#       return Disposable.empty()
# 
#     if Scheduler.currentThread.isScheduleRequired():
#       Scheduler.currentThread.schedule(scheduled)
#     else:
#       scheduled()
# 
#     return d
# 
#   def run(self, observer, cancel, setSink):
#     raise NotImplementedError()


from rx.disposable import BooleanDisposable, Disposable
from rx.internal import Struct
from rx.observable import Producer
import rx.linq.sink


class ToObservable(Producer):
  def __init__(self, source, scheduler):
    self.source = source
    self.scheduler = scheduler

  def run(self, observer, cancel, setSink):
    sink = self.Sink(self, observer, cancel)
    setSink(sink)
    return sink.run()

  class Sink(rx.linq.sink.Sink):
    def __init__(self, parent, observer, cancel):
      super(ToObservable.Sink, self).__init__(observer, cancel)
      self.parent = parent

    def run(self):
      it = None

      try:
        it = iter(self.parent.source)
      except Exception as e:
        self.observer.onError(e)
        self.dispose()

# Path: rx/disposable.py
# class BooleanDisposable(Cancelable):
#   """Represents a disposable resource that
#   can be checked if it already has been disposed"""
# 
#   def __init__(self):
#     super(BooleanDisposable, self).__init__()
# 
#   def dispose(self):
#     self._isDisposed.exchange(True)
# 
# class Disposable(object):
#   """Represents a disposable object"""
# 
#   def dispose(self):
#     pass
# 
#   def __enter__(self):
#     return self
# 
#   def __exit__(self, exc_type, exc_value, traceback):
#     self.dispose()
# 
#   @staticmethod
#   def create(action):
#     return AnonymouseDisposable(action)
# 
#   @staticmethod
#   def empty():
#     return disposableEmpty
# 
# Path: rx/internal.py
# class Struct(object):
#   def __init__(self, **entries):
#     self.__dict__.update(entries)
# 
#   def __eq__(self, other):
#     if not isinstance(other, Struct):
#       return False
# 
#     return self.__dict__ == other.__dict__
# 
#   def __repr__(self):
#     return repr(self.__dict__)
# 
# Path: rx/observable.py
# class Producer(Observable):
#   """Base class for implementation of query operators, providing
#   performance benefits over the use of Observable.Create"""
# 
#   def subscribeCore(self, observer):
#     return self.subscribeRaw(observer, True)
# 
#   def subscribeRaw(self, observer, enableSafequard):
#     sink = SingleAssignmentDisposable()
#     subscription = SingleAssignmentDisposable()
# 
#     d = CompositeDisposable(sink, subscription)
# 
#     if enableSafequard:
#       observer = AutoDetachObserver(observer, d)
# 
#     def assignSink(s):
#       sink.disposable = s
# 
#     def scheduled():
#       subscription.disposable = self.run(observer, subscription, assignSink)
#       return Disposable.empty()
# 
#     if Scheduler.currentThread.isScheduleRequired():
#       Scheduler.currentThread.schedule(scheduled)
#     else:
#       scheduled()
# 
#     return d
# 
#   def run(self, observer, cancel, setSink):
#     raise NotImplementedError()


# Path: rx/linq/toObservable.py
from rx.disposable import BooleanDisposable, Disposable
from rx.internal import Struct
from rx.observable import Producer
import rx.linq.sink



class ToObservable(Producer):
  def __init__(self, source, scheduler):
    self.source = source
    self.scheduler = scheduler

  def run(self, observer, cancel, setSink):
    sink = self.Sink(self, observer, cancel)
    setSink(sink)
    return sink.run()

  class Sink(rx.linq.sink.Sink):
    def __init__(self, parent, observer, cancel):
      super(ToObservable.Sink, self).__init__(observer, cancel)
      self.parent = parent

    def run(self):
      it = None

      try:
        it = iter(self.parent.source)
      except Exception as e:
        self.observer.onError(e)
        self.dispose()

        return Disposable.empty()