Skip to content

Commit

Permalink
refactor: move stream to extra rust file
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Aug 23, 2024
1 parent 2730029 commit 39c01d1
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 224 deletions.
5 changes: 3 additions & 2 deletions lib/src/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:rhttp/src/model/response.dart';
import 'package:rhttp/src/model/settings.dart';
import 'package:rhttp/src/rust/api/error.dart' as rust_error;
import 'package:rhttp/src/rust/api/http.dart' as rust;
import 'package:rhttp/src/rust/api/stream.dart' as rust_stream;
import 'package:rhttp/src/util/stream_listener.dart';

/// Non-Generated helper function that is used by
Expand Down Expand Up @@ -41,10 +42,10 @@ Future<HttpResponse> requestInternalGeneric(HttpRequest request) async {
body: request.body,
);

final rust.Dart2RustStreamReceiver? bodyStream;
final rust_stream.Dart2RustStreamReceiver? bodyStream;
if (request.body is HttpBodyBytesStream) {
final stream = (request.body as HttpBodyBytesStream).stream;
final (sender, receiver) = await rust.createStream();
final (sender, receiver) = await rust_stream.createStream();
listenToStreamWithBackpressure(
stream: stream,
onData: (data) async => await sender.add(data: data),
Expand Down
12 changes: 1 addition & 11 deletions lib/src/rust/api/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import 'client.dart';
import 'error.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'package:freezed_annotation/freezed_annotation.dart' hide protected;
import 'stream.dart';
part 'http.freezed.dart';

// These functions are ignored because they are not marked as `pub`: `build_cancel_tokens`, `from_version`, `header_to_vec`, `make_http_request_helper`, `make_http_request_inner`, `make_http_request_receive_stream_inner`, `register_client_internal`, `to_method`
// These types are ignored because they are not used by any `pub` functions: `RequestCancelTokens`
// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `clone`, `clone`, `clone`, `fmt`

Future<(Dart2RustStreamSink, Dart2RustStreamReceiver)> createStream() =>
RustLib.instance.api.crateApiHttpCreateStream();

Future<PlatformInt64> registerClient({required ClientSettings settings}) =>
RustLib.instance.api.crateApiHttpRegisterClient(settings: settings);

Expand Down Expand Up @@ -84,14 +82,6 @@ Stream<Uint8List> makeHttpRequestReceiveStream(
Future<bool> cancelRequest({required PlatformInt64 address}) =>
RustLib.instance.api.crateApiHttpCancelRequest(address: address);

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<Dart2RustStreamReceiver>>
abstract class Dart2RustStreamReceiver implements RustOpaqueInterface {}

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<Dart2RustStreamSink>>
abstract class Dart2RustStreamSink implements RustOpaqueInterface {
Future<void> add({required int data});
}

@freezed
sealed class HttpBody with _$HttpBody {
const HttpBody._();
Expand Down
20 changes: 20 additions & 0 deletions lib/src/rust/api/stream.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This file is automatically generated, so please do not edit it.
// Generated by `flutter_rust_bridge`@ 2.3.0.

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
import 'error.dart';
import 'http.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

Future<(Dart2RustStreamSink, Dart2RustStreamReceiver)> createStream() =>
RustLib.instance.api.crateApiStreamCreateStream();

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner< Dart2RustStreamReceiver>>
abstract class Dart2RustStreamReceiver implements RustOpaqueInterface {}

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<Dart2RustStreamSink>>
abstract class Dart2RustStreamSink implements RustOpaqueInterface {
Future<void> add({required int data});
}
137 changes: 69 additions & 68 deletions lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import 'api/client.dart';
import 'api/error.dart';
import 'api/http.dart';
import 'api/stream.dart';
import 'dart:async';
import 'dart:convert';
import 'frb_generated.dart';
Expand Down Expand Up @@ -70,7 +71,7 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
String get codegenVersion => '2.3.0';

@override
int get rustContentHash => 1538750230;
int get rustContentHash => -2112869887;

static const kDefaultExternalLibraryLoaderConfig =
ExternalLibraryLoaderConfig(
Expand All @@ -83,14 +84,8 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
abstract class RustLibApi extends BaseApi {
Future<ClientSettings> crateApiClientClientSettingsDefault();

Future<void> crateApiHttpDart2RustStreamSinkAdd(
{required Dart2RustStreamSink that, required int data});

Future<bool> crateApiHttpCancelRequest({required PlatformInt64 address});

Future<(Dart2RustStreamSink, Dart2RustStreamReceiver)>
crateApiHttpCreateStream();

Future<HttpResponse> crateApiHttpMakeHttpRequest(
{PlatformInt64? clientAddress,
ClientSettings? settings,
Expand Down Expand Up @@ -129,6 +124,12 @@ abstract class RustLibApi extends BaseApi {

Future<void> crateApiInitInitApp();

Future<void> crateApiStreamDart2RustStreamSinkAdd(
{required Dart2RustStreamSink that, required int data});

Future<(Dart2RustStreamSink, Dart2RustStreamReceiver)>
crateApiStreamCreateStream();

RustArcIncrementStrongCountFnType
get rust_arc_increment_strong_count_Dart2RustStreamReceiver;

Expand Down Expand Up @@ -180,42 +181,14 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: [],
);

@override
Future<void> crateApiHttpDart2RustStreamSinkAdd(
{required Dart2RustStreamSink that, required int data}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_Auto_RefMut_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDart2RustStreamSink(
that, serializer);
sse_encode_u_8(data, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 2, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_unit,
decodeErrorData: sse_decode_rhttp_error,
),
constMeta: kCrateApiHttpDart2RustStreamSinkAddConstMeta,
argValues: [that, data],
apiImpl: this,
));
}

TaskConstMeta get kCrateApiHttpDart2RustStreamSinkAddConstMeta =>
const TaskConstMeta(
debugName: "Dart2RustStreamSink_add",
argNames: ["that", "data"],
);

@override
Future<bool> crateApiHttpCancelRequest({required PlatformInt64 address}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_i_64(address, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 3, port: port_);
funcId: 2, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_bool,
Expand All @@ -232,31 +205,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["address"],
);

@override
Future<(Dart2RustStreamSink, Dart2RustStreamReceiver)>
crateApiHttpCreateStream() {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 4, port: port_);
},
codec: SseCodec(
decodeSuccessData:
sse_decode_record_auto_owned_rust_opaque_flutter_rust_bridgefor_generated_rust_auto_opaque_inner_dart_2_rust_stream_sink_auto_owned_rust_opaque_flutter_rust_bridgefor_generated_rust_auto_opaque_inner_dart_2_rust_stream_receiver,
decodeErrorData: null,
),
constMeta: kCrateApiHttpCreateStreamConstMeta,
argValues: [],
apiImpl: this,
));
}

TaskConstMeta get kCrateApiHttpCreateStreamConstMeta => const TaskConstMeta(
debugName: "create_stream",
argNames: [],
);

@override
Future<HttpResponse> crateApiHttpMakeHttpRequest(
{PlatformInt64? clientAddress,
Expand Down Expand Up @@ -287,7 +235,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
onCancelToken, serializer);
sse_encode_bool(cancelable, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 5, port: port_);
funcId: 3, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_http_response,
Expand Down Expand Up @@ -365,7 +313,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
onCancelToken, serializer);
sse_encode_bool(cancelable, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 6, port: port_);
funcId: 4, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_unit,
Expand Down Expand Up @@ -420,7 +368,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_box_autoadd_client_settings(settings, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 7, port: port_);
funcId: 5, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_i_64,
Expand All @@ -444,7 +392,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
callFfi: () {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_box_autoadd_client_settings(settings, serializer);
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 8)!;
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 6)!;
},
codec: SseCodec(
decodeSuccessData: sse_decode_i_64,
Expand All @@ -471,7 +419,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_encode_i_64(address, serializer);
sse_encode_bool(cancelRunningRequests, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 9, port: port_);
funcId: 7, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_unit,
Expand All @@ -494,7 +442,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 10, port: port_);
funcId: 8, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_unit,
Expand All @@ -511,6 +459,59 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: [],
);

@override
Future<void> crateApiStreamDart2RustStreamSinkAdd(
{required Dart2RustStreamSink that, required int data}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_Auto_RefMut_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerDart2RustStreamSink(
that, serializer);
sse_encode_u_8(data, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 9, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_unit,
decodeErrorData: sse_decode_rhttp_error,
),
constMeta: kCrateApiStreamDart2RustStreamSinkAddConstMeta,
argValues: [that, data],
apiImpl: this,
));
}

TaskConstMeta get kCrateApiStreamDart2RustStreamSinkAddConstMeta =>
const TaskConstMeta(
debugName: "Dart2RustStreamSink_add",
argNames: ["that", "data"],
);

@override
Future<(Dart2RustStreamSink, Dart2RustStreamReceiver)>
crateApiStreamCreateStream() {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 10, port: port_);
},
codec: SseCodec(
decodeSuccessData:
sse_decode_record_auto_owned_rust_opaque_flutter_rust_bridgefor_generated_rust_auto_opaque_inner_dart_2_rust_stream_sink_auto_owned_rust_opaque_flutter_rust_bridgefor_generated_rust_auto_opaque_inner_dart_2_rust_stream_receiver,
decodeErrorData: null,
),
constMeta: kCrateApiStreamCreateStreamConstMeta,
argValues: [],
apiImpl: this,
));
}

TaskConstMeta get kCrateApiStreamCreateStreamConstMeta => const TaskConstMeta(
debugName: "create_stream",
argNames: [],
);

Future<void> Function(int, dynamic)
encode_DartFn_Inputs_http_response_Output_unit_AnyhowException(
FutureOr<void> Function(HttpResponse) raw) {
Expand Down Expand Up @@ -2607,5 +2608,5 @@ class Dart2RustStreamSinkImpl extends RustOpaque
);

Future<void> add({required int data}) => RustLib.instance.api
.crateApiHttpDart2RustStreamSinkAdd(that: this, data: data);
.crateApiStreamDart2RustStreamSinkAdd(that: this, data: data);
}
1 change: 1 addition & 0 deletions lib/src/rust/frb_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import 'api/client.dart';
import 'api/error.dart';
import 'api/http.dart';
import 'api/stream.dart';
import 'dart:async';
import 'dart:convert';
import 'dart:ffi' as ffi;
Expand Down
1 change: 1 addition & 0 deletions lib/src/rust/frb_generated.web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import 'api/client.dart';
import 'api/error.dart';
import 'api/http.dart';
import 'api/stream.dart';
import 'dart:async';
import 'dart:convert';
import 'frb_generated.dart';
Expand Down
Loading

0 comments on commit 39c01d1

Please sign in to comment.