Skip to content

Commit

Permalink
faster intToRustBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed Jun 4, 2024
1 parent 062b391 commit fcb5f5a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/gen/compounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ macro_rules! impl_renderable_for_compound {

static RustBuffer lower(Api api, $type_label value) {
if (value == null) {
return toRustBuffer(api, Uint8List.fromList([0]));
return intToRustBuffer(api, 0);
}

final length = $cl_name.allocationSize(value);
Expand Down
2 changes: 1 addition & 1 deletion src/gen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn generate_enum(obj: &Enum, type_helper: &dyn TypeHelperRenderer) -> dart::
}

static RustBuffer lower(Api api, $cls_name input) {
return toRustBuffer(api, createUint8ListFromInt(input.index + 1)); // So enums aren't zero indexed?
return intToRustBuffer(api, input.index + 1); // So enums aren't zero indexed?
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/gen/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use genco::quote;
use heck::{ToLowerCamelCase, ToUpperCamelCase};

use uniffi_bindgen::backend::CodeType;
use uniffi_bindgen::interface::{AsType, Callable, ExternalKind, FfiType, Type};
use uniffi_bindgen::ComponentInterface;
use uniffi_bindgen::interface::{AsType, FfiType, Type};

use crate::gen::primitives;

Expand Down
22 changes: 22 additions & 0 deletions src/gen/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,28 @@ impl Renderer<(FunctionDefinition, dart::Tokens)> for TypeHelpersRenderer<'_> {
return RustBuffer.fromBytes(api, bytes.ref);
}

RustBuffer intToRustBuffer(Api api, int value) {
int length = value.bitLength ~/ 8 + 1;

// Ensure the length is either 4 or 8
if (length != 4 && length != 8) {
length = (value < 0x100000000) ? 4 : 8;
}


final Pointer<Uint8> frameData = calloc<Uint8>(length); // Allocate a pointer large enough.
final pointerList = frameData.asTypedList(length); // Create a list that uses our pointer and copy in the data.

for (int i = length - 1; i >= 0; i--) {
pointerList[i] = value & 0xFF;
value >>= 8;
}
final bytes = calloc<ForeignBytes>();
bytes.ref.len = length;
bytes.ref.data = frameData;
return RustBuffer.fromBytes(api, bytes.ref);
}


class ForeignBytes extends Struct {
@Int32()
Expand Down

0 comments on commit fcb5f5a

Please sign in to comment.