Skip to content

Commit

Permalink
std::encoding::json: minor improvements for smart pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 18, 2024
1 parent 43f0a3d commit 6fea8b6
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions std/encoding/json/encode.jule
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,6 @@ impl jsonEncoder {
self.buf.writeByte('"')
}

fn encodePtr[T, Flag](mut self, &t: T)! {
if t == nil {
self.encodeNil()
} else {
comptime::TypeAlias(elem, comptime::TypeOf(T).Elem())
self.encode[elem, Flag](*t) else { error(error) }
}
}

fn encodeStruct[T, Flag](mut self, &t: T)! {
const tt = comptime::TypeOf(T)
const useIndent = comptime::TypeOf(Flag) == comptime::TypeOf(encodeFlagType.Indent)
Expand Down Expand Up @@ -383,7 +374,15 @@ impl jsonEncoder {
}
const match tt.Kind() {
| comptime::Kind.SmartPtr:
self.encodePtr[T, Flag](t) else { error(error) }
// Avoid to implement as a function.
// Fills call stack faster especially with recursive structures.
// Handle smart pointers here quicky and forward to relevant encoder.
if t == nil {
self.encodeNil()
} else {
comptime::TypeAlias(elem, tt.Elem())
self.encode[elem, Flag](*t) else { error(error) }
}
| comptime::Kind.Struct:
self.encodeStruct[T, Flag](t) else { error(error) }
| comptime::Kind.Map:
Expand Down

0 comments on commit 6fea8b6

Please sign in to comment.