File: blob.R

package info (click to toggle)
r-cran-blob 1.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: sh: 13; makefile: 2
file content (88 lines) | stat: -rw-r--r-- 2,012 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#' @import vctrs
#' @import rlang
NULL

#' @importFrom methods setOldClass
setOldClass(c("blob", "vctrs_list_of", "vctrs_vctr"))

#' Construct a blob object
#'
#' `new_blob()` is a low-level constructor that takes a list of
#' raw vectors.
#' `blob()` constructs a blob from individual raw vectors.
#' `as_blob()` and `is_blob()` are simple forwarders to [vctrs::vec_cast()]
#' and [inherits()], respectively.
#'
#' @seealso [as.blob()] for the legacy interface for specifying casts.
#'
#' @param ... Individual raw vectors
#' @param x A list of raw vectors, or other object to coerce
#' @export
#' @examples
#' x1 <- charToRaw("Good morning")
#' x2 <- as.raw(c(0x48, 0x65, 0x6c, 0x6c, 0x6f))
#'
#' new_blob(list(x1, x2))
#' blob(x1, x2)
#'
#' as.blob(c("Good morning", "Good evening"))
blob <- function(...) {
  x <- list2(...)
  validate_blob(x)
  new_blob(x)
}

#' @export
#' @rdname blob
new_blob <- function(x = list()) {
  new_list_of(x, ptype = raw(), class = "blob")
}

#' @export
#' @rdname blob
validate_blob <- function(x) {
  x_arg <- as_label(enexpr(x))
  check_raw_list(x, x_arg)
}

check_raw_list <- function(x, x_arg) {
  if (!is_raw_list(x)) {
    stop("`", x_arg, "` must be a list of raw vectors", call. = FALSE)
  }
}

#' @export
#' @rdname blob
as_blob <- function(x) {
  vec_cast(x, new_blob())
}

#' @export
#' @rdname blob
is_blob <- function(x) {
  vec_is(x, new_blob())
}

#' Deprecated generic
#'
#' The `as.blob()` generic has been deprecated in favor of
#' the [as_blob()] function (for users) and the [vec_cast.blob()] method
#' (for implementers).
#' Implement a `vec_cast.blob.myclass()` method to support
#' coercing objects of your class to blobs.
#' See [vctrs::vec_cast()] for more detail.
#'
#' @param x An object.
#' @param ... Passed on to methods.
#'
#' @export
#' @keywords internal
as.blob <- function(x, ...) {
  signal_soft_deprecated("as.blob() is deprecated, use as_blob().")
  UseMethod("as.blob")
}

#' @export
as.blob.default <- function(x, ...) {
  as_blob(x)
}