Skip to content

Commit

Permalink
Feedback, and copy string-slices into byte-arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed May 16, 2024
1 parent d794f4e commit 86acb04
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
8 changes: 4 additions & 4 deletions include/toit/toit.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ typedef toit_err_t (*toit_msg_on_created_cb_t)(void* user_data, toit_msg_context
* service) to this service.
*
* The data is owned by the receiver and must be freed.
* If the Toit side sent a string, then the data is guaranteed to be null-terminated. The
* length does *not* include the null-terminator.
* If the Toit side sent a string, then the data is guaranteed to be 0-terminated. The
* length does *not* include the 0-terminator.
*
* @param user_data The user data passed to `toit_msg_add_handler`.
* @param sender The PID of the sender.
Expand All @@ -87,8 +87,8 @@ typedef toit_err_t (*toit_msg_on_message_cb_t)(void* user_data, int sender, uint
* It is an error to not reply to the request, or to reply more than once.
*
* The data is owned by the receiver and must be freed.
* If the Toit side sent a string, then the data is guaranteed to be null-terminated. The
* length does *not* include the null-terminator.
* If the Toit side sent a string, then the data is guaranteed to be 0-terminated. The
* length does *not* include the 0-terminator.
*
* @param user_data The user data passed to `toit_msg_add_handler`.
* @param sender The PID of the sender.
Expand Down
5 changes: 2 additions & 3 deletions lib/system/external.toit
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ class Client:
/** Helper to convert the $message to a string or ByteArray. */
encode-message_ message/io.Data --copy/bool -> io.Data:
bytes/ByteArray := ?
if message is string:
// By taking a copy we ensure that we don't have to worry about string slices.
return (message as string).copy
if message is string and message is not StringSlice_:
return message
if not copy and message is ByteArray:
return message
bytes = ByteArray message.byte-size
Expand Down

0 comments on commit 86acb04

Please sign in to comment.