Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public protocol CredentialsProviding {
}

/// A pair defining an identity provider and a valid login token sourced from it.
public struct CognitoLoginPair: CStruct {
public class CognitoLoginPair: CStruct {
public var IdentityProviderName: String
public var IdentityProviderToken: String

Expand All @@ -20,17 +20,20 @@ public struct CognitoLoginPair: CStruct {
self.IdentityProviderName = identityProviderName
self.IdentityProviderToken = identityProviderToken
}


private var name_buffer: aws_byte_buf = aws_byte_buf()
private var token_buffer: aws_byte_buf = aws_byte_buf()
typealias RawType = aws_cognito_identity_provider_token_pair
func withCStruct<Result>(_ body: (aws_cognito_identity_provider_token_pair) -> Result) -> Result {
var token_pair = aws_cognito_identity_provider_token_pair()

return withByteCursorFromStrings(IdentityProviderName,
IdentityProviderToken) { identityProviderNameCursor, IdentityProviderTokenCursor in
token_pair.identity_provider_name = identityProviderNameCursor
token_pair.identity_provider_token = IdentityProviderTokenCursor
return body(token_pair)
}
token_pair.identity_provider_name = aws_byte_cursor_from_buf(&self.name_buffer)
token_pair.identity_provider_token = aws_byte_cursor_from_buf(&self.token_buffer)
return body(token_pair)
}

deinit{
aws_byte_buf_clean_up(&self.name_buffer)
aws_byte_buf_clean_up(&self.token_buffer)
}
}

Expand Down
10 changes: 6 additions & 4 deletions Source/AwsCommonRuntimeKit/crt/CStruct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ extension CStruct {
}

extension Array where Element: CStruct {
/// Convert a CStruct Array into raw c pointer. The function would not do a deep copy of the CStruct element. If you required a deep copy
/// make sure to clean up the memory underneath.
/// Convert a CStruct Array into raw c pointer. However, as the conversion made by withCPointer will be unavailable out of
/// the scope, we need to make sure that we have a copy of the underlying memory.
func withAWSArrayList<Result>(_ body: (OpaquePointer?) throws -> Result) rethrows -> Result {
guard capacity != 0 else {
return try body(nil)
Expand All @@ -42,9 +42,11 @@ extension Array where Element: CStruct {
forEach {
$0.withCPointer {
// `aws_array_list_push_back` will do a memory copy of $0 into array_list, but it would
// not do a deep copy there.
// not do a deep copy there. For example, if the c struct contains a aws_byte_cursor, it
// would only copy the cursor, not the underlying memory it points to, it is possible that
// the memory will be unavailable out of the scope.
guard aws_array_list_push_back(array_list, $0) == AWS_OP_SUCCESS else {
fatalError("Unable to add user property")
fatalError("Unable to add array element")
}
}
}
Expand Down
Loading