tqchen commented on code in PR #623: URL: https://github.com/apache/tvm-ffi/pull/623#discussion_r3521605950
########## rust/tvm-ffi/src/collections/map.rs: ########## @@ -0,0 +1,474 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +//! Immutable [`Map`] container backed by the C++ `ffi.Map` object. +//! +//! The `MapObj` header mirrors C++ `MapBaseObj`, so [`Map::len`] reads `size` +//! directly (no FFI). The hash-table storage itself is an implementation detail, +//! so lookups and iteration are delegated to the global functions the C++ runtime +//! registers (`ffi.MapGetItem`, `ffi.MapForwardIterFunctor`, ...): there is no +//! Map-specific C ABI, and a Rust re-implementation would have to replicate the +//! hashing (`AnyHash`/`AnyEqual`) and dense/small probing, just as the Python +//! bindings also delegate. Element types are erased to `Any`, so `K`/`V` are +//! validated lazily on access (see [`Map::get`] / [`Map::iter`]) rather than by +//! [`AnyCompatible::check_any_strict`]. +use std::fmt::Debug; +use std::marker::PhantomData; +use std::ops::Deref; + +use crate::any::TryFromTemp; +use crate::derive::Object; +use crate::function::Function; +use crate::object::{Object, ObjectArc}; +use crate::{Any, AnyCompatible, AnyView, Error, ObjectRefCore, Result}; +use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex; +use tvm_ffi_sys::{TVMFFIAny, TVMFFIObject}; + +/// Container object for [`Map`]. The header fields mirror C++ `MapBaseObj` +/// (`include/tvm/ffi/container/map_base.h`) so [`Map::len`] can read `size` +/// without an FFI call; the storage `data` points to stays opaque. +/// +/// This is a partial mirror by design: `MapBaseObj` has one more trailing field +/// after `slots_` (a `data_deleter_` function pointer), omitted here because this +/// binding is read-only — it only reads fields up to `size`, which precede it, +/// and never allocates a `MapObj` itself (the C++ runtime allocates the larger +/// `Small`/`DenseMapBaseObj` subclass), so the trailing field is never touched. +/// The layout also assumes `TVM_FFI_DEBUG_WITH_ABI_CHANGE` is off (the default): +/// with that debug flag set, `MapBaseObj` gains a *leading* `state_marker` field +/// that would shift every offset below. +#[repr(C)] +#[derive(Object)] +#[type_key = "ffi.Map"] +#[type_index(TypeIndex::kTVMFFIMap)] +pub struct MapObj { Review Comment: this one is fine, since we do not have it on, maybe we should even consider emove it -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
