Skip to content

Commit dd5bf5e

Browse files
dbenson24copybara-github
authored andcommitted
Add upb_Map_GetMutable API to upb
PiperOrigin-RevId: 742338581
1 parent 3e20696 commit dd5bf5e

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

rust/upb/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ pub use extension_registry::{upb_ExtensionRegistry, RawExtensionRegistry};
2929

3030
mod map;
3131
pub use map::{
32-
upb_Map, upb_Map_Clear, upb_Map_Delete, upb_Map_Get, upb_Map_Insert, upb_Map_New, upb_Map_Next,
33-
upb_Map_Size, MapInsertStatus, RawMap, UPB_MAP_BEGIN,
32+
upb_Map, upb_Map_Clear, upb_Map_Delete, upb_Map_Get, upb_Map_GetMutable, upb_Map_Insert,
33+
upb_Map_New, upb_Map_Next, upb_Map_Size, MapInsertStatus, RawMap, UPB_MAP_BEGIN,
3434
};
3535

3636
mod message;

rust/upb/map.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// https://developers.google.com/open-source/licenses/bsd
77

88
use super::opaque_pointee::opaque_pointee;
9-
use super::{upb_MessageValue, CType, RawArena};
9+
use super::{upb_Message, upb_MessageValue, CType, RawArena};
1010
use core::ptr::NonNull;
1111

1212
opaque_pointee!(upb_Map);
@@ -33,6 +33,7 @@ extern "C" {
3333
arena: RawArena,
3434
) -> MapInsertStatus;
3535
pub fn upb_Map_Get(map: RawMap, key: upb_MessageValue, value: *mut upb_MessageValue) -> bool;
36+
pub fn upb_Map_GetMutable(map: RawMap, key: upb_MessageValue) -> *mut upb_Message;
3637
pub fn upb_Map_Delete(
3738
map: RawMap,
3839
key: upb_MessageValue,
@@ -60,6 +61,7 @@ mod tests {
6061
assert_linked!(upb_Map_Size);
6162
assert_linked!(upb_Map_Insert);
6263
assert_linked!(upb_Map_Get);
64+
assert_linked!(upb_Map_GetMutable);
6365
assert_linked!(upb_Map_Delete);
6466
assert_linked!(upb_Map_Clear);
6567
assert_linked!(upb_Map_Next);

upb/message/map.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "upb/hash/str_table.h"
1818
#include "upb/mem/arena.h"
1919
#include "upb/message/internal/map.h"
20+
#include "upb/message/internal/types.h"
2021
#include "upb/message/map.h"
2122
#include "upb/message/message.h"
2223
#include "upb/message/value.h"
@@ -52,6 +53,16 @@ bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
5253
return _upb_Map_Get(map, &key, map->key_size, val, map->val_size);
5354
}
5455

56+
struct upb_Message* upb_Map_GetMutable(upb_Map* map, upb_MessageValue key) {
57+
UPB_ASSERT(map->val_size == sizeof(upb_Message*));
58+
upb_Message* val = NULL;
59+
if (_upb_Map_Get(map, &key, map->key_size, &val, sizeof(upb_Message*))) {
60+
return val;
61+
} else {
62+
return NULL;
63+
}
64+
}
65+
5566
void upb_Map_Clear(upb_Map* map) { _upb_Map_Clear(map); }
5667

5768
upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,

upb/message/map.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "upb/base/descriptor_constants.h"
1414
#include "upb/mem/arena.h"
1515
#include "upb/message/internal/map.h"
16+
#include "upb/message/internal/types.h"
1617
#include "upb/message/value.h"
1718
#include "upb/mini_table/field.h"
1819
#include "upb/mini_table/message.h"
@@ -39,6 +40,12 @@ UPB_API size_t upb_Map_Size(const upb_Map* map);
3940
UPB_API bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
4041
upb_MessageValue* val);
4142

43+
// Returns a mutable pointer to the value for the given key. Returns NULL if the
44+
// key is not present.
45+
// This function is only legal to call for maps that contain messages.
46+
UPB_API struct upb_Message* upb_Map_GetMutable(upb_Map* map,
47+
upb_MessageValue key);
48+
4249
// Removes all entries in the map.
4350
UPB_API void upb_Map_Clear(upb_Map* map);
4451

@@ -71,7 +78,7 @@ UPB_API bool upb_Map_Delete(upb_Map* map, upb_MessageValue key,
7178
// ...
7279
// }
7380

74-
#define kUpb_Map_Begin ((size_t) - 1)
81+
#define kUpb_Map_Begin ((size_t)-1)
7582

7683
// Advances to the next entry. Returns false if no more entries are present.
7784
// Otherwise returns true and populates both *key and *value.

0 commit comments

Comments
 (0)