Skip to content

Commit 87f7fe2

Browse files
wojdyrwjakob
authored andcommitted
add none_caster<T> as a base of three type_casters:
type_caster<std::nullptr_t> type_caster<std::monostate> type_caster<std::nullopt_t>
1 parent 8636eb5 commit 87f7fe2

File tree

3 files changed

+7
-28
lines changed

3 files changed

+7
-28
lines changed

include/nanobind/nb_cast.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,22 @@ template <> struct type_caster<void> {
238238
}
239239
};
240240

241-
template <> struct type_caster<std::nullptr_t> {
241+
template <typename T> struct none_caster {
242242
bool from_python(handle src, uint8_t, cleanup_list *) noexcept {
243243
if (src.is_none())
244244
return true;
245245
return false;
246246
}
247247

248-
static handle from_cpp(std::nullptr_t, rv_policy, cleanup_list *) noexcept {
248+
static handle from_cpp(T, rv_policy, cleanup_list *) noexcept {
249249
return none().release();
250250
}
251251

252-
NB_TYPE_CASTER(std::nullptr_t, const_name("None"))
252+
NB_TYPE_CASTER(T, const_name("None"))
253253
};
254254

255+
template <> struct type_caster<std::nullptr_t> : none_caster<std::nullptr_t> { };
256+
255257
template <> struct type_caster<bool> {
256258
bool from_python(handle src, uint8_t, cleanup_list *) noexcept {
257259
if (src.ptr() == Py_True) {

include/nanobind/stl/optional.h

+1-13
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,7 @@ template <typename T> struct remove_opt_mono<std::optional<T>>
2121
template <typename T>
2222
struct type_caster<std::optional<T>> : optional_caster<std::optional<T>> {};
2323

24-
template <> struct type_caster<std::nullopt_t> {
25-
bool from_python(handle src, uint8_t, cleanup_list *) noexcept {
26-
if (src.is_none())
27-
return true;
28-
return false;
29-
}
30-
31-
static handle from_cpp(std::nullopt_t, rv_policy, cleanup_list *) noexcept {
32-
return none().release();
33-
}
34-
35-
NB_TYPE_CASTER(std::nullopt_t, const_name("None"))
36-
};
24+
template <> struct type_caster<std::nullopt_t> : none_caster<std::nullopt_t> { };
3725

3826
NAMESPACE_END(detail)
3927
NAMESPACE_END(NB_NAMESPACE)

include/nanobind/stl/variant.h

+1-12
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,7 @@ struct concat_variant<std::variant<Ts1...>, std::variant<Ts2...>, Ts3...>
2424
template <typename... Ts> struct remove_opt_mono<std::variant<Ts...>>
2525
: concat_variant<std::conditional_t<std::is_same_v<std::monostate, Ts>, std::variant<>, std::variant<remove_opt_mono_t<Ts>>>...> {};
2626

27-
template <> struct type_caster<std::monostate> {
28-
NB_TYPE_CASTER(std::monostate, const_name("None"))
29-
30-
bool from_python(handle src, uint8_t, cleanup_list *) noexcept {
31-
return src.is_none();
32-
}
33-
34-
static handle from_cpp(const std::monostate &, rv_policy,
35-
cleanup_list *) noexcept {
36-
return none().release();
37-
}
38-
};
27+
template <> struct type_caster<std::monostate> : none_caster<std::monostate> { };
3928

4029
template <typename... Ts> struct type_caster<std::variant<Ts...>> {
4130
NB_TYPE_CASTER(std::variant<Ts...>, union_name(make_caster<Ts>::Name...))

0 commit comments

Comments
 (0)