Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] add FileHandle.write(self, data: List[UInt8]) and possibly define a serialization interface #2614

Closed
1 task done
martinvuyk opened this issue May 11, 2024 · 1 comment
Labels
enhancement New feature or request mojo-repo Tag all issues with this label

Comments

@martinvuyk
Copy link

martinvuyk commented May 11, 2024

Review Mojo's priorities

What is your request?

Besides adding the function mentioned in the title, I've been thinking of maybe defining a serialization and deserialization interface (trait) would make many later implementations easier (Json, XML, etc.). That way:

trait Serializable:
  fn to_bytes(self) -> List[UInt8]:
    ...


trait DeSerializable:
  @staticmethod
  fn from_bytes(data: List[UInt8])  ->  Self:
    ...


struct FileHandle:
  fn write[T: Serializable](self, data: T):
    self.write(data.to_bytes())

  fn write[T: Serializable, size: Int](self, data: List[T]):
    # this could be buffered if size is small and sent all at once though it would use more memory
    for item in data:
      self.write(item[].to_bytes())
      _ = self.seek(size, os.SEEK_CUR)

  fn read[T: DeSerializable, size: Int](self) -> T:
    return T.from_bytes(self.read_bytes(size))

  fn read[T: DeSerializable, size: Int, amnt_items: Int](self) -> List[T]:
    var data = self.read_bytes(amnt_items * size)
    var items = List[T](capacity=amnt_items)
    # could this be vectorized/parallelized according to size and or amnt_items?
    for i in range(amnt_items):
      items[i] = T.from_bytes(data[i * size:(i + 1) * size])
    return items

What is your motivation for this change?

Having a clearly defined serialization interface and adding it to all mayor stdlib types will make everything easier later on. And if List[UInt8] will represent every type for exchange with external systems it also helps to define interfaces for it, and there would be no need to make something like pickle.

Any other details?

No response

@martinvuyk martinvuyk added enhancement New feature or request mojo-repo Tag all issues with this label labels May 11, 2024
Copy link
Collaborator

Thanks for the feature request! I'm going to close this in favor of #1504 which is the same idea. We'll get to read and write traits soonish as we start building out the asyncio part of the standard library. The path we take on async IO will help inform how we define these traits (and whether async is the default for example), among other things. There's some nuances though such as how do we model and handle uninitialize memory for these interfaces and traits.

@JoeLoser JoeLoser closed this as not planned Won't fix, can't repro, duplicate, stale May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

2 participants