fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
3.74k stars 267 forks source link

I found that it does not convert Vec<u8> to Uint8List as stated in the doc, but instead converts it to List<int> #2042

Closed limitLiu closed 4 weeks ago

limitLiu commented 4 weeks ago

Describe the bug

I saw in the doc that

When you have Vec\<u8> (or Vec\<i8>, or Vec\<i32>, etc), it will be translated into Uint8List or its friends.

Vec and array

Steps to reproduce

The issue can be reproduced by running flutter_rust_bridge_codegen generate with version v2.0.0-dev.37.

use std::result;

use crate::frb_generated::SseEncode;

#[derive(Debug)]
pub enum FFIError {
  JsonError(sonic_rs::Error),
  QrCode(String),
}

pub type Result<T> = result::Result<T, FFIError>;

impl From<sonic_rs::Error> for FFIError {
  fn from(value: sonic_rs::Error) -> Self {
    FFIError::JsonError(value)
  }
}

impl SseEncode for FFIError {
  fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
    <String>::sse_encode(format!("{:?}", self), serializer);
  }
}

impl std::fmt::Display for FFIError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match *self {
      FFIError::JsonError(ref cause) => write!(f, "{}", cause),
      FFIError::QrCode(ref cause) => write!(f, "{}", cause),
    }
  }
}

pub fn decode(bytes: Vec<u8>, width: u32, height: u32) -> Result<String> {
  let decoder = bardecoder::default_decoder();
  let img = ImageBuffer::from_vec(width, height, bytes).unwrap();
  let img = DynamicImage::ImageRgba8(img);
  let results = decoder.decode(&img);
  let result = &results[0];
  match result {
    Ok(r) => Ok(r.to_owned()),
    Err(e) => Err(err::FFIError::QrCode(e.to_string())),
  }
}
Future<String> decode(
        {required List<int> bytes, required int width, required int height}) =>
    RustLib.instance.api
        .crateApiBarDecoderDecode(bytes: bytes, width: width, height: height);

Logs

-

Expected behavior

I hope it works as described in the doc.

Generated binding code

No response

OS

macOS

Version of flutter_rust_bridge_codegen

2.0.0-dev.37

Flutter info

No response

Version of clang++

No response

Additional context

No response

fzyzcjy commented 4 weeks ago

Hi, IIRC we have loosen the type constraint, such that users can feel more natural.

IIRC, a Uint8List in Dart implements the List<int>. Thus, you can provide both a Uint8List and a List<int> into that argument.

Similarly, the return type when having Vec<u8> may be List<int>, but indeed it is a Uint8List. For this, one way to check is to get a real return object and print(thatObject.runtimeType) and see it.

If that does not hold, feel free to ping me and then it is a real bug!

github-actions[bot] commented 2 weeks ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.