Skip to content

Commit 02e7456

Browse files
authored
Merge pull request #11 from tidyverse/f-vctrs
- The `blob` class is now based on `list_of(raw())` from the _vctrs_ package (#11).
2 parents 5519ce9 + a0d96db commit 02e7456

23 files changed

+586
-150
lines changed

DESCRIPTION

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ URL: https://github.com/tidyverse/blob
2121
BugReports: https://github.com/tidyverse/blob/issues
2222
Imports:
2323
methods,
24-
prettyunits
24+
prettyunits,
25+
rlang,
26+
vctrs
2527
Suggests:
2628
covr,
29+
crayon,
2730
pillar (>= 1.2.1),
2831
testthat
32+
Remotes:
33+
r-lib/vctrs
2934
Encoding: UTF-8
3035
LazyData: true
3136
Roxygen: list(markdown = TRUE, roclets = c("collate", "namespace", "rd", "pkgapi::api_roclet"))

NAMESPACE

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
S3method("[",blob)
4-
S3method("[<-",blob)
5-
S3method("[[<-",blob)
6-
S3method("is.na<-",blob)
7-
S3method(as.blob,blob)
8-
S3method(as.blob,character)
9-
S3method(as.blob,integer)
10-
S3method(as.blob,list)
11-
S3method(as.blob,raw)
12-
S3method(as.data.frame,blob)
13-
S3method(c,blob)
3+
S3method(as.blob,default)
144
S3method(format,blob)
15-
S3method(is.na,blob)
16-
S3method(print,blob)
5+
S3method(obj_print_data,blob)
6+
S3method(vec_cast,blob)
7+
S3method(vec_cast.blob,blob)
8+
S3method(vec_cast.blob,character)
9+
S3method(vec_cast.blob,default)
10+
S3method(vec_cast.blob,integer)
11+
S3method(vec_cast.blob,list)
12+
S3method(vec_cast.blob,logical)
13+
S3method(vec_cast.blob,raw)
14+
S3method(vec_cast.list,blob)
15+
S3method(vec_ptype_abbr,blob)
16+
S3method(vec_ptype_full,blob)
17+
S3method(vec_type2,blob)
18+
S3method(vec_type2.blob,blob)
19+
S3method(vec_type2.blob,default)
20+
S3method(vec_type2.blob,list)
21+
S3method(vec_type2.blob,vctrs_unspecified)
22+
S3method(vec_type2.list,blob)
1723
export(as.blob)
24+
export(as_blob)
1825
export(blob)
26+
export(is_blob)
1927
export(new_blob)
28+
export(vec_cast.blob)
29+
export(vec_type2.blob)
30+
import(rlang)
31+
import(vctrs)
2032
importFrom(methods,setOldClass)

R/accessors.R

Lines changed: 0 additions & 30 deletions
This file was deleted.

R/blob.R

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
#' @import vctrs
2+
#' @import rlang
3+
NULL
4+
15
#' @importFrom methods setOldClass
2-
setOldClass("blob")
6+
setOldClass(c("blob", "vctrs_list_of", "vctrs_vctr"))
37

48
#' Construct a blob object
59
#'
610
#' `new_blob()` is a low-level constructor that takes a list of
7-
#' raw vectors. `blob()` constructs a blob from individual raw vectors,
8-
#' and `as.blob()` is a S3 generic that converts existing objects.
11+
#' raw vectors.
12+
#' `blob()` constructs a blob from individual raw vectors.
13+
#' `as_blob()` and `is_blob()` are simple forwarders to [vctrs::vec_cast()]
14+
#' and [inherits()], respectively.
15+
#'
16+
#' @seealso [as.blob()] for the legacy interface for specifying casts.
917
#'
1018
#' @param ... Individual raw vectors
1119
#' @param x A list of raw vectors, or other object to coerce
@@ -19,48 +27,55 @@ setOldClass("blob")
1927
#'
2028
#' as.blob(c("Good morning", "Good evening"))
2129
blob <- function(...) {
22-
new_blob(list(...))
30+
x <- list2(...)
31+
check_raw_list(x)
32+
new_blob(x)
2333
}
2434

25-
#' @export
26-
#' @rdname blob
27-
new_blob <- function(x) {
35+
check_raw_list <- function(x) {
36+
quo <- enquo(x)
2837
if (!is_raw_list(x)) {
29-
stop("`x` must be a list of raw vectors", call. = FALSE)
38+
stop("`", as_label(quo), "` must be a list of raw vectors", call. = FALSE)
3039
}
31-
structure(x, class = "blob")
3240
}
3341

3442
#' @export
3543
#' @rdname blob
36-
as.blob <- function(x, ...) {
37-
UseMethod("as.blob")
38-
}
39-
40-
#' @export
41-
as.blob.blob <- function(x, ...) {
42-
x
44+
new_blob <- function(x = list()) {
45+
vec_assert(x, list())
46+
new_list_of(x, ptype = raw(), class = "blob")
4347
}
4448

4549
#' @export
46-
as.blob.list <- function(x, ...) {
47-
new_blob(x)
50+
#' @rdname blob
51+
as_blob <- function(x) {
52+
vec_cast(x, new_blob())
4853
}
4954

5055
#' @export
51-
as.blob.raw <- function(x, ...) {
52-
new_blob(list(x))
56+
#' @rdname blob
57+
is_blob <- function(x) {
58+
inherits(x, "blob")
5359
}
5460

61+
#' Deprecated generic
62+
#'
63+
#' The `as.blob()` generic has been deprecated in favor of
64+
#' [vec_cast.blob()].
65+
#' Implement a `vec_cast.blob.myclass()` method to support
66+
#' coercing objects of your class to blobs.
67+
#' See [vctrs::vec_cast()] for more detail.
68+
#'
69+
#' @param x An object.
70+
#' @param ... Passed on to methods.
71+
#'
5572
#' @export
56-
as.blob.character <- function(x, ...) {
57-
new_blob(lapply(x, charToRaw))
73+
as.blob <- function(x, ...) {
74+
signal_soft_deprecated("as.blob() is deprecated, use as_blob().")
75+
UseMethod("as.blob")
5876
}
5977

6078
#' @export
61-
as.blob.integer <- function(x, ...) {
62-
new_blob(lapply(x, as.raw))
79+
as.blob.default <- function(x, ...) {
80+
as_blob(x)
6381
}
64-
65-
#' @export
66-
as.data.frame.blob <- as.data.frame.difftime

R/cast.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#' Casting
2+
#'
3+
#' Double dispatch methods to support [vctrs::vec_cast()].
4+
#'
5+
#' @inheritParams vctrs::vec_cast
6+
#'
7+
#' @method vec_cast blob
8+
#' @export
9+
#' @export vec_cast.blob
10+
vec_cast.blob <- function(x, to) UseMethod("vec_cast.blob")
11+
12+
#' @method vec_cast.blob default
13+
#' @export
14+
vec_cast.blob.default <- function(x, to) stop_incompatible_cast(x, to)
15+
16+
#' @method vec_cast.blob logical
17+
#' @export
18+
vec_cast.blob.logical <- function(x, to) vec_unspecified_cast(x, to)
19+
20+
#' @method vec_cast.blob blob
21+
#' @export
22+
vec_cast.blob.blob <- function(x, to) x
23+
24+
#' @method vec_cast.blob list
25+
#' @export
26+
vec_cast.blob.list <- function(x, to) blob(!!!x)
27+
28+
#' @method vec_cast.blob integer
29+
#' @export
30+
vec_cast.blob.integer <- function(x, to) {
31+
warn_deprecated("Coercing an integer vector to a blob is deprecated, please coerce to a list first.")
32+
blob(!!!lapply(x, as_single_raw))
33+
}
34+
35+
#' @method vec_cast.blob raw
36+
#' @export
37+
vec_cast.blob.raw <- function(x, to) blob(x)
38+
39+
#' @method vec_cast.blob character
40+
#' @export
41+
vec_cast.blob.character <- function(x, to) blob(!!!lapply(x, charToRaw))
42+
43+
#' @method vec_cast.list blob
44+
#' @export
45+
vec_cast.list.blob <- function(x, to) vec_data(x)

R/coerce.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#' Coercion
2+
#'
3+
#' Double dispatch methods to support [vctrs::vec_type2()].
4+
#'
5+
#' @inheritParams vctrs::vec_type2
6+
#'
7+
#' @method vec_type2 blob
8+
#' @export
9+
#' @export vec_type2.blob
10+
vec_type2.blob <- function(x, y) UseMethod("vec_type2.blob", y)
11+
12+
#' @method vec_type2.blob default
13+
#' @export
14+
vec_type2.blob.default <- function(x, y) stop_incompatible_type(x, y)
15+
16+
#' @method vec_type2.blob blob
17+
#' @export
18+
vec_type2.blob.blob <- function(x, y) {
19+
new_blob(list())
20+
}
21+
22+
#' @method vec_type2.blob vctrs_unspecified
23+
#' @export
24+
vec_type2.blob.vctrs_unspecified <- function(x, y) x
25+
26+
#' @method vec_type2.blob list
27+
#' @export
28+
vec_type2.blob.list <- function(x, y) {
29+
check_raw_list(y)
30+
new_blob(list())
31+
}
32+
33+
#' @method vec_type2.list blob
34+
#' @export
35+
vec_type2.list.blob <- function(x, y) {
36+
check_raw_list(x)
37+
new_blob(list())
38+
}

0 commit comments

Comments
 (0)