cloudpeers / ffi-gen

Call rust from any language.
20 stars 5 forks source link

How to access generated dart stream bindings #59

Closed canewsin closed 2 years ago

canewsin commented 2 years ago

rsh content

object Timer {
    fn to_string() -> string;
}

object TimerStream {
    fn subscribe() -> Stream<Timer>;
}

rust

#[derive(Debug, Clone)]
pub struct Timer(u8);

impl Timer {
    pub fn to_string(&self) -> String {
        format!("{:?}", self.0)
    }
}

impl Stream for Timer {
    type Item = Timer;

    fn poll_next(
        self: std::pin::Pin<&mut Self>,
        _cx: &mut std::task::Context<'_>,
    ) -> Poll<Option<Self::Item>> {
        match self.0 {
            sec => {
                std::thread::sleep(Duration::from_millis(1000));
                Poll::Ready(Some(Timer(sec + 1)))
            }
        }
    }
}

pub struct TimerStream(Timer);

impl TimerStream {
    pub fn subscribe(&self) -> impl Stream<Item = Timer> {
        let timer = (&self.0).clone();
        timer
    }
}

unable to find access for subscribe, because Dart constructor is private, is there any method to access this stream(i.e subscribe()) some where ?

generated dart

class TimerStream {
  final Api _api;
  final _Box _box;

  TimerStream._(this._api, this._box);

  Stream<Timer> subscribe() {
    var tmp0 = 0;
    tmp0 = _box.borrow();
    final tmp1 = _api._timerStreamSubscribe(
      tmp0,
    );
    final tmp3 = tmp1;
    final ffi.Pointer<ffi.Void> tmp3_0 = ffi.Pointer.fromAddress(tmp3);
    final tmp3_1 = _Box(_api, tmp3_0, "__TimerStream_subscribe_stream_drop");
    tmp3_1._finalizer = _api._registerFinalizer(tmp3_1);
    final tmp2 = _nativeStream(tmp3_1, _api.__timerStreamSubscribeStreamPoll);
    return tmp2;
  }

  /// Manually drops the object and unregisters the FinalizableHandle.
  void drop() {
    _box.drop();
  }
}
dvc94ch commented 2 years ago

you need to either add a constructor on the object or a function that creates the object.

try this:

pub fn create_timer_stream() -> TimerStream {
    TimerStream::new()
}